Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
52 / 52 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
InitUserPreference | |
100.00% |
52 / 52 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
3 |
1 | <?php |
2 | |
3 | use MediaWiki\Maintenance\Maintenance; |
4 | |
5 | // @codeCoverageIgnoreStart |
6 | require_once __DIR__ . '/Maintenance.php'; |
7 | // @codeCoverageIgnoreEnd |
8 | |
9 | /** |
10 | * Maintenance script that initializes a user preference |
11 | * based on the value of another preference. |
12 | * |
13 | * This is done by copying any non-empty (i.e. excluding 0 or null) value for a given |
14 | * source preference to be the value of the target preference. Only users with the value set |
15 | * will have the target preference set. |
16 | * |
17 | * @ingroup Maintenance |
18 | */ |
19 | class InitUserPreference extends Maintenance { |
20 | public function __construct() { |
21 | parent::__construct(); |
22 | $this->addOption( |
23 | 'target', |
24 | 'Name of the user preference to initialize', |
25 | true, |
26 | true, |
27 | 't' |
28 | ); |
29 | $this->addOption( |
30 | 'source', |
31 | 'Name of the user preference to take the value from', |
32 | true, |
33 | true, |
34 | 's' |
35 | ); |
36 | $this->setBatchSize( 300 ); |
37 | } |
38 | |
39 | public function execute() { |
40 | $target = $this->getOption( 'target' ); |
41 | $source = $this->getOption( 'source' ); |
42 | $this->output( "Initializing '$target' based on the value of '$source'\n" ); |
43 | |
44 | $dbr = $this->getReplicaDB(); |
45 | $dbw = $this->getPrimaryDB(); |
46 | |
47 | $iterator = new BatchRowIterator( |
48 | $dbr, |
49 | $dbr->newSelectQueryBuilder() |
50 | ->from( 'user_properties' ) |
51 | ->select( [ 'up_user', 'up_value' ] ) |
52 | ->where( [ |
53 | 'up_property' => $source, |
54 | $dbr->expr( 'up_value', '!=', null ), |
55 | $dbr->expr( 'up_value', '!=', '0' ), |
56 | ] ) |
57 | ->caller( __METHOD__ ), |
58 | [ 'up_user', 'up_property' ], |
59 | $this->getBatchSize() |
60 | ); |
61 | |
62 | $processed = 0; |
63 | foreach ( $iterator as $batch ) { |
64 | foreach ( $batch as $row ) { |
65 | $dbw->newInsertQueryBuilder() |
66 | ->insertInto( 'user_properties' ) |
67 | ->row( [ |
68 | 'up_user' => $row->up_user, |
69 | 'up_property' => $target, |
70 | 'up_value' => $row->up_value, |
71 | ] ) |
72 | ->onDuplicateKeyUpdate() |
73 | ->uniqueIndexFields( [ 'up_user', 'up_property' ] ) |
74 | ->set( [ 'up_value' => $row->up_value ] ) |
75 | ->caller( __METHOD__ )->execute(); |
76 | |
77 | $processed += $dbw->affectedRows(); |
78 | } |
79 | } |
80 | |
81 | $this->output( "Processed $processed user(s)\n" ); |
82 | $this->output( "Finished!\n" ); |
83 | } |
84 | } |
85 | |
86 | // @codeCoverageIgnoreStart |
87 | $maintClass = InitUserPreference::class; |
88 | require_once RUN_MAINTENANCE_IF_MAIN; |
89 | // @codeCoverageIgnoreEnd |