3 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
4 $optionsWithArgs = array( 'old', 'new' );
6 require_once( 'commandLine.inc' );
16 private $mMode, $mReady ;
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();
23 $this->mReady = $this->initializeOpts( $opts, $args );
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) ;
35 // We want only one of them
36 $isValid = (($list + $usage + $change) == 1);
41 /** load script options in the object */
42 private function initializeOpts( $opts, $args ) {
44 $this->mQuick = isset( $opts['nowarn'] );
45 $this->mQuiet = isset( $opts['quiet'] );
46 $this->mDry = isset( $opts['dry'] );
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];
60 die("There is a bug in the software, this should never happen\n");
66 // Dumb stuff to run a mode.
67 public function run() {
72 $this->{$this->mMode}( );
80 /** List default options and their value */
81 private function LISTER( ) {
82 $def = User::getDefaultOptions();
85 foreach( $def as $opt => $value ) {
86 $maxOpt = max( $maxOpt, strlen($opt) );
88 foreach( $def as $opt => $value ) {
89 printf( "%-{$maxOpt}s: %s\n", $opt, $value );
93 /** List options usage */
94 private function USAGER( ) {
96 $defaultOptions = User::getDefaultOptions();
98 // We list user by user_id from one of the slave database
99 $dbr = wfGetDB( DB_SLAVE );
100 $result = $dbr->select( 'user',
106 while( $id = $dbr->fetchObject( $result ) ) {
108 $user = User::newFromId( $id->user_id );
110 // Get the options and update stats
111 if( $this->mAnOption ) {
113 if(!array_key_exists( $this->mAnOption, $defaultOptions ) ) {
114 print "Invalid user option. Use --list to see valid choices\n";
118 $userValue = $user->getOption( $this->mAnOption );
119 if( $userValue <> $defaultOptions[$this->mAnOption] ) {
120 @$ret[$this->mAnOption][$userValue]++;
125 foreach( $defaultOptions as $name => $defaultValue ) {
126 $userValue = $user->getOption( $name );
127 if( $userValue <> $defaultValue ) {
128 @$ret[$name][$userValue]++;
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";
144 /** Change our users options */
145 private function CHANGER( ) {
148 // We list user by user_id from one of the slave database
149 $dbr = wfGetDB( DB_SLAVE );
150 $result = $dbr->select( 'user',
156 while( $id = $dbr->fetchObject( $result ) ) {
158 $user = User::newFromId( $id->user_id );
160 $curValue = $user->getOption( $this->mAnOption );
161 $username = $user->getName();
163 if( $curValue == $this->mOldValue ) {
166 print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
170 $user->setOption( $this->mAnOption, $this->mNewValue );
172 // Will not save the settings if run with --dry
174 $user->saveSettings();
176 if( !$this->mQuiet) { print " OK\n"; }
178 } elseif( !$this->mQuiet ) {
179 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
185 /** Return an array of option names */
186 public static function getDefaultOptionsNames() {
187 $def = User::getDefaultOptions();
189 foreach( $def as $optname => $defaultValue) {
190 array_push( $ret, $optname );
200 public static function showUsageAndExit() {
203 This script pass through all users and change one of their options.
204 The new option is NOT validated.
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>
212 --list : list available user options and their default value
214 --usage : report all options statistics or just one if you specify it.
216 --old <old value> : the value to look for
217 --new <new value> : new value to update users with
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
228 /** The warning message and countdown */
229 public function warn() {
231 if( $this->mQuick ) {
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'.
239 Abort with control-c in the next five seconds....
241 require('counter.php');