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