Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
34.27% covered (danger)
34.27%
73 / 213
37.50% covered (danger)
37.50%
3 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
UserOptionsMaintenance
34.27% covered (danger)
34.27%
73 / 213
37.50% covered (danger)
37.50%
3 / 8
979.56
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 execute
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
8.23
 listAvailableOptions
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 showUsageStats
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
10
 updateOptions
0.00% covered (danger)
0.00%
0 / 61
0.00% covered (danger)
0.00%
0 / 1
240
 deleteOptions
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
156
 deleteDefaults
18.18% covered (danger)
18.18%
6 / 33
0.00% covered (danger)
0.00%
0 / 1
25.72
 warn
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Script to change users preferences on the fly.
4 *
5 * Made on an original idea by Fooey (freenode)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 * @author Antoine Musso <hashar at free dot fr>
25 */
26
27use MediaWiki\Maintenance\Maintenance;
28use MediaWiki\User\User;
29use MediaWiki\User\UserIdentityValue;
30use Wikimedia\Rdbms\IExpression;
31use Wikimedia\Rdbms\SelectQueryBuilder;
32
33// @codeCoverageIgnoreStart
34require_once __DIR__ . '/Maintenance.php';
35// @codeCoverageIgnoreEnd
36
37/**
38 * @ingroup Maintenance
39 */
40class UserOptionsMaintenance extends Maintenance {
41
42    public function __construct() {
43        parent::__construct();
44
45        $this->addDescription( 'Pass through all users and change or delete one of their options.
46The new option is NOT validated.' );
47
48        $this->addOption( 'list', 'List available user options and their default value' );
49        $this->addOption( 'usage', 'Report all options statistics or just one if you specify it' );
50        $this->addOption(
51            'old',
52            'The value to look for. If it is a default value for the option, pass --old-is-default as well.',
53            false, true, false, true
54        );
55        $this->addOption( 'old-is-default', 'If passed, --old is interpreted as a default value.' );
56        $this->addOption( 'new', 'New value to update users with', false, true );
57        $this->addOption( 'delete', 'Delete the option instead of updating' );
58        $this->addOption( 'delete-defaults', 'Delete user_properties row matching the default' );
59        $this->addOption( 'fromuserid', 'Start from this user ID when changing/deleting options',
60            false, true );
61        $this->addOption( 'touserid', 'Do not go beyond this user ID when changing/deleting options',
62            false, true );
63        $this->addOption( 'nowarn', 'Hides the 5 seconds warning' );
64        $this->addOption( 'dry', 'Do not save user settings back to database' );
65        $this->addArg( 'option name', 'Name of the option to change or provide statistics about', false );
66        $this->setBatchSize( 100 );
67    }
68
69    /**
70     * Do the actual work
71     */
72    public function execute() {
73        if ( $this->hasOption( 'list' ) ) {
74            $this->listAvailableOptions();
75        } elseif ( $this->hasOption( 'usage' ) ) {
76            $this->showUsageStats();
77        } elseif ( $this->hasOption( 'old' )
78            && $this->hasOption( 'new' )
79            && $this->hasArg( 0 )
80        ) {
81            $this->updateOptions();
82        } elseif ( $this->hasOption( 'delete' ) ) {
83            $this->deleteOptions();
84        } elseif ( $this->hasOption( 'delete-defaults' ) ) {
85            $this->deleteDefaults();
86        } else {
87            $this->maybeHelp( true );
88        }
89    }
90
91    /**
92     * List default options and their value
93     */
94    private function listAvailableOptions() {
95        $userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup();
96        $def = $userOptionsLookup->getDefaultOptions( null );
97        ksort( $def );
98        $maxOpt = 0;
99        foreach ( $def as $opt => $value ) {
100            $maxOpt = max( $maxOpt, strlen( $opt ) );
101        }
102        foreach ( $def as $opt => $value ) {
103            $this->output( sprintf( "%-{$maxOpt}s: %s\n", $opt, $value ) );
104        }
105    }
106
107    /**
108     * List options usage
109     */
110    private function showUsageStats() {
111        $option = $this->getArg( 0 );
112
113        $ret = [];
114        $userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup();
115        $defaultOptions = $userOptionsLookup->getDefaultOptions();
116
117        if ( $option && !array_key_exists( $option, $defaultOptions ) ) {
118            $this->fatalError( "Invalid user option. Use --list to see valid choices\n" );
119        }
120
121        // We list user by user_id from one of the replica DBs
122        $dbr = $this->getServiceContainer()->getConnectionProvider()->getReplicaDatabase();
123
124        $result = $dbr->newSelectQueryBuilder()
125            ->select( [ 'user_id' ] )
126            ->from( 'user' )
127            ->caller( __METHOD__ )->fetchResultSet();
128
129        foreach ( $result as $id ) {
130            $user = User::newFromId( $id->user_id );
131
132            // Get the options and update stats
133            if ( $option ) {
134                $userValue = $userOptionsLookup->getOption( $user, $option );
135                if ( $userValue != $defaultOptions[$option] ) {
136                    $ret[$option][$userValue] = ( $ret[$option][$userValue] ?? 0 ) + 1;
137                }
138            } else {
139                foreach ( $defaultOptions as $name => $defaultValue ) {
140                    $userValue = $userOptionsLookup->getOption( $user, $name );
141                    if ( $userValue != $defaultValue ) {
142                        $ret[$name][$userValue] = ( $ret[$name][$userValue] ?? 0 ) + 1;
143                    }
144                }
145            }
146        }
147
148        foreach ( $ret as $optionName => $usageStats ) {
149            $this->output( "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n" );
150            foreach ( $usageStats as $value => $count ) {
151                $this->output( " $count user(s): '$value'\n" );
152            }
153            print "\n";
154        }
155    }
156
157    /**
158     * Change our users options
159     */
160    private function updateOptions() {
161        $dryRun = $this->hasOption( 'dry' );
162        $settingWord = $dryRun ? 'Would set' : 'Setting';
163        $option = $this->getArg( 0 );
164        $fromIsDefault = $this->hasOption( 'old-is-default' );
165        $from = $this->getOption( 'old' );
166        $to = $this->getOption( 'new' );
167
168        // The fromuserid parameter is inclusive, but iterating is easier with an exclusive
169        // range so convert it.
170        $fromUserId = (int)$this->getOption( 'fromuserid', 1 ) - 1;
171        $toUserId = (int)$this->getOption( 'touserid', 0 ) ?: null;
172        $fromAsText = implode( '|', $from );
173
174        if ( !$dryRun ) {
175            $forUsers = ( $fromUserId || $toUserId ) ? "some users (ID $fromUserId-$toUserId)" : 'ALL USERS';
176            $this->warn(
177                <<<WARN
178The script is about to change the options for $forUsers in the database.
179Users with option <$option> = '$fromAsText' will be made to use '$to'.
180
181Abort with control-c in the next five seconds....
182WARN
183            );
184        }
185
186        $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
187        $tempUserConfig = $this->getServiceContainer()->getTempUserConfig();
188        $dbr = $this->getReplicaDB();
189        $queryBuilderTemplate = $dbr->newSelectQueryBuilder()
190            ->table( 'user' )
191            ->leftJoin( 'user_properties', null, [
192                'user_id = up_user',
193                'up_property' => $option,
194            ] )
195            ->fields( [ 'user_id', 'user_name' ] )
196            // up_value is unindexed so this can be slow, but should be acceptable in a script
197            ->where( [ 'up_value' => $fromIsDefault ? null : $from ] )
198            // need to order by ID so we can use ID ranges for query continuation
199            // also needed for the fromuserid / touserid parameters to work
200            ->orderBy( 'user_id', SelectQueryBuilder::SORT_ASC )
201            ->limit( $this->getBatchSize() )
202            ->caller( __METHOD__ );
203        if ( $toUserId ) {
204            $queryBuilderTemplate->andWhere( $dbr->expr( 'user_id', '<=', $toUserId ) );
205        }
206
207        if ( $tempUserConfig->isKnown() ) {
208            $queryBuilderTemplate->andWhere(
209                $tempUserConfig->getMatchCondition( $dbr, 'user_name', IExpression::NOT_LIKE )
210            );
211        }
212
213        do {
214            $queryBuilder = clone $queryBuilderTemplate;
215            $queryBuilder->andWhere( $dbr->expr( 'user_id', '>', $fromUserId ) );
216            $result = $queryBuilder->fetchResultSet();
217            foreach ( $result as $row ) {
218                $fromUserId = (int)$row->user_id;
219                $oldOptionIsDefault = true;
220
221                $user = UserIdentityValue::newRegistered( $row->user_id, $row->user_name );
222                if ( $fromIsDefault ) {
223                    // $user has the default value for $option; skip if it doesn't match
224                    // NOTE: This is intentionally a loose comparison. $from is always a string
225                    // (coming from the command line), but the default value might be of a
226                    // different type.
227                    $oldOptionMatchingDefault = null;
228                    foreach ( $from as $oldOption ) {
229                        $oldOptionIsDefault = $oldOption != $userOptionsManager->getDefaultOption( $option, $user );
230                        if ( $oldOptionIsDefault ) {
231                            $oldOptionMatchingDefault = $oldOption;
232                            break;
233                        }
234                    }
235                    $fromAsText = $oldOptionMatchingDefault ?? $fromAsText;
236                }
237
238                $this->output( "$settingWord {$option} for {$row->user_name} from '{$fromAsText}' to '{$to}'\n" );
239                if ( !$dryRun && $oldOptionIsDefault ) {
240                    $userOptionsManager->setOption( $user, $option, $to );
241                    $userOptionsManager->saveOptions( $user );
242                }
243            }
244            $this->waitForReplication();
245        } while ( $result->numRows() );
246    }
247
248    /**
249     * Delete occurrences of the option (with the given value, if provided)
250     */
251    private function deleteOptions() {
252        $dryRun = $this->hasOption( 'dry' );
253        $option = $this->getArg( 0 );
254        $fromUserId = (int)$this->getOption( 'fromuserid', 0 );
255        $toUserId = (int)$this->getOption( 'touserid', 0 ) ?: null;
256        $old = $this->getOption( 'old' );
257
258        if ( !$dryRun ) {
259            $forUsers = ( $fromUserId || $toUserId ) ? "some users (ID $fromUserId-$toUserId)" : 'ALL USERS';
260            $this->warn( <<<WARN
261The script is about to delete '$option' option for $forUsers from user_properties table.
262This action is IRREVERSIBLE.
263
264Abort with control-c in the next five seconds....
265WARN
266            );
267        }
268
269        $dbr = $this->getReplicaDB();
270        $dbw = $this->getPrimaryDB();
271
272        $rowsNum = 0;
273        $rowsInThisBatch = -1;
274        $minUserId = $fromUserId;
275        while ( $rowsInThisBatch != 0 ) {
276            $queryBuilder = $dbr->newSelectQueryBuilder()
277                ->select( 'up_user' )
278                ->from( 'user_properties' )
279                ->where( [ 'up_property' => $option, $dbr->expr( 'up_user', '>', $minUserId ) ] );
280            if ( $this->hasOption( 'touserid' ) ) {
281                $queryBuilder->andWhere( $dbr->expr( 'up_user', '<', $toUserId ) );
282            }
283            if ( $this->hasOption( 'old' ) ) {
284                $queryBuilder->andWhere( [ 'up_value' => $old ] );
285            }
286            // need to order by ID so we can use ID ranges for query continuation
287            $queryBuilder
288                ->orderBy( 'up_user', SelectQueryBuilder::SORT_ASC )
289                ->limit( $this->getBatchSize() );
290
291            $userIds = $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
292            if ( $userIds === [] ) {
293                // no rows left
294                break;
295            }
296
297            if ( !$dryRun ) {
298                $delete = $dbw->newDeleteQueryBuilder()
299                    ->deleteFrom( 'user_properties' )
300                    ->where( [ 'up_property' => $option, 'up_user' => $userIds ] );
301                if ( $this->hasOption( 'old' ) ) {
302                    $delete->andWhere( [ 'up_value' => $old ] );
303                }
304                $delete->caller( __METHOD__ )->execute();
305                $rowsInThisBatch = $dbw->affectedRows();
306            } else {
307                $rowsInThisBatch = count( $userIds );
308            }
309
310            $this->waitForReplication();
311            $rowsNum += $rowsInThisBatch;
312            $minUserId = max( $userIds );
313        }
314
315        if ( !$dryRun ) {
316            $this->output( "Done! Deleted $rowsNum rows.\n" );
317        } else {
318            $this->output( "Would delete $rowsNum rows.\n" );
319        }
320    }
321
322    private function deleteDefaults() {
323        $dryRun = $this->hasOption( 'dry' );
324        $option = $this->getArg( 0 );
325        $fromUserId = (int)$this->getOption( 'fromuserid', 0 );
326        $toUserId = (int)$this->getOption( 'touserid', 0 ) ?: null;
327
328        if ( $option === null ) {
329            $this->fatalError( "Option name is required" );
330        }
331
332        if ( !$dryRun ) {
333            $this->warn( <<<WARN
334This script is about to delete all rows in user_properties that match the current
335defaults for the user (including conditional defaults).
336This action is IRREVERSIBLE.
337
338Abort with control-c in the next five seconds....
339WARN
340            );
341        }
342
343        $dbr = $this->getDB( DB_REPLICA );
344        $dbw = $this->getDB( DB_PRIMARY );
345
346        $queryBuilderTemplate = $dbr->newSelectQueryBuilder()
347            ->select( [ 'user_id', 'user_name', 'up_value' ] )
348            ->from( 'user_properties' )
349            ->join( 'user', null, [ 'up_user = user_id' ] )
350            ->where( [ 'up_property' => $option ] )
351            ->limit( $this->getBatchSize() )
352            ->caller( __METHOD__ );
353
354        if ( $toUserId !== null ) {
355            $queryBuilderTemplate->andWhere( $dbr->expr( 'up_user', '<=', $toUserId ) );
356        }
357
358        $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
359        do {
360            $queryBuilder = clone $queryBuilderTemplate;
361            $queryBuilder->andWhere( $dbr->expr( 'up_user', '>', $fromUserId ) );
362            $result = $queryBuilder->fetchResultSet();
363            foreach ( $result as $row ) {
364                $fromUserId = (int)$row->user_id;
365
366                // NOTE: If up_value equals to the default, this will drop the row. Otherwise, it
367                // is going to be a no-op.
368                $user = UserIdentityValue::newRegistered( $row->user_id, $row->user_name );
369                $userOptionsManager->setOption( $user, $option, $row->up_value );
370                $userOptionsManager->saveOptions( $user );
371            }
372            $this->waitForReplication();
373        } while ( $result->numRows() );
374
375        $this->output( "Done!\n" );
376    }
377
378    /**
379     * The warning message and countdown
380     *
381     * @param string $message
382     */
383    private function warn( string $message ) {
384        if ( $this->hasOption( 'nowarn' ) ) {
385            return;
386        }
387
388        $this->output( $message );
389        $this->countDown( 5 );
390    }
391}
392
393// @codeCoverageIgnoreStart
394$maintClass = UserOptionsMaintenance::class;
395require_once RUN_MAINTENANCE_IF_MAIN;
396// @codeCoverageIgnoreEnd