MediaWiki  REL1_22
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 __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 
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 ) {
00216                     print " OK\n";
00217                 }
00218 
00219             } elseif ( !$this->mQuiet ) {
00220                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
00221             }
00222         }
00223     }
00224 
00229     public static function getDefaultOptionsNames() {
00230         $def = User::getDefaultOptions();
00231         $ret = array();
00232         foreach ( $def as $optname => $defaultValue ) {
00233             array_push( $ret, $optname );
00234         }
00235         return $ret;
00236     }
00237 
00238     #
00239     # Helper methods
00240     #
00241 
00242     public static function showUsageAndExit() {
00243 print <<<USAGE
00244 
00245 This script pass through all users and change one of their options.
00246 The new option is NOT validated.
00247 
00248 Usage:
00249     php userOptions.php --list
00250     php userOptions.php [user option] --usage
00251     php userOptions.php [options] <user option> --old <old value> --new <new value>
00252 
00253 Switchs:
00254     --list : list available user options and their default value
00255 
00256     --usage : report all options statistics or just one if you specify it.
00257 
00258     --old <old value> : the value to look for
00259     --new <new value> : new value to update users with
00260 
00261 Options:
00262     --nowarn: hides the 5 seconds warning
00263     --quiet : do not print what is happening
00264     --dry   : do not save user settings back to database
00265 
00266 USAGE;
00267     exit( 0 );
00268     }
00269 
00274     public function warn() {
00275 
00276         if ( $this->mQuick ) {
00277             return true;
00278         }
00279 
00280 print <<<WARN
00281 The script is about to change the skin for ALL USERS in the database.
00282 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
00283 
00284 Abort with control-c in the next five seconds....
00285 WARN;
00286         wfCountDown( 5 );
00287         return true;
00288     }
00289 
00290 }