Require PHP >= 5.3.3
[mediawiki.git] / maintenance / userOptions.inc
blob99ba3b8329ca6c7773bbfa0abb934911e8f665e4
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 __DIR__ . '/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         /**
44          * Constructor. Will show usage and exit if script options are not correct
45          * @param array $opts
46          * @param array $args
47          */
48         function __construct( $opts, $args ) {
49                 if ( !$this->checkOpts( $opts, $args ) ) {
50                         UserOptions::showUsageAndExit();
51                 } else {
52                         $this->mReady = $this->initializeOpts( $opts, $args );
53                 }
54         }
56         /**
57          * This is used to check options. Only needed on construction
58          *
59          * @param array $opts
60          * @param array $args
61          *
62          * @return bool
63          */
64         private function checkOpts( $opts, $args ) {
65                 // The three possible ways to run the script:
66                 $list = isset( $opts['list'] );
67                 $usage = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
68                 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 );
70                 // We want only one of them
71                 $isValid = ( ( $list + $usage + $change ) == 1 );
73                 return $isValid;
74         }
76         /**
77          * load script options in the object
78          *
79          * @param array $opts
80          * @param array $args
81          *
82          * @return bool
83          */
84         private function initializeOpts( $opts, $args ) {
86                 $this->mQuick = isset( $opts['nowarn'] );
87                 $this->mQuiet = isset( $opts['quiet'] );
88                 $this->mDry = isset( $opts['dry'] );
90                 // Set object properties, specially 'mMode' used by run()
91                 if ( isset( $opts['list'] ) ) {
92                         $this->mMode = 'LISTER';
93                 } elseif ( isset( $opts['usage'] ) ) {
94                         $this->mMode = 'USAGER';
95                         $this->mAnOption = isset( $args[0] ) ? $args[0] : false;
96                 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
97                         $this->mMode = 'CHANGER';
98                         $this->mOldValue = $opts['old'];
99                         $this->mNewValue = $opts['new'];
100                         $this->mAnOption = $args[0];
101                 } else {
102                         die( "There is a bug in the software, this should never happen\n" );
103                 }
105                 return true;
106         }
108         // Dumb stuff to run a mode.
109         public function run() {
110                 if ( !$this->mReady ) {
111                         return false;
112                 }
114                 $this->{$this->mMode}();
116                 return true;
117         }
119         #
120         # Modes.
121         #
123         /** List default options and their value */
124         private function LISTER() {
125                 $def = User::getDefaultOptions();
126                 ksort( $def );
127                 $maxOpt = 0;
128                 foreach ( $def as $opt => $value ) {
129                         $maxOpt = max( $maxOpt, strlen( $opt ) );
130                 }
131                 foreach ( $def as $opt => $value ) {
132                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
133                 }
134         }
136         /** List options usage */
137         private function USAGER() {
138                 $ret = array();
139                 $defaultOptions = User::getDefaultOptions();
141                 // We list user by user_id from one of the slave database
142                 $dbr = wfGetDB( DB_SLAVE );
143                 $result = $dbr->select( 'user',
144                         array( 'user_id' ),
145                         array(),
146                         __METHOD__
147                 );
149                 foreach ( $result as $id ) {
151                         $user = User::newFromId( $id->user_id );
153                         // Get the options and update stats
154                         if ( $this->mAnOption ) {
156                                 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
157                                         print "Invalid user option. Use --list to see valid choices\n";
158                                         exit;
159                                 }
161                                 $userValue = $user->getOption( $this->mAnOption );
162                                 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
163                                         // @codingStandardsIgnoreStart Ignore silencing errors is discouraged warning
164                                         @$ret[$this->mAnOption][$userValue]++;
165                                         // @codingStandardsIgnoreEnd
166                                 }
167                         } else {
169                                 foreach ( $defaultOptions as $name => $defaultValue ) {
170                                         $userValue = $user->getOption( $name );
171                                         if ( $userValue <> $defaultValue ) {
172                                                 // @codingStandardsIgnoreStart Ignore silencing errors is discouraged warning
173                                                 @$ret[$name][$userValue]++;
174                                                 // @codingStandardsIgnoreEnd
175                                         }
176                                 }
177                         }
178                 }
180                 foreach ( $ret as $optionName => $usageStats ) {
181                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
182                         foreach ( $usageStats as $value => $count ) {
183                                 print " $count user(s): '$value'\n";
184                         }
185                         print "\n";
186                 }
187         }
189         /** Change our users options */
190         private function CHANGER() {
191                 $this->warn();
193                 // We list user by user_id from one of the slave database
194                 $dbr = wfGetDB( DB_SLAVE );
195                 $result = $dbr->select( 'user',
196                         array( 'user_id' ),
197                         array(),
198                         __METHOD__
199                 );
201                 foreach ( $result as $id ) {
203                         $user = User::newFromId( $id->user_id );
205                         $curValue = $user->getOption( $this->mAnOption );
206                         $username = $user->getName();
208                         if ( $curValue == $this->mOldValue ) {
210                                 if ( !$this->mQuiet ) {
211                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' " .
212                                                 "to '{$this->mNewValue}'): ";
213                                 }
215                                 // Change value
216                                 $user->setOption( $this->mAnOption, $this->mNewValue );
218                                 // Will not save the settings if run with --dry
219                                 if ( !$this->mDry ) {
220                                         $user->saveSettings();
221                                 }
222                                 if ( !$this->mQuiet ) {
223                                         print " OK\n";
224                                 }
225                         } elseif ( !$this->mQuiet ) {
226                                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
227                         }
228                 }
229         }
231         /**
232          * Return an array of option names
233          * @return array
234          */
235         public static function getDefaultOptionsNames() {
236                 $def = User::getDefaultOptions();
237                 $ret = array();
238                 foreach ( $def as $optname => $defaultValue ) {
239                         array_push( $ret, $optname );
240                 }
242                 return $ret;
243         }
245         #
246         # Helper methods
247         #
249         public static function showUsageAndExit() {
250                 print <<<USAGE
252 This script pass through all users and change one of their options.
253 The new option is NOT validated.
255 Usage:
256         php userOptions.php --list
257         php userOptions.php [user option] --usage
258         php userOptions.php [options] <user option> --old <old value> --new <new value>
260 Switchs:
261         --list : list available user options and their default value
263         --usage : report all options statistics or just one if you specify it.
265         --old <old value> : the value to look for
266         --new <new value> : new value to update users with
268 Options:
269         --nowarn: hides the 5 seconds warning
270         --quiet : do not print what is happening
271         --dry   : do not save user settings back to database
273 USAGE;
274                 exit( 0 );
275         }
277         /**
278          * The warning message and countdown
279          * @return bool
280          */
281         public function warn() {
283                 if ( $this->mQuick ) {
284                         return true;
285                 }
287                 print <<<WARN
288 The script is about to change the skin for ALL USERS in the database.
289 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
291 Abort with control-c in the next five seconds....
292 WARN;
293                 wfCountDown( 5 );
295                 return true;
296         }