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