r89329 followup: roa-rup -> rup in addwiki.php's initial main page text.
[lhc/web/wiklou.git] / maintenance / userOptions.inc
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 */
23
24 // Options we will use
25 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
26 $optionsWithArgs = array( 'old', 'new' );
27
28 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
29
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;
40
41 private $mMode, $mReady ;
42
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 }
51
52
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 ) ;
59
60 // We want only one of them
61 $isValid = ( ( $list + $usage + $change ) == 1 );
62
63 return $isValid;
64 }
65
66 /** load script options in the object */
67 private function initializeOpts( $opts, $args ) {
68
69 $this->mQuick = isset( $opts['nowarn'] );
70 $this->mQuiet = isset( $opts['quiet'] );
71 $this->mDry = isset( $opts['dry'] );
72
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 }
87
88 return true;
89 }
90
91 // Dumb stuff to run a mode.
92 public function run() {
93 if ( !$this->mReady ) {
94 return false;
95 }
96
97 $this-> { $this->mMode } ( );
98
99 }
100
101 #
102 # Modes.
103 #
104
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 }
117
118 /** List options usage */
119 private function USAGER( ) {
120 $ret = array();
121 $defaultOptions = User::getDefaultOptions();
122
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 );
130
131 foreach ( $result as $id ) {
132
133 $user = User::newFromId( $id->user_id );
134
135 // Get the options and update stats
136 if ( $this->mAnOption ) {
137
138 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
139 print "Invalid user option. Use --list to see valid choices\n";
140 exit;
141 }
142
143 $userValue = $user->getOption( $this->mAnOption );
144 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
145 @$ret[$this->mAnOption][$userValue]++;
146 }
147
148 } else {
149
150 foreach ( $defaultOptions as $name => $defaultValue ) {
151 $userValue = $user->getOption( $name );
152 if ( $userValue <> $defaultValue ) {
153 @$ret[$name][$userValue]++;
154 }
155 }
156 }
157 }
158
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 }
167
168
169 /** Change our users options */
170 private function CHANGER( ) {
171 $this->warn();
172
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 );
180
181 foreach ( $result as $id ) {
182
183 $user = User::newFromId( $id->user_id );
184
185 $curValue = $user->getOption( $this->mAnOption );
186 $username = $user->getName();
187
188 if ( $curValue == $this->mOldValue ) {
189
190 if ( !$this->mQuiet ) {
191 print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
192 }
193
194 // Change value
195 $user->setOption( $this->mAnOption, $this->mNewValue );
196
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"; }
202
203 } elseif ( !$this->mQuiet ) {
204 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
205 }
206 }
207 }
208
209
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 }
219
220
221 #
222 # Helper methods
223 #
224
225 public static function showUsageAndExit() {
226 print <<<USAGE
227
228 This script pass through all users and change one of their options.
229 The new option is NOT validated.
230
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>
235
236 Switchs:
237 --list : list available user options and their default value
238
239 --usage : report all options statistics or just one if you specify it.
240
241 --old <old value> : the value to look for
242 --new <new value> : new value to update users with
243
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
248
249 USAGE;
250 exit( 0 );
251 }
252
253 /** The warning message and countdown */
254 public function warn() {
255
256 if ( $this->mQuick ) {
257 return true;
258 }
259
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'.
263
264 Abort with control-c in the next five seconds....
265 WARN;
266 wfCountDown( 5 );
267 return true;
268 }
269
270 }