Move up to date the parser test expectation.
[lhc/web/wiklou.git] / maintenance / userOptions.inc
1 <?php
2 /**
3 * @file
4 * @ingroup Maintenance
5 */
6
7 // Options we will use
8 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
9 $optionsWithArgs = array( 'old', 'new' );
10
11 require_once( dirname( __FILE__ ) . '/commandLine.inc' );
12
13 /**
14 * @ingroup Maintenance
15 */
16 class userOptions {
17 public $mQuick;
18 public $mQuiet;
19 public $mDry;
20 public $mAnOption;
21 public $mOldValue;
22 public $mNewValue;
23
24 private $mMode, $mReady ;
25
26 /** Constructor. Will show usage and exit if script options are not correct */
27 function __construct( $opts, $args ) {
28 if ( !$this->checkOpts( $opts, $args ) ) {
29 userOptions::showUsageAndExit();
30 } else {
31 $this->mReady = $this->initializeOpts( $opts, $args );
32 }
33 }
34
35
36 /** This is used to check options. Only needed on construction */
37 private function checkOpts( $opts, $args ) {
38 // The three possible ways to run the script:
39 $list = isset( $opts['list'] );
40 $usage = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
41 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 ) ;
42
43 // We want only one of them
44 $isValid = ( ( $list + $usage + $change ) == 1 );
45
46 return $isValid;
47 }
48
49 /** load script options in the object */
50 private function initializeOpts( $opts, $args ) {
51
52 $this->mQuick = isset( $opts['nowarn'] );
53 $this->mQuiet = isset( $opts['quiet'] );
54 $this->mDry = isset( $opts['dry'] );
55
56 // Set object properties, specially 'mMode' used by run()
57 if ( isset( $opts['list'] ) ) {
58 $this->mMode = 'LISTER' ;
59 } elseif ( isset( $opts['usage'] ) ) {
60 $this->mMode = 'USAGER' ;
61 $this->mAnOption = isset( $args[0] ) ? $args[0] : false ;
62 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
63 $this->mMode = 'CHANGER' ;
64 $this->mOldValue = $opts['old'] ;
65 $this->mNewValue = $opts['new'] ;
66 $this->mAnOption = $args[0];
67 } else {
68 die( "There is a bug in the software, this should never happen\n" );
69 }
70
71 return true;
72 }
73
74 // Dumb stuff to run a mode.
75 public function run() {
76 if ( !$this->mReady ) {
77 return false;
78 }
79
80 $this-> { $this->mMode } ( );
81
82 }
83
84 #
85 # Modes.
86 #
87
88 /** List default options and their value */
89 private function LISTER( ) {
90 $def = User::getDefaultOptions();
91 ksort( $def );
92 $maxOpt = 0;
93 foreach ( $def as $opt => $value ) {
94 $maxOpt = max( $maxOpt, strlen( $opt ) );
95 }
96 foreach ( $def as $opt => $value ) {
97 printf( "%-{$maxOpt}s: %s\n", $opt, $value );
98 }
99 }
100
101 /** List options usage */
102 private function USAGER( ) {
103 $ret = array();
104 $defaultOptions = User::getDefaultOptions();
105
106 // We list user by user_id from one of the slave database
107 $dbr = wfGetDB( DB_SLAVE );
108 $result = $dbr->select( 'user',
109 array( 'user_id' ),
110 array(),
111 __METHOD__
112 );
113
114 while ( $id = $dbr->fetchObject( $result ) ) {
115
116 $user = User::newFromId( $id->user_id );
117
118 // Get the options and update stats
119 if ( $this->mAnOption ) {
120
121 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
122 print "Invalid user option. Use --list to see valid choices\n";
123 exit;
124 }
125
126 $userValue = $user->getOption( $this->mAnOption );
127 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
128 @$ret[$this->mAnOption][$userValue]++;
129 }
130
131 } else {
132
133 foreach ( $defaultOptions as $name => $defaultValue ) {
134 $userValue = $user->getOption( $name );
135 if ( $userValue <> $defaultValue ) {
136 @$ret[$name][$userValue]++;
137 }
138 }
139 }
140 }
141
142 foreach ( $ret as $optionName => $usageStats ) {
143 print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
144 foreach ( $usageStats as $value => $count ) {
145 print " $count user(s): '$value'\n";
146 }
147 print "\n";
148 }
149 }
150
151
152 /** Change our users options */
153 private function CHANGER( ) {
154 $this->warn();
155
156 // We list user by user_id from one of the slave database
157 $dbr = wfGetDB( DB_SLAVE );
158 $result = $dbr->select( 'user',
159 array( 'user_id' ),
160 array(),
161 __METHOD__
162 );
163
164 while ( $id = $dbr->fetchObject( $result ) ) {
165
166 $user = User::newFromId( $id->user_id );
167
168 $curValue = $user->getOption( $this->mAnOption );
169 $username = $user->getName();
170
171 if ( $curValue == $this->mOldValue ) {
172
173 if ( !$this->mQuiet ) {
174 print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
175 }
176
177 // Change value
178 $user->setOption( $this->mAnOption, $this->mNewValue );
179
180 // Will not save the settings if run with --dry
181 if ( !$this->mDry ) {
182 $user->saveSettings();
183 }
184 if ( !$this->mQuiet ) { print " OK\n"; }
185
186 } elseif ( !$this->mQuiet ) {
187 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
188 }
189 }
190 }
191
192
193 /** Return an array of option names */
194 public static function getDefaultOptionsNames() {
195 $def = User::getDefaultOptions();
196 $ret = array();
197 foreach ( $def as $optname => $defaultValue ) {
198 array_push( $ret, $optname );
199 }
200 return $ret;
201 }
202
203
204 #
205 # Helper methods
206 #
207
208 public static function showUsageAndExit() {
209 print <<<USAGE
210
211 This script pass through all users and change one of their options.
212 The new option is NOT validated.
213
214 Usage:
215 php userOptions.php --list
216 php userOptions.php [user option] --usage
217 php userOptions.php [options] <user option> --old <old value> --new <new value>
218
219 Switchs:
220 --list : list available user options and their default value
221
222 --usage : report all options statistics or just one if you specify it.
223
224 --old <old value> : the value to look for
225 --new <new value> : new value to update users with
226
227 Options:
228 --nowarn: hides the 5 seconds warning
229 --quiet : do not print what is happening
230 --dry : do not save user settings back to database
231
232 USAGE;
233 exit( 0 );
234 }
235
236 /** The warning message and countdown */
237 public function warn() {
238
239 if ( $this->mQuick ) {
240 return true;
241 }
242
243 print <<<WARN
244 The script is about to change the skin for ALL USERS in the database.
245 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
246
247 Abort with control-c in the next five seconds....
248 WARN;
249 wfCountDown( 5 );
250 return true;
251 }
252
253 }