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