MediaWiki  master
initUserPreference.php
Go to the documentation of this file.
1 <?php
9 require_once __DIR__ . '/Maintenance.php';
10 
18  public function __construct() {
19  parent::__construct();
20  $this->addOption(
21  'target',
22  'Name of the user preference to initialize',
23  true,
24  true,
25  't'
26  );
27  $this->addOption(
28  'source',
29  'Name of the user preference to take the value from',
30  true,
31  true,
32  's'
33  );
34  $this->setBatchSize( 300 );
35  }
36 
37  public function execute() {
38  $target = $this->getOption( 'target' );
39  $source = $this->getOption( 'source' );
40  $this->output( "Initializing '$target' based on the value of '$source'\n" );
41 
42  $dbr = $this->getDB( DB_REPLICA );
43  $dbw = $this->getDB( DB_PRIMARY );
44 
45  $iterator = new BatchRowIterator(
46  $dbr,
47  'user_properties',
48  [ 'up_user', 'up_property' ],
49  $this->getBatchSize()
50  );
51  $iterator->setFetchColumns( [ 'up_user', 'up_value' ] );
52  $iterator->addConditions( [
53  'up_property' => $source,
54  'up_value IS NOT NULL',
55  'up_value != 0',
56  ] );
57  $iterator->setCaller( __METHOD__ );
58 
59  $processed = 0;
60  foreach ( $iterator as $batch ) {
61  foreach ( $batch as $row ) {
62  $dbw->upsert(
63  'user_properties',
64  [
65  'up_user' => $row->up_user,
66  'up_property' => $target,
67  'up_value' => $row->up_value,
68  ],
69  [ [ 'up_user', 'up_property' ] ],
70  [
71  'up_value' => $row->up_value,
72  ],
73  __METHOD__
74  );
75 
76  $processed += $dbw->affectedRows();
77  }
78  }
79 
80  $this->output( "Processed $processed user(s)\n" );
81  $this->output( "Finished!\n" );
82  }
83 }
84 
85 $maintClass = InitUserPreference::class;
86 require_once RUN_MAINTENANCE_IF_MAIN;
InitUserPreference\execute
execute()
Do the actual work.
Definition: initUserPreference.php:37
$maintClass
$maintClass
Definition: initUserPreference.php:85
BatchRowIterator
Allows iterating a large number of rows in batches transparently.
Definition: BatchRowIterator.php:33
InitUserPreference\__construct
__construct()
Default constructor.
Definition: initUserPreference.php:18
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$dbr
$dbr
Definition: testCompression.php:54
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:368
$source
$source
Definition: mwdoc-filter.php:34
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
InitUserPreference
Initialize a user preference based on the value of another preference.
Definition: initUserPreference.php:17
Maintenance\setBatchSize
setBatchSize( $s=0)
Definition: Maintenance.php:375