Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
InitUserPreference
0.00% covered (danger)
0.00%
0 / 51
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Initialize a user preference based on the value
4 * of another preference.
5 *
6 * @ingroup Maintenance
7 */
8
9require_once __DIR__ . '/Maintenance.php';
10
11/**
12 * Maintenance script that initializes a user preference
13 * based on the value of another preference.
14 *
15 * @ingroup Maintenance
16 */
17class InitUserPreference extends Maintenance {
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->getReplicaDB();
43        $dbw = $this->getPrimaryDB();
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            $dbr->expr( 'up_value', '!=', null ),
55            $dbr->expr( 'up_value', '!=', 0 ),
56        ] );
57        $iterator->setCaller( __METHOD__ );
58
59        $processed = 0;
60        foreach ( $iterator as $batch ) {
61            foreach ( $batch as $row ) {
62                $dbw->newInsertQueryBuilder()
63                    ->insertInto( 'user_properties' )
64                    ->row( [
65                        'up_user' => $row->up_user,
66                        'up_property' => $target,
67                        'up_value' => $row->up_value,
68                    ] )
69                    ->onDuplicateKeyUpdate()
70                    ->uniqueIndexFields( [ 'up_user', 'up_property' ] )
71                    ->set( [ 'up_value' => $row->up_value ] )
72                    ->caller( __METHOD__ )->execute();
73
74                $processed += $dbw->affectedRows();
75            }
76        }
77
78        $this->output( "Processed $processed user(s)\n" );
79        $this->output( "Finished!\n" );
80    }
81}
82
83$maintClass = InitUserPreference::class;
84require_once RUN_MAINTENANCE_IF_MAIN;