Of course didn't mean to commit var_dump
[mediawiki.git] / maintenance / userOptions.inc
blobbfc8e8e67ef18b61cf5bddd7afe9f5fa810a81e3
1 <?php
2 /**
3  * @file
4  * @ingroup Maintenance
5  */
7 // Options we will use
8 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
9 $optionsWithArgs = array( 'old', 'new' );
11 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
13 /**
14  * @ingroup Maintenance
15  */
16 class userOptions {
17         public $mQuick;
18         public $mQuiet;
19         public $mDry;
20         public $mAnOption;
21         public $mOldValue;
22         public $mNewValue;
24         private $mMode, $mReady ;
26         /** Constructor. Will show usage and exit if script options are not correct */
27         function __construct( $opts, $args ) {
28                 if ( !$this->checkOpts( $opts, $args ) ) {
29                         userOptions::showUsageAndExit();
30                 } else {
31                         $this->mReady = $this->initializeOpts( $opts, $args );
32                 }
33         }
36         /** This is used to check options. Only needed on construction */
37         private function checkOpts( $opts, $args ) {
38                 // The three possible ways to run the script:
39                 $list   = isset( $opts['list'] );
40                 $usage  = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
41                 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 ) ;
43                 // We want only one of them
44                 $isValid = ( ( $list + $usage + $change ) == 1 );
46                 return $isValid;
47         }
49         /** load script options in the object */
50         private function initializeOpts( $opts, $args ) {
52                 $this->mQuick = isset( $opts['nowarn'] );
53                 $this->mQuiet = isset( $opts['quiet'] );
54                 $this->mDry   = isset( $opts['dry'] );
56                 // Set object properties, specially 'mMode' used by run()
57                 if ( isset( $opts['list'] ) ) {
58                         $this->mMode = 'LISTER' ;
59                 } elseif ( isset( $opts['usage'] ) ) {
60                         $this->mMode = 'USAGER' ;
61                         $this->mAnOption = isset( $args[0] ) ? $args[0] : false ;
62                 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
63                         $this->mMode = 'CHANGER' ;
64                         $this->mOldValue = $opts['old'] ;
65                         $this->mNewValue = $opts['new'] ;
66                         $this->mAnOption = $args[0];
67                 } else {
68                         die( "There is a bug in the software, this should never happen\n" );
69                 }
71                 return true;
72         }
74         // Dumb stuff to run a mode.
75         public function run() {
76                 if ( !$this->mReady ) {
77                         return false;
78                 }
80                 $this-> { $this->mMode } ( );
82         }
84         #
85         # Modes.
86         # 
88         /** List default options and their value */
89         private function LISTER( ) {
90                 $def = User::getDefaultOptions();
91                 ksort( $def );
92                 $maxOpt = 0;
93                 foreach ( $def as $opt => $value ) {
94                         $maxOpt = max( $maxOpt, strlen( $opt ) );
95                 }
96                 foreach ( $def as $opt => $value ) {
97                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
98                 }
99         }
101         /** List options usage */
102         private function USAGER( ) {
103                 $ret = array();
104                 $defaultOptions = User::getDefaultOptions();
106                 // We list user by user_id from one of the slave database
107                 $dbr = wfGetDB( DB_SLAVE );
108                 $result = $dbr->select( 'user',
109                         array( 'user_id' ),
110                         array(),
111                         __METHOD__
112                         );
114                 while ( $id = $dbr->fetchObject( $result ) ) {
116                         $user = User::newFromId( $id->user_id );
118                         // Get the options and update stats
119                         if ( $this->mAnOption ) {
121                                 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
122                                         print "Invalid user option. Use --list to see valid choices\n";
123                                         exit;
124                                 }
126                                 $userValue = $user->getOption( $this->mAnOption );
127                                 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
128                                         @$ret[$this->mAnOption][$userValue]++;
129                                 }
131                         } else {
133                                 foreach ( $defaultOptions as $name => $defaultValue ) {
134                                         $userValue = $user->getOption( $name );
135                                         if ( $userValue <> $defaultValue ) {
136                                                 @$ret[$name][$userValue]++;
137                                         }
138                                 }
139                         }
140                 }
142                 foreach ( $ret as $optionName => $usageStats ) {
143                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
144                         foreach ( $usageStats as $value => $count ) {
145                                 print " $count user(s): '$value'\n";
146                         }
147                         print "\n";
148                 }
149         }
152         /** Change our users options */
153         private function CHANGER( ) {
154                 $this->warn();
156                 // We list user by user_id from one of the slave database
157                 $dbr = wfGetDB( DB_SLAVE );
158                 $result = $dbr->select( 'user',
159                         array( 'user_id' ),
160                         array(),
161                         __METHOD__
162                         );
164                 while ( $id = $dbr->fetchObject( $result ) ) {
166                         $user = User::newFromId( $id->user_id );
168                         $curValue = $user->getOption( $this->mAnOption );
169                         $username = $user->getName();
171                         if ( $curValue == $this->mOldValue ) {
173                                 if ( !$this->mQuiet ) {
174                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
175                                 }
177                                 // Change value 
178                                 $user->setOption( $this->mAnOption, $this->mNewValue );
180                                 // Will not save the settings if run with --dry
181                                 if ( !$this->mDry ) {
182                                         $user->saveSettings();
183                                 }
184                                 if ( !$this->mQuiet ) { print " OK\n"; }
186                         } elseif ( !$this->mQuiet ) {
187                                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
188                         }
189                 }
190         }
193         /** Return an array of option names */
194         public static function getDefaultOptionsNames() {
195                 $def = User::getDefaultOptions();
196                 $ret = array();
197                 foreach ( $def as $optname => $defaultValue ) {
198                         array_push( $ret, $optname );
199                 }
200                 return $ret;
201         }
204         #
205         # Helper methods
206         #
208         public static function showUsageAndExit() {
209 print <<<USAGE
211 This script pass through all users and change one of their options.
212 The new option is NOT validated.
214 Usage:
215     php userOptions.php --list
216     php userOptions.php [user option] --usage
217     php userOptions.php [options] <user option> --old <old value> --new <new value>
219 Switchs:
220     --list : list available user options and their default value
222     --usage : report all options statistics or just one if you specify it.
224     --old <old value> : the value to look for
225     --new <new value> : new value to update users with
227 Options:
228     --nowarn: hides the 5 seconds warning
229     --quiet : do not print what is happening
230     --dry   : do not save user settings back to database
232 USAGE;
233     exit( 0 );
234         }
236         /** The warning message and countdown */
237         public function warn() {
239                 if ( $this->mQuick ) {
240                         return true;
241                 }
243 print <<<WARN
244 The script is about to change the skin for ALL USERS in the database.
245 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
247 Abort with control-c in the next five seconds....
248 WARN;
249                 wfCountDown( 5 );
250                 return true;
251         }