MediaWiki
REL1_23
|
00001 <?php 00024 // Options we will use 00025 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' ); 00026 $optionsWithArgs = array( 'old', 'new' ); 00027 00028 require_once __DIR__ . '/commandLine.inc'; 00029 00033 class UserOptions { 00034 public $mQuick; 00035 public $mQuiet; 00036 public $mDry; 00037 public $mAnOption; 00038 public $mOldValue; 00039 public $mNewValue; 00040 00041 private $mMode, $mReady; 00042 00044 function __construct( $opts, $args ) { 00045 if ( !$this->checkOpts( $opts, $args ) ) { 00046 UserOptions::showUsageAndExit(); 00047 } else { 00048 $this->mReady = $this->initializeOpts( $opts, $args ); 00049 } 00050 } 00051 00060 private function checkOpts( $opts, $args ) { 00061 // The three possible ways to run the script: 00062 $list = isset( $opts['list'] ); 00063 $usage = isset( $opts['usage'] ) && ( count( $args ) <= 1 ); 00064 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 ); 00065 00066 // We want only one of them 00067 $isValid = ( ( $list + $usage + $change ) == 1 ); 00068 00069 return $isValid; 00070 } 00071 00080 private function initializeOpts( $opts, $args ) { 00081 00082 $this->mQuick = isset( $opts['nowarn'] ); 00083 $this->mQuiet = isset( $opts['quiet'] ); 00084 $this->mDry = isset( $opts['dry'] ); 00085 00086 // Set object properties, specially 'mMode' used by run() 00087 if ( isset( $opts['list'] ) ) { 00088 $this->mMode = 'LISTER'; 00089 } elseif ( isset( $opts['usage'] ) ) { 00090 $this->mMode = 'USAGER'; 00091 $this->mAnOption = isset( $args[0] ) ? $args[0] : false; 00092 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) { 00093 $this->mMode = 'CHANGER'; 00094 $this->mOldValue = $opts['old']; 00095 $this->mNewValue = $opts['new']; 00096 $this->mAnOption = $args[0]; 00097 } else { 00098 die( "There is a bug in the software, this should never happen\n" ); 00099 } 00100 00101 return true; 00102 } 00103 00104 // Dumb stuff to run a mode. 00105 public function run() { 00106 if ( !$this->mReady ) { 00107 return false; 00108 } 00109 00110 $this->{ $this->mMode } (); 00111 return true; 00112 } 00113 00114 # 00115 # Modes. 00116 # 00117 00119 private function LISTER() { 00120 $def = User::getDefaultOptions(); 00121 ksort( $def ); 00122 $maxOpt = 0; 00123 foreach ( $def as $opt => $value ) { 00124 $maxOpt = max( $maxOpt, strlen( $opt ) ); 00125 } 00126 foreach ( $def as $opt => $value ) { 00127 printf( "%-{$maxOpt}s: %s\n", $opt, $value ); 00128 } 00129 } 00130 00132 private function USAGER() { 00133 $ret = array(); 00134 $defaultOptions = User::getDefaultOptions(); 00135 00136 // We list user by user_id from one of the slave database 00137 $dbr = wfGetDB( DB_SLAVE ); 00138 $result = $dbr->select( 'user', 00139 array( 'user_id' ), 00140 array(), 00141 __METHOD__ 00142 ); 00143 00144 foreach ( $result as $id ) { 00145 00146 $user = User::newFromId( $id->user_id ); 00147 00148 // Get the options and update stats 00149 if ( $this->mAnOption ) { 00150 00151 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) { 00152 print "Invalid user option. Use --list to see valid choices\n"; 00153 exit; 00154 } 00155 00156 $userValue = $user->getOption( $this->mAnOption ); 00157 if ( $userValue <> $defaultOptions[$this->mAnOption] ) { 00158 @$ret[$this->mAnOption][$userValue]++; 00159 } 00160 00161 } else { 00162 00163 foreach ( $defaultOptions as $name => $defaultValue ) { 00164 $userValue = $user->getOption( $name ); 00165 if ( $userValue <> $defaultValue ) { 00166 @$ret[$name][$userValue]++; 00167 } 00168 } 00169 } 00170 } 00171 00172 foreach ( $ret as $optionName => $usageStats ) { 00173 print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n"; 00174 foreach ( $usageStats as $value => $count ) { 00175 print " $count user(s): '$value'\n"; 00176 } 00177 print "\n"; 00178 } 00179 } 00180 00182 private function CHANGER() { 00183 $this->warn(); 00184 00185 // We list user by user_id from one of the slave database 00186 $dbr = wfGetDB( DB_SLAVE ); 00187 $result = $dbr->select( 'user', 00188 array( 'user_id' ), 00189 array(), 00190 __METHOD__ 00191 ); 00192 00193 foreach ( $result as $id ) { 00194 00195 $user = User::newFromId( $id->user_id ); 00196 00197 $curValue = $user->getOption( $this->mAnOption ); 00198 $username = $user->getName(); 00199 00200 if ( $curValue == $this->mOldValue ) { 00201 00202 if ( !$this->mQuiet ) { 00203 print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): "; 00204 } 00205 00206 // Change value 00207 $user->setOption( $this->mAnOption, $this->mNewValue ); 00208 00209 // Will not save the settings if run with --dry 00210 if ( !$this->mDry ) { 00211 $user->saveSettings(); 00212 } 00213 if ( !$this->mQuiet ) { 00214 print " OK\n"; 00215 } 00216 00217 } elseif ( !$this->mQuiet ) { 00218 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n"; 00219 } 00220 } 00221 } 00222 00227 public static function getDefaultOptionsNames() { 00228 $def = User::getDefaultOptions(); 00229 $ret = array(); 00230 foreach ( $def as $optname => $defaultValue ) { 00231 array_push( $ret, $optname ); 00232 } 00233 return $ret; 00234 } 00235 00236 # 00237 # Helper methods 00238 # 00239 00240 public static function showUsageAndExit() { 00241 print <<<USAGE 00242 00243 This script pass through all users and change one of their options. 00244 The new option is NOT validated. 00245 00246 Usage: 00247 php userOptions.php --list 00248 php userOptions.php [user option] --usage 00249 php userOptions.php [options] <user option> --old <old value> --new <new value> 00250 00251 Switchs: 00252 --list : list available user options and their default value 00253 00254 --usage : report all options statistics or just one if you specify it. 00255 00256 --old <old value> : the value to look for 00257 --new <new value> : new value to update users with 00258 00259 Options: 00260 --nowarn: hides the 5 seconds warning 00261 --quiet : do not print what is happening 00262 --dry : do not save user settings back to database 00263 00264 USAGE; 00265 exit( 0 ); 00266 } 00267 00272 public function warn() { 00273 00274 if ( $this->mQuick ) { 00275 return true; 00276 } 00277 00278 print <<<WARN 00279 The script is about to change the skin for ALL USERS in the database. 00280 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'. 00281 00282 Abort with control-c in the next five seconds.... 00283 WARN; 00284 wfCountDown( 5 ); 00285 return true; 00286 } 00287 00288 }