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