Prevent space before ellipsis when truncating
[mediawiki.git] / maintenance / userOptions.inc
blob51da80d34409fb22be15937ef26885d6cdf0d4ab
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         /** 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         /**
54          * This is used to check options. Only needed on construction
55          *
56          * @param $opts array
57          * @param $args array
58          *
59          * @return bool
60          */
61         private function checkOpts( $opts, $args ) {
62                 // The three possible ways to run the script:
63                 $list = isset( $opts['list'] );
64                 $usage = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
65                 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 );
67                 // We want only one of them
68                 $isValid = ( ( $list + $usage + $change ) == 1 );
70                 return $isValid;
71         }
73         /**
74          * load script options in the object
75          *
76          * @param $opts array
77          * @param $args array
78          *
79          * @return bool
80          */
81         private function initializeOpts( $opts, $args ) {
83                 $this->mQuick = isset( $opts['nowarn'] );
84                 $this->mQuiet = isset( $opts['quiet'] );
85                 $this->mDry = isset( $opts['dry'] );
87                 // Set object properties, specially 'mMode' used by run()
88                 if ( isset( $opts['list'] ) ) {
89                         $this->mMode = 'LISTER';
90                 } elseif ( isset( $opts['usage'] ) ) {
91                         $this->mMode = 'USAGER';
92                         $this->mAnOption = isset( $args[0] ) ? $args[0] : false;
93                 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
94                         $this->mMode = 'CHANGER';
95                         $this->mOldValue = $opts['old'];
96                         $this->mNewValue = $opts['new'];
97                         $this->mAnOption = $args[0];
98                 } else {
99                         die( "There is a bug in the software, this should never happen\n" );
100                 }
102                 return true;
103         }
105         // Dumb stuff to run a mode.
106         public function run() {
107                 if ( !$this->mReady ) {
108                         return false;
109                 }
111                 $this->{ $this->mMode } ();
112                 return true;
113         }
115         #
116         # Modes.
117         #
119         /** List default options and their value */
120         private function LISTER() {
121                 $def = User::getDefaultOptions();
122                 ksort( $def );
123                 $maxOpt = 0;
124                 foreach ( $def as $opt => $value ) {
125                         $maxOpt = max( $maxOpt, strlen( $opt ) );
126                 }
127                 foreach ( $def as $opt => $value ) {
128                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
129                 }
130         }
132         /** List options usage */
133         private function USAGER() {
134                 $ret = array();
135                 $defaultOptions = User::getDefaultOptions();
137                 // We list user by user_id from one of the slave database
138                 $dbr = wfGetDB( DB_SLAVE );
139                 $result = $dbr->select( 'user',
140                         array( 'user_id' ),
141                         array(),
142                         __METHOD__
143                         );
145                 foreach ( $result as $id ) {
147                         $user = User::newFromId( $id->user_id );
149                         // Get the options and update stats
150                         if ( $this->mAnOption ) {
152                                 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
153                                         print "Invalid user option. Use --list to see valid choices\n";
154                                         exit;
155                                 }
157                                 $userValue = $user->getOption( $this->mAnOption );
158                                 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
159                                         @$ret[$this->mAnOption][$userValue]++;
160                                 }
162                         } else {
164                                 foreach ( $defaultOptions as $name => $defaultValue ) {
165                                         $userValue = $user->getOption( $name );
166                                         if ( $userValue <> $defaultValue ) {
167                                                 @$ret[$name][$userValue]++;
168                                         }
169                                 }
170                         }
171                 }
173                 foreach ( $ret as $optionName => $usageStats ) {
174                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
175                         foreach ( $usageStats as $value => $count ) {
176                                 print " $count user(s): '$value'\n";
177                         }
178                         print "\n";
179                 }
180         }
183         /** Change our users options */
184         private function CHANGER() {
185                 $this->warn();
187                 // We list user by user_id from one of the slave database
188                 $dbr = wfGetDB( DB_SLAVE );
189                 $result = $dbr->select( 'user',
190                         array( 'user_id' ),
191                         array(),
192                         __METHOD__
193                         );
195                 foreach ( $result as $id ) {
197                         $user = User::newFromId( $id->user_id );
199                         $curValue = $user->getOption( $this->mAnOption );
200                         $username = $user->getName();
202                         if ( $curValue == $this->mOldValue ) {
204                                 if ( !$this->mQuiet ) {
205                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
206                                 }
208                                 // Change value
209                                 $user->setOption( $this->mAnOption, $this->mNewValue );
211                                 // Will not save the settings if run with --dry
212                                 if ( !$this->mDry ) {
213                                         $user->saveSettings();
214                                 }
215                                 if ( !$this->mQuiet ) {
216                                         print " OK\n";
217                                 }
219                         } elseif ( !$this->mQuiet ) {
220                                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
221                         }
222                 }
223         }
225         /**
226          * Return an array of option names
227          * @return array
228          */
229         public static function getDefaultOptionsNames() {
230                 $def = User::getDefaultOptions();
231                 $ret = array();
232                 foreach ( $def as $optname => $defaultValue ) {
233                         array_push( $ret, $optname );
234                 }
235                 return $ret;
236         }
238         #
239         # Helper methods
240         #
242         public static function showUsageAndExit() {
243 print <<<USAGE
245 This script pass through all users and change one of their options.
246 The new option is NOT validated.
248 Usage:
249         php userOptions.php --list
250         php userOptions.php [user option] --usage
251         php userOptions.php [options] <user option> --old <old value> --new <new value>
253 Switchs:
254         --list : list available user options and their default value
256         --usage : report all options statistics or just one if you specify it.
258         --old <old value> : the value to look for
259         --new <new value> : new value to update users with
261 Options:
262         --nowarn: hides the 5 seconds warning
263         --quiet : do not print what is happening
264         --dry   : do not save user settings back to database
266 USAGE;
267         exit( 0 );
268         }
270         /**
271          * The warning message and countdown
272          * @return bool
273          */
274         public function warn() {
276                 if ( $this->mQuick ) {
277                         return true;
278                 }
280 print <<<WARN
281 The script is about to change the skin for ALL USERS in the database.
282 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
284 Abort with control-c in the next five seconds....
285 WARN;
286                 wfCountDown( 5 );
287                 return true;
288         }