Merge "docs: Fix typo"
[mediawiki.git] / maintenance / userOptions.php
blobf8c3faf121967e6dbf1e525f751338f9c82e9df9
1 <?php
2 /**
3 * Script to change users preferences on the fly.
5 * Made on an original idea by Fooey (freenode)
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.
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.
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
22 * @file
23 * @ingroup Maintenance
24 * @author Antoine Musso <hashar at free dot fr>
27 use MediaWiki\Maintenance\Maintenance;
28 use MediaWiki\User\User;
29 use MediaWiki\User\UserIdentityValue;
30 use Wikimedia\Rdbms\IExpression;
31 use Wikimedia\Rdbms\SelectQueryBuilder;
33 // @codeCoverageIgnoreStart
34 require_once __DIR__ . '/Maintenance.php';
35 // @codeCoverageIgnoreEnd
37 /**
38 * @ingroup Maintenance
40 class UserOptionsMaintenance extends Maintenance {
42 public function __construct() {
43 parent::__construct();
45 $this->addDescription( 'Pass through all users and change or delete one of their options.
46 The new option is NOT validated.' );
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
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 );
69 /**
70 * Do the actual work
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 );
91 /**
92 * List default options and their value
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 ) );
102 foreach ( $def as $opt => $value ) {
103 $this->output( sprintf( "%-{$maxOpt}s: %s\n", $opt, $value ) );
108 * List options usage
110 private function showUsageStats() {
111 $option = $this->getArg( 0 );
113 $ret = [];
114 $userOptionsLookup = $this->getServiceContainer()->getUserOptionsLookup();
115 $defaultOptions = $userOptionsLookup->getDefaultOptions();
117 if ( $option && !array_key_exists( $option, $defaultOptions ) ) {
118 $this->fatalError( "Invalid user option. Use --list to see valid choices\n" );
121 // We list user by user_id from one of the replica DBs
122 $dbr = $this->getServiceContainer()->getConnectionProvider()->getReplicaDatabase();
124 $result = $dbr->newSelectQueryBuilder()
125 ->select( [ 'user_id' ] )
126 ->from( 'user' )
127 ->caller( __METHOD__ )->fetchResultSet();
129 foreach ( $result as $id ) {
130 $user = User::newFromId( $id->user_id );
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;
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;
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" );
153 print "\n";
158 * Change our users options
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' );
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 );
174 if ( !$dryRun ) {
175 $forUsers = ( $fromUserId || $toUserId ) ? "some users (ID $fromUserId-$toUserId)" : 'ALL USERS';
176 $this->warn(
177 <<<WARN
178 The script is about to change the options for $forUsers in the database.
179 Users with option <$option> = '$fromAsText' will be made to use '$to'.
181 Abort with control-c in the next five seconds....
182 WARN
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,
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 ) );
207 if ( $tempUserConfig->isKnown() ) {
208 $queryBuilderTemplate->andWhere(
209 $tempUserConfig->getMatchCondition( $dbr, 'user_name', IExpression::NOT_LIKE )
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;
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;
235 $fromAsText = $oldOptionMatchingDefault ?? $fromAsText;
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 );
244 $this->waitForReplication();
245 } while ( $result->numRows() );
249 * Delete occurrences of the option (with the given value, if provided)
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' );
258 if ( !$dryRun ) {
259 $forUsers = ( $fromUserId || $toUserId ) ? "some users (ID $fromUserId-$toUserId)" : 'ALL USERS';
260 $this->warn( <<<WARN
261 The script is about to delete '$option' option for $forUsers from user_properties table.
262 This action is IRREVERSIBLE.
264 Abort with control-c in the next five seconds....
265 WARN
269 $dbr = $this->getReplicaDB();
270 $dbw = $this->getPrimaryDB();
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 ) );
283 if ( $this->hasOption( 'old' ) ) {
284 $queryBuilder->andWhere( [ 'up_value' => $old ] );
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() );
291 $userIds = $queryBuilder->caller( __METHOD__ )->fetchFieldValues();
292 if ( $userIds === [] ) {
293 // no rows left
294 break;
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 ] );
304 $delete->caller( __METHOD__ )->execute();
305 $rowsInThisBatch = $dbw->affectedRows();
306 } else {
307 $rowsInThisBatch = count( $userIds );
310 $this->waitForReplication();
311 $rowsNum += $rowsInThisBatch;
312 $minUserId = max( $userIds );
315 if ( !$dryRun ) {
316 $this->output( "Done! Deleted $rowsNum rows.\n" );
317 } else {
318 $this->output( "Would delete $rowsNum rows.\n" );
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;
328 if ( $option === null ) {
329 $this->fatalError( "Option name is required" );
332 if ( !$dryRun ) {
333 $this->warn( <<<WARN
334 This script is about to delete all rows in user_properties that match the current
335 defaults for the user (including conditional defaults).
336 This action is IRREVERSIBLE.
338 Abort with control-c in the next five seconds....
339 WARN
343 $dbr = $this->getDB( DB_REPLICA );
344 $dbw = $this->getDB( DB_PRIMARY );
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__ );
354 if ( $toUserId !== null ) {
355 $queryBuilderTemplate->andWhere( $dbr->expr( 'up_user', '<=', $toUserId ) );
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;
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 );
372 $this->waitForReplication();
373 } while ( $result->numRows() );
375 $this->output( "Done!\n" );
379 * The warning message and countdown
381 private function warn( string $message ) {
382 if ( $this->hasOption( 'nowarn' ) ) {
383 return;
386 $this->output( $message );
387 $this->countDown( 5 );
391 // @codeCoverageIgnoreStart
392 $maintClass = UserOptionsMaintenance::class;
393 require_once RUN_MAINTENANCE_IF_MAIN;
394 // @codeCoverageIgnoreEnd