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