* Use ui language now that it is possible
[mediawiki.git] / maintenance / userOptions.inc
blob702ab715818a80ecfaa457a021250bbc1f677723
1 <?php
2 // Options we will use
3 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
4 $optionsWithArgs = array( 'old', 'new' );
6 require_once( 'commandLine.inc' );
8 class userOptions {
9         public $mQuick;
10         public $mQuiet;
11         public $mDry;
12         public $mAnOption;
13         public $mOldValue;
14         public $mNewValue;
16         private $mMode, $mReady ;
18         /** Constructor. Will show usage and exit if script options are not correct */
19         function __construct( $opts, $args ) {
20                 if( !$this->checkOpts( $opts, $args ) ) {
21                         userOptions::showUsageAndExit();
22                 } else {
23                         $this->mReady = $this->initializeOpts( $opts, $args );
24                 }
25         }
28         /** This is used to check options. Only needed on construction */
29         private function checkOpts( $opts, $args ) {
30                 // The three possible ways to run the script:
31                 $list   = isset( $opts['list'] );
32                 $usage  = isset( $opts['usage'] ) && (count($args) <= 1);
33                 $change = isset( $opts['old']) && isset($opts['new']) && (count($args) <= 1) ;
35                 // We want only one of them
36                 $isValid = (($list + $usage + $change) == 1);
38                 return $isValid;
39         }
41         /** load script options in the object */
42         private function initializeOpts( $opts, $args ) {
44                 $this->mQuick = isset( $opts['nowarn'] );
45                 $this->mQuiet = isset( $opts['quiet'] );
46                 $this->mDry   = isset( $opts['dry'] );
48                 // Set object properties, specially 'mMode' used by run()
49                 if( isset($opts['list']) ) {
50                         $this->mMode = 'LISTER' ;
51                 } elseif( isset($opts['usage']) ) {
52                         $this->mMode = 'USAGER' ;
53                         $this->mAnOption = isset($args[0]) ? $args[0] : false ;
54                 } elseif( isset($opts['old']) && isset($opts['new']) ) {
55                         $this->mMode = 'CHANGER' ;
56                         $this->mOldValue = $opts['old'] ;
57                         $this->mNewValue = $opts['new'] ;
58                         $this->mAnOption = $args[0];
59                 } else {
60                         die("There is a bug in the software, this should never happen\n");
61                 }
63                 return true;
64         }
66         // Dumb stuff to run a mode.
67         public function run() {
68                 if(!$this->mReady ) {
69                         return false;
70                 }
72                 $this->{$this->mMode}( );
74         }
76         #
77         # Modes.
78         # 
80         /** List default options and their value */
81         private function LISTER( ) {
82                 $def = User::getDefaultOptions();
83                 ksort($def);
84                 $maxOpt = 0;
85                 foreach( $def as $opt => $value ) {
86                         $maxOpt = max( $maxOpt, strlen($opt) );
87                 }
88                 foreach( $def as $opt => $value ) {
89                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
90                 }
91         }
93         /** List options usage */
94         private function USAGER( ) {
95                 $ret = array();
96                 $defaultOptions = User::getDefaultOptions();
98                 // We list user by user_id from one of the slave database
99                 $dbr = wfGetDB( DB_SLAVE );
100                 $result = $dbr->select( 'user',
101                         array( 'user_id' ),
102                         array(),
103                         __METHOD__
104                         );
106                 while( $id = $dbr->fetchObject( $result ) ) {
108                         $user = User::newFromId( $id->user_id );
110                         // Get the options and update stats
111                         foreach( $defaultOptions as $name => $defaultValue ) {
112                                 $userValue = $user->getOption( $name );
113                                 if( $userValue <> $defaultValue ) {
114                                         @$ret[$name][$userValue]++;
115                                 }
116                         }
117                 }
119                 foreach( $ret as $optionName => $usageStats ) {
120                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
121                         foreach( $usageStats as $value => $count ) {
122                                 print " $count user(s): '$value'\n";
123                         }
124                         print "\n";
125                 }
126         }
129         /** Change our users options */
130         private function CHANGER( ) {
131                 $this->warn();
133                 // We list user by user_id from one of the slave database
134                 $dbr = wfGetDB( DB_SLAVE );
135                 $result = $dbr->select( 'user',
136                         array( 'user_id' ),
137                         array(),
138                         __METHOD__
139                         );
141                 while( $id = $dbr->fetchObject( $result ) ) {
143                         $user = User::newFromId( $id->user_id );
145                         $curValue = $user->getOption( $this->mAnOption );
146                         $username = $user->getName();
148                         if( $curValue == $this->mOldValue ) {
150                                 if(!$this->mQuiet) {
151                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
152                                 }
154                                 // Change value 
155                                 $user->setOption( $this->mAnOption, $this->mNewValue );
157                                 // Will not save the settings if run with --dry
158                                 if(!$this->mDry) {
159                                         $user->saveSettings();
160                                 }
161                                 if( !$this->mQuiet) { print " OK\n"; }
163                         } elseif( !$this->mQuiet ) {
164                                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
165                         }
166                 }
167         }
170         /** Return an array of option names */
171         public static function getDefaultOptionsNames() {
172                 $def = User::getDefaultOptions();
173                 $ret = array();
174                 foreach( $def as $optname => $defaultValue) {
175                         array_push( $ret, $optname );
176                 }
177                 return $ret;
178         }
181         #
182         # Helper methods
183         #
185         public static function showUsageAndExit() {
186 print <<<USAGE
188 This script pass through all users and change one of their options.
189 The new option is NOT validated.
191 Usage:
192     php userOptions.php --list
193     php userOptions.php <user option> --usage
194     php userOptions.php [options] <user option> --old <old value> --new <new value>
196 Switchs:
197     --list : list available user options and their default value
199     --usage <option name> : report statistics about an option
201     --old <old value> : the value to look for
202     --new <new value> : new value to update users with
204 Options:
205     --nowarn: hides the 5 seconds warning
206     --quiet : do not print what is happening
207     --dry   : do not save user settings back to database
209 USAGE;
210     exit(0);
211         }
213         /** The warning message and countdown */
214         public function warn() {
216                 if( $this->mQuick ) {
217                         return true;
218                 }
220 print <<<WARN
221 The script is about to change the skin for ALL USERS in the database.
222 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
224 Abort with control-c in the next five seconds....
225 WARN;
226                 require('counter.php');
227                 for ($i=6;$i>=1;) {
228                         print_c($i, --$i);
229                         sleep(1);
230                 }
231                 print "\n";
233                 return true;
234         }