initUserPreference.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Initialize a user preference based on the value
  4. * of another preference.
  5. *
  6. * @ingroup Maintenance
  7. */
  8. require_once __DIR__ . '/Maintenance.php';
  9. /**
  10. * Maintenance script that initializes a user preference
  11. * based on the value of another preference.
  12. *
  13. * @ingroup Maintenance
  14. */
  15. class InitUserPreference extends Maintenance {
  16. public function __construct() {
  17. parent::__construct();
  18. $this->addOption(
  19. 'target',
  20. 'Name of the user preference to initialize',
  21. true,
  22. true,
  23. 't'
  24. );
  25. $this->addOption(
  26. 'source',
  27. 'Name of the user preference to take the value from',
  28. true,
  29. true,
  30. 's'
  31. );
  32. $this->setBatchSize( 300 );
  33. }
  34. public function execute() {
  35. $target = $this->getOption( 'target' );
  36. $source = $this->getOption( 'source' );
  37. $this->output( "Initializing '$target' based on the value of '$source'\n" );
  38. $dbr = $this->getDB( DB_REPLICA );
  39. $dbw = $this->getDB( DB_MASTER );
  40. $iterator = new BatchRowIterator(
  41. $dbr,
  42. 'user_properties',
  43. [ 'up_user', 'up_property' ],
  44. $this->getBatchSize()
  45. );
  46. $iterator->setFetchColumns( [ 'up_user', 'up_value' ] );
  47. $iterator->addConditions( [
  48. 'up_property' => $source,
  49. 'up_value IS NOT NULL',
  50. 'up_value != 0',
  51. ] );
  52. $processed = 0;
  53. foreach ( $iterator as $batch ) {
  54. foreach ( $batch as $row ) {
  55. $values = [
  56. 'up_user' => $row->up_user,
  57. 'up_property' => $target,
  58. 'up_value' => $row->up_value,
  59. ];
  60. $dbw->upsert(
  61. 'user_properties',
  62. $values,
  63. [ 'up_user', 'up_property' ],
  64. $values,
  65. __METHOD__
  66. );
  67. $processed += $dbw->affectedRows();
  68. }
  69. }
  70. $this->output( "Processed $processed user(s)\n" );
  71. $this->output( "Finished!\n" );
  72. }
  73. }
  74. $maintClass = InitUserPreference::class; // Tells it to run the class
  75. require_once RUN_MAINTENANCE_IF_MAIN;