00001 <?php
00007
00008 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
00009 $optionsWithArgs = array( 'old', 'new' );
00010
00011 require_once( 'commandLine.inc' );
00012
00016 class userOptions {
00017 public $mQuick;
00018 public $mQuiet;
00019 public $mDry;
00020 public $mAnOption;
00021 public $mOldValue;
00022 public $mNewValue;
00023
00024 private $mMode, $mReady ;
00025
00027 function __construct( $opts, $args ) {
00028 if( !$this->checkOpts( $opts, $args ) ) {
00029 userOptions::showUsageAndExit();
00030 } else {
00031 $this->mReady = $this->initializeOpts( $opts, $args );
00032 }
00033 }
00034
00035
00037 private function checkOpts( $opts, $args ) {
00038
00039 $list = isset( $opts['list'] );
00040 $usage = isset( $opts['usage'] ) && (count($args) <= 1);
00041 $change = isset( $opts['old']) && isset($opts['new']) && (count($args) <= 1) ;
00042
00043
00044 $isValid = (($list + $usage + $change) == 1);
00045
00046 return $isValid;
00047 }
00048
00050 private function initializeOpts( $opts, $args ) {
00051
00052 $this->mQuick = isset( $opts['nowarn'] );
00053 $this->mQuiet = isset( $opts['quiet'] );
00054 $this->mDry = isset( $opts['dry'] );
00055
00056
00057 if( isset($opts['list']) ) {
00058 $this->mMode = 'LISTER' ;
00059 } elseif( isset($opts['usage']) ) {
00060 $this->mMode = 'USAGER' ;
00061 $this->mAnOption = isset($args[0]) ? $args[0] : false ;
00062 } elseif( isset($opts['old']) && isset($opts['new']) ) {
00063 $this->mMode = 'CHANGER' ;
00064 $this->mOldValue = $opts['old'] ;
00065 $this->mNewValue = $opts['new'] ;
00066 $this->mAnOption = $args[0];
00067 } else {
00068 die("There is a bug in the software, this should never happen\n");
00069 }
00070
00071 return true;
00072 }
00073
00074
00075 public function run() {
00076 if(!$this->mReady ) {
00077 return false;
00078 }
00079
00080 $this->{$this->mMode}( );
00081
00082 }
00083
00084 #
00085 # Modes.
00086 #
00087
00089 private function LISTER( ) {
00090 $def = User::getDefaultOptions();
00091 ksort($def);
00092 $maxOpt = 0;
00093 foreach( $def as $opt => $value ) {
00094 $maxOpt = max( $maxOpt, strlen($opt) );
00095 }
00096 foreach( $def as $opt => $value ) {
00097 printf( "%-{$maxOpt}s: %s\n", $opt, $value );
00098 }
00099 }
00100
00102 private function USAGER( ) {
00103 $ret = array();
00104 $defaultOptions = User::getDefaultOptions();
00105
00106
00107 $dbr = wfGetDB( DB_SLAVE );
00108 $result = $dbr->select( 'user',
00109 array( 'user_id' ),
00110 array(),
00111 __METHOD__
00112 );
00113
00114 while( $id = $dbr->fetchObject( $result ) ) {
00115
00116 $user = User::newFromId( $id->user_id );
00117
00118
00119 if( $this->mAnOption ) {
00120
00121 if(!array_key_exists( $this->mAnOption, $defaultOptions ) ) {
00122 print "Invalid user option. Use --list to see valid choices\n";
00123 exit;
00124 }
00125
00126 $userValue = $user->getOption( $this->mAnOption );
00127 if( $userValue <> $defaultOptions[$this->mAnOption] ) {
00128 @$ret[$this->mAnOption][$userValue]++;
00129 }
00130
00131 } else {
00132
00133 foreach( $defaultOptions as $name => $defaultValue ) {
00134 $userValue = $user->getOption( $name );
00135 if( $userValue <> $defaultValue ) {
00136 @$ret[$name][$userValue]++;
00137 }
00138 }
00139 }
00140 }
00141
00142 foreach( $ret as $optionName => $usageStats ) {
00143 print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
00144 foreach( $usageStats as $value => $count ) {
00145 print " $count user(s): '$value'\n";
00146 }
00147 print "\n";
00148 }
00149 }
00150
00151
00153 private function CHANGER( ) {
00154 $this->warn();
00155
00156
00157 $dbr = wfGetDB( DB_SLAVE );
00158 $result = $dbr->select( 'user',
00159 array( 'user_id' ),
00160 array(),
00161 __METHOD__
00162 );
00163
00164 while( $id = $dbr->fetchObject( $result ) ) {
00165
00166 $user = User::newFromId( $id->user_id );
00167
00168 $curValue = $user->getOption( $this->mAnOption );
00169 $username = $user->getName();
00170
00171 if( $curValue == $this->mOldValue ) {
00172
00173 if(!$this->mQuiet) {
00174 print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
00175 }
00176
00177
00178 $user->setOption( $this->mAnOption, $this->mNewValue );
00179
00180
00181 if(!$this->mDry) {
00182 $user->saveSettings();
00183 }
00184 if( !$this->mQuiet) { print " OK\n"; }
00185
00186 } elseif( !$this->mQuiet ) {
00187 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
00188 }
00189 }
00190 }
00191
00192
00194 public static function getDefaultOptionsNames() {
00195 $def = User::getDefaultOptions();
00196 $ret = array();
00197 foreach( $def as $optname => $defaultValue) {
00198 array_push( $ret, $optname );
00199 }
00200 return $ret;
00201 }
00202
00203
00204 #
00205 # Helper methods
00206 #
00207
00208 public static function showUsageAndExit() {
00209 print <<<USAGE
00210
00211 This script pass through all users and change one of their options.
00212 The new option is NOT validated.
00213
00214 Usage:
00215 php userOptions.php --list
00216 php userOptions.php [user option] --usage
00217 php userOptions.php [options] <user option> --old <old value> --new <new value>
00218
00219 Switchs:
00220 --list : list available user options and their default value
00221
00222 --usage : report all options statistics or just one if you specify it.
00223
00224 --old <old value> : the value to look for
00225 --new <new value> : new value to update users with
00226
00227 Options:
00228 --nowarn: hides the 5 seconds warning
00229 --quiet : do not print what is happening
00230 --dry : do not save user settings back to database
00231
00232 USAGE;
00233 exit(0);
00234 }
00235
00237 public function warn() {
00238
00239 if( $this->mQuick ) {
00240 return true;
00241 }
00242
00243 print <<<WARN
00244 The script is about to change the skin for ALL USERS in the database.
00245 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
00246
00247 Abort with control-c in the next five seconds....
00248 WARN;
00249 require('counter.php');
00250 for ($i=6;$i>=1;) {
00251 print_c($i, --$i);
00252 sleep(1);
00253 }
00254 print "\n";
00255
00256 return true;
00257 }
00258
00259 }