MediaWiki  REL1_19
userOptions.inc
Go to the documentation of this file.
00001 <?php
00024 // Options we will use
00025 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
00026 $optionsWithArgs = array( 'old', 'new' );
00027 
00028 require_once( dirname( __FILE__ ) . '/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 
00052 
00061         private function checkOpts( $opts, $args ) {
00062                 // The three possible ways to run the script:
00063                 $list   = isset( $opts['list'] );
00064                 $usage  = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
00065                 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 ) ;
00066 
00067                 // We want only one of them
00068                 $isValid = ( ( $list + $usage + $change ) == 1 );
00069 
00070                 return $isValid;
00071         }
00072 
00081         private function initializeOpts( $opts, $args ) {
00082 
00083                 $this->mQuick = isset( $opts['nowarn'] );
00084                 $this->mQuiet = isset( $opts['quiet'] );
00085                 $this->mDry   = isset( $opts['dry'] );
00086 
00087                 // Set object properties, specially 'mMode' used by run()
00088                 if ( isset( $opts['list'] ) ) {
00089                         $this->mMode = 'LISTER' ;
00090                 } elseif ( isset( $opts['usage'] ) ) {
00091                         $this->mMode = 'USAGER' ;
00092                         $this->mAnOption = isset( $args[0] ) ? $args[0] : false ;
00093                 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
00094                         $this->mMode = 'CHANGER' ;
00095                         $this->mOldValue = $opts['old'] ;
00096                         $this->mNewValue = $opts['new'] ;
00097                         $this->mAnOption = $args[0];
00098                 } else {
00099                         die( "There is a bug in the software, this should never happen\n" );
00100                 }
00101 
00102                 return true;
00103         }
00104 
00105         // Dumb stuff to run a mode.
00106         public function run() {
00107                 if ( !$this->mReady ) {
00108                         return false;
00109                 }
00110 
00111                 $this->{ $this->mMode } ( );
00112                 return true;
00113         }
00114 
00115         #
00116         # Modes.
00117         #
00118 
00120         private function LISTER( ) {
00121                 $def = User::getDefaultOptions();
00122                 ksort( $def );
00123                 $maxOpt = 0;
00124                 foreach ( $def as $opt => $value ) {
00125                         $maxOpt = max( $maxOpt, strlen( $opt ) );
00126                 }
00127                 foreach ( $def as $opt => $value ) {
00128                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
00129                 }
00130         }
00131 
00133         private function USAGER( ) {
00134                 $ret = array();
00135                 $defaultOptions = User::getDefaultOptions();
00136 
00137                 // We list user by user_id from one of the slave database
00138                 $dbr = wfGetDB( DB_SLAVE );
00139                 $result = $dbr->select( 'user',
00140                         array( 'user_id' ),
00141                         array(),
00142                         __METHOD__
00143                         );
00144 
00145                 foreach ( $result as $id ) {
00146 
00147                         $user = User::newFromId( $id->user_id );
00148 
00149                         // Get the options and update stats
00150                         if ( $this->mAnOption ) {
00151 
00152                                 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
00153                                         print "Invalid user option. Use --list to see valid choices\n";
00154                                         exit;
00155                                 }
00156 
00157                                 $userValue = $user->getOption( $this->mAnOption );
00158                                 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
00159                                         @$ret[$this->mAnOption][$userValue]++;
00160                                 }
00161 
00162                         } else {
00163 
00164                                 foreach ( $defaultOptions as $name => $defaultValue ) {
00165                                         $userValue = $user->getOption( $name );
00166                                         if ( $userValue <> $defaultValue ) {
00167                                                 @$ret[$name][$userValue]++;
00168                                         }
00169                                 }
00170                         }
00171                 }
00172 
00173                 foreach ( $ret as $optionName => $usageStats ) {
00174                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
00175                         foreach ( $usageStats as $value => $count ) {
00176                                 print " $count user(s): '$value'\n";
00177                         }
00178                         print "\n";
00179                 }
00180         }
00181 
00182 
00184         private function CHANGER( ) {
00185                 $this->warn();
00186 
00187                 // We list user by user_id from one of the slave database
00188                 $dbr = wfGetDB( DB_SLAVE );
00189                 $result = $dbr->select( 'user',
00190                         array( 'user_id' ),
00191                         array(),
00192                         __METHOD__
00193                         );
00194 
00195                 foreach ( $result as $id ) {
00196 
00197                         $user = User::newFromId( $id->user_id );
00198 
00199                         $curValue = $user->getOption( $this->mAnOption );
00200                         $username = $user->getName();
00201 
00202                         if ( $curValue == $this->mOldValue ) {
00203 
00204                                 if ( !$this->mQuiet ) {
00205                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
00206                                 }
00207 
00208                                 // Change value
00209                                 $user->setOption( $this->mAnOption, $this->mNewValue );
00210 
00211                                 // Will not save the settings if run with --dry
00212                                 if ( !$this->mDry ) {
00213                                         $user->saveSettings();
00214                                 }
00215                                 if ( !$this->mQuiet ) { print " OK\n"; }
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 }