composer.json: Add a phan script command
[mediawiki.git] / maintenance / userOptions.php
blob2b5ee9b53b3e0fc3449eabc167f67fcf319d5c31
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 require_once __DIR__ . '/Maintenance.php';
29 /**
30 * @ingroup Maintenance
32 class UserOptionsMaintenance extends Maintenance {
34 public function __construct() {
35 parent::__construct();
37 $this->addDescription( 'Pass through all users and change one of their options.
38 The new option is NOT validated.' );
40 $this->addOption( 'list', 'List available user options and their default value' );
41 $this->addOption( 'usage', 'Report all options statistics or just one if you specify it' );
42 $this->addOption( 'old', 'The value to look for', false, true );
43 $this->addOption( 'new', 'New value to update users with', false, true );
44 $this->addOption( 'nowarn', 'Hides the 5 seconds warning' );
45 $this->addOption( 'dry', 'Do not save user settings back to database' );
46 $this->addArg( 'option name', 'Name of the option to change or provide statistics about', false );
49 /**
50 * Do the actual work
52 public function execute() {
53 if ( $this->hasOption( 'list' ) ) {
54 $this->listAvailableOptions();
55 } elseif ( $this->hasOption( 'usage' ) ) {
56 $this->showUsageStats();
57 } elseif ( $this->hasOption( 'old' )
58 && $this->hasOption( 'new' )
59 && $this->hasArg( 0 )
60 ) {
61 $this->updateOptions();
62 } else {
63 $this->maybeHelp( true );
67 /**
68 * List default options and their value
70 private function listAvailableOptions() {
71 $def = User::getDefaultOptions();
72 ksort( $def );
73 $maxOpt = 0;
74 foreach ( $def as $opt => $value ) {
75 $maxOpt = max( $maxOpt, strlen( $opt ) );
77 foreach ( $def as $opt => $value ) {
78 $this->output( sprintf( "%-{$maxOpt}s: %s\n", $opt, $value ) );
82 /**
83 * List options usage
85 private function showUsageStats() {
86 $option = $this->getArg( 0 );
88 $ret = [];
89 $defaultOptions = User::getDefaultOptions();
91 // We list user by user_id from one of the replica DBs
92 $dbr = wfGetDB( DB_REPLICA );
93 $result = $dbr->select( 'user',
94 [ 'user_id' ],
95 [],
96 __METHOD__
99 foreach ( $result as $id ) {
100 $user = User::newFromId( $id->user_id );
102 // Get the options and update stats
103 if ( $option ) {
104 if ( !array_key_exists( $option, $defaultOptions ) ) {
105 $this->fatalError( "Invalid user option. Use --list to see valid choices\n" );
108 $userValue = $user->getOption( $option );
109 if ( $userValue <> $defaultOptions[$option] ) {
110 $ret[$option][$userValue] = ( $ret[$option][$userValue] ?? 0 ) + 1;
112 } else {
113 foreach ( $defaultOptions as $name => $defaultValue ) {
114 $userValue = $user->getOption( $name );
115 if ( $userValue != $defaultValue ) {
116 $ret[$name][$userValue] = ( $ret[$name][$userValue] ?? 0 ) + 1;
122 foreach ( $ret as $optionName => $usageStats ) {
123 $this->output( "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n" );
124 foreach ( $usageStats as $value => $count ) {
125 $this->output( " $count user(s): '$value'\n" );
127 print "\n";
132 * Change our users options
134 private function updateOptions() {
135 $dryRun = $this->hasOption( 'dry' );
136 $option = $this->getArg( 0 );
137 $from = $this->getOption( 'old' );
138 $to = $this->getOption( 'new' );
140 if ( !$dryRun ) {
141 $this->warn( $option, $from, $to );
144 // We list user by user_id from one of the replica DBs
145 // @todo: getting all users in one query does not scale
146 $dbr = wfGetDB( DB_REPLICA );
147 $result = $dbr->select( 'user',
148 [ 'user_id' ],
150 __METHOD__
153 foreach ( $result as $id ) {
154 $user = User::newFromId( $id->user_id );
156 $curValue = $user->getOption( $option );
157 $username = $user->getName();
159 if ( $curValue == $from ) {
160 $this->output( "Setting {$option} for $username from '{$from}' to '{$to}'): " );
162 // Change value
163 $user->setOption( $option, $to );
165 // Will not save the settings if run with --dry
166 if ( !$dryRun ) {
167 $user->saveSettings();
169 $this->output( " OK\n" );
170 } else {
171 $this->output( "Not changing '$username' using <{$option}> = '$curValue'\n" );
177 * The warning message and countdown
179 * @param string $option
180 * @param string $from
181 * @param string $to
183 private function warn( $option, $from, $to ) {
184 if ( $this->hasOption( 'nowarn' ) ) {
185 return;
188 $this->output( <<<WARN
189 The script is about to change the options for ALL USERS in the database.
190 Users with option <$option> = '$from' will be made to use '$to'.
192 Abort with control-c in the next five seconds....
193 WARN
195 $this->countDown( 5 );
199 $maintClass = UserOptionsMaintenance::class;
200 require RUN_MAINTENANCE_IF_MAIN;