assertType() is deprecated, switching usages tp assertInternalType() and assertInstan...
[mediawiki.git] / maintenance / userOptions.inc
blob6edef9d9eec72c16c2d88d8abee17366fb2283a0
1 <?php
2 /**
3  * Helper class for userOptions.php script.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @ingroup Maintenance
22  */
24 // Options we will use
25 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
26 $optionsWithArgs = array( 'old', 'new' );
28 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
30 /**
31  * @ingroup Maintenance
32  */
33 class userOptions {
34         public $mQuick;
35         public $mQuiet;
36         public $mDry;
37         public $mAnOption;
38         public $mOldValue;
39         public $mNewValue;
41         private $mMode, $mReady ;
43         /** Constructor. Will show usage and exit if script options are not correct */
44         function __construct( $opts, $args ) {
45                 if ( !$this->checkOpts( $opts, $args ) ) {
46                         userOptions::showUsageAndExit();
47                 } else {
48                         $this->mReady = $this->initializeOpts( $opts, $args );
49                 }
50         }
53         /** This is used to check options. Only needed on construction */
54         private function checkOpts( $opts, $args ) {
55                 // The three possible ways to run the script:
56                 $list   = isset( $opts['list'] );
57                 $usage  = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
58                 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 ) ;
60                 // We want only one of them
61                 $isValid = ( ( $list + $usage + $change ) == 1 );
63                 return $isValid;
64         }
66         /** load script options in the object */
67         private function initializeOpts( $opts, $args ) {
69                 $this->mQuick = isset( $opts['nowarn'] );
70                 $this->mQuiet = isset( $opts['quiet'] );
71                 $this->mDry   = isset( $opts['dry'] );
73                 // Set object properties, specially 'mMode' used by run()
74                 if ( isset( $opts['list'] ) ) {
75                         $this->mMode = 'LISTER' ;
76                 } elseif ( isset( $opts['usage'] ) ) {
77                         $this->mMode = 'USAGER' ;
78                         $this->mAnOption = isset( $args[0] ) ? $args[0] : false ;
79                 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
80                         $this->mMode = 'CHANGER' ;
81                         $this->mOldValue = $opts['old'] ;
82                         $this->mNewValue = $opts['new'] ;
83                         $this->mAnOption = $args[0];
84                 } else {
85                         die( "There is a bug in the software, this should never happen\n" );
86                 }
88                 return true;
89         }
91         // Dumb stuff to run a mode.
92         public function run() {
93                 if ( !$this->mReady ) {
94                         return false;
95                 }
97                 $this-> { $this->mMode } ( );
99         }
101         #
102         # Modes.
103         #
105         /** List default options and their value */
106         private function LISTER( ) {
107                 $def = User::getDefaultOptions();
108                 ksort( $def );
109                 $maxOpt = 0;
110                 foreach ( $def as $opt => $value ) {
111                         $maxOpt = max( $maxOpt, strlen( $opt ) );
112                 }
113                 foreach ( $def as $opt => $value ) {
114                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
115                 }
116         }
118         /** List options usage */
119         private function USAGER( ) {
120                 $ret = array();
121                 $defaultOptions = User::getDefaultOptions();
123                 // We list user by user_id from one of the slave database
124                 $dbr = wfGetDB( DB_SLAVE );
125                 $result = $dbr->select( 'user',
126                         array( 'user_id' ),
127                         array(),
128                         __METHOD__
129                         );
131                 foreach ( $result as $id ) {
133                         $user = User::newFromId( $id->user_id );
135                         // Get the options and update stats
136                         if ( $this->mAnOption ) {
138                                 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
139                                         print "Invalid user option. Use --list to see valid choices\n";
140                                         exit;
141                                 }
143                                 $userValue = $user->getOption( $this->mAnOption );
144                                 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
145                                         @$ret[$this->mAnOption][$userValue]++;
146                                 }
148                         } else {
150                                 foreach ( $defaultOptions as $name => $defaultValue ) {
151                                         $userValue = $user->getOption( $name );
152                                         if ( $userValue <> $defaultValue ) {
153                                                 @$ret[$name][$userValue]++;
154                                         }
155                                 }
156                         }
157                 }
159                 foreach ( $ret as $optionName => $usageStats ) {
160                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
161                         foreach ( $usageStats as $value => $count ) {
162                                 print " $count user(s): '$value'\n";
163                         }
164                         print "\n";
165                 }
166         }
169         /** Change our users options */
170         private function CHANGER( ) {
171                 $this->warn();
173                 // We list user by user_id from one of the slave database
174                 $dbr = wfGetDB( DB_SLAVE );
175                 $result = $dbr->select( 'user',
176                         array( 'user_id' ),
177                         array(),
178                         __METHOD__
179                         );
181                 foreach ( $result as $id ) {
183                         $user = User::newFromId( $id->user_id );
185                         $curValue = $user->getOption( $this->mAnOption );
186                         $username = $user->getName();
188                         if ( $curValue == $this->mOldValue ) {
190                                 if ( !$this->mQuiet ) {
191                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
192                                 }
194                                 // Change value
195                                 $user->setOption( $this->mAnOption, $this->mNewValue );
197                                 // Will not save the settings if run with --dry
198                                 if ( !$this->mDry ) {
199                                         $user->saveSettings();
200                                 }
201                                 if ( !$this->mQuiet ) { print " OK\n"; }
203                         } elseif ( !$this->mQuiet ) {
204                                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
205                         }
206                 }
207         }
210         /** Return an array of option names */
211         public static function getDefaultOptionsNames() {
212                 $def = User::getDefaultOptions();
213                 $ret = array();
214                 foreach ( $def as $optname => $defaultValue ) {
215                         array_push( $ret, $optname );
216                 }
217                 return $ret;
218         }
221         #
222         # Helper methods
223         #
225         public static function showUsageAndExit() {
226 print <<<USAGE
228 This script pass through all users and change one of their options.
229 The new option is NOT validated.
231 Usage:
232         php userOptions.php --list
233         php userOptions.php [user option] --usage
234         php userOptions.php [options] <user option> --old <old value> --new <new value>
236 Switchs:
237         --list : list available user options and their default value
239         --usage : report all options statistics or just one if you specify it.
241         --old <old value> : the value to look for
242         --new <new value> : new value to update users with
244 Options:
245         --nowarn: hides the 5 seconds warning
246         --quiet : do not print what is happening
247         --dry   : do not save user settings back to database
249 USAGE;
250         exit( 0 );
251         }
253         /** The warning message and countdown */
254         public function warn() {
256                 if ( $this->mQuick ) {
257                         return true;
258                 }
260 print <<<WARN
261 The script is about to change the skin for ALL USERS in the database.
262 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
264 Abort with control-c in the next five seconds....
265 WARN;
266                 wfCountDown( 5 );
267                 return true;
268         }