Generalize and rename cleanupSkin.php so it can changes any option.
authorAntoine Musso <hashar@users.mediawiki.org>
Wed, 24 Jan 2007 22:13:12 +0000 (22:13 +0000)
committerAntoine Musso <hashar@users.mediawiki.org>
Wed, 24 Jan 2007 22:13:12 +0000 (22:13 +0000)
FIXME: does not validate the new value

maintenance/cleanupSkin.php [deleted file]
maintenance/userOptions.inc [new file with mode: 0644]
maintenance/userOptions.php [new file with mode: 0644]

diff --git a/maintenance/cleanupSkin.php b/maintenance/cleanupSkin.php
deleted file mode 100644 (file)
index 32bad30..0000000
+++ /dev/null
@@ -1,93 +0,0 @@
-<?php
-/**
- * Script to change users skins on the fly.
- * This is for at least MediaWiki 1.10alpha (r19611) and have not been
- * tested with previous versions. It should probably work with 1.7+.
- *
- * Made on an original idea by Fooey (freenode)
- *
- * @author Ashar Voultoiz <hashar@altern.org>
- */
-
-// Options we will use
-$options = array( 'quick' );
-$optionsWithArgs = array( 'old', 'new' );
-
-// This is a command line script, load tools and parse args
-require_once( 'commandLine.inc' );
-
-// Check for mandatory options or print an usage message
-if( !(isset($options['old']) && isset($options['new']) ) ) {
-print <<<USAGE
-This script pass through all users and change their skins from 'oldSkinName'
-to 'newSkinName'. There is NO validation about the new skin existence!
-
-Usage: php cleanupSkin.php --old <oldSkinName> --new <newSkinName>
-                           [--quick] [--quiet]
-
-Options:
-    --old <oldSkinName> : the old skin name
-    --new <newSkinName> : new skin name to update users with
-    --quick : hides the 5 seconds warning
-    --quiet : do not print what is happening
-
-
-USAGE;
-       exit(0);
-}
-
-// Load up the arguments:
-$oldSkinName = $options['old'];
-$newSkinName = $options['new'];
-$quick = isset($options['quick']);
-$quiet = isset($options['quiet']);
-
-// We list the user by user_id from one of the slave databases
-$dbr = wfGetDB( DB_SLAVE );
-$result = $dbr->select( 'user',
-       array( 'user_id' ),
-       array(),
-       __FILE__
-       );
-
-// The warning message and countdown
-if( !$quick ) {
-print <<<WARN
-The script is about to change the skin for ALL USERS in the database.
-Users with skin '$oldSkinName' will be made to use '$newSkinName'.
-
-Abort with control-c in the next five seconds....
-WARN;
-       require('counter.php');
-       for ($i=6;$i>=1;) {
-               print_c($i, --$i);
-               sleep(1);
-       }
-       print "\n";
-}
-
-// Iterate through the users
-while( $id = $dbr->fetchObject( $result ) ) {
-
-       $user = User::newFromId( $id->user_id );
-
-       // We get this users informations
-       $curSkinName = $user->getOption( 'skin' );
-       $username = $user->getName();
-
-       // Is he using the skin we want to migrate ?
-       if( $curSkinName == $oldSkinName ) {
-
-               if(!$quiet) print "Changing skin for $username ('$oldSkinName' -> '$newSkinName'):";
-
-               // Change skin and save it
-               $user->setOption( 'skin', $newSkinName );
-               $user->saveSettings();
-
-               if(!$quiet) print " OK\n";
-       } elseif(!$quiet) {
-               print "Not changing '$username' using skin '$curSkinName'\n";
-       }
-}
-print "Done.\n";
-?>
diff --git a/maintenance/userOptions.inc b/maintenance/userOptions.inc
new file mode 100644 (file)
index 0000000..9ad9ca3
--- /dev/null
@@ -0,0 +1,237 @@
+<?php
+// Options we will use
+$options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
+$optionsWithArgs = array( 'old', 'new' );
+
+require_once( 'commandLine.inc' );
+
+class userOptions {
+       public $mQuick;
+       public $mQuiet;
+       public $mDry;
+       public $mAnOption;
+       public $mOldValue;
+       public $mNewValue;
+
+       private $mMode, $mReady ;
+
+       /** Constructor. Will show usage and exit if script options are not correct */
+       function __construct( $opts, $args ) {
+               if( !$this->checkOpts( $opts, $args ) ) {
+                       userOptions::showUsageAndExit();
+               } else {
+                       $this->mReady = $this->initializeOpts( $opts, $args );
+               }
+       }
+
+
+       /** This is used to check options. Only needed on construction */
+       private function checkOpts( $opts, $args ) {
+               // The three possible ways to run the script:
+               $list   = isset( $opts['list'] );
+               $usage  = isset( $opts['usage'] ) && (count($args) <= 1);
+               $change = isset( $opts['old']) && isset($opts['new']) && (count($args) <= 1) ;
+
+               // We want only one of them
+               $isValid = (($list + $usage + $change) == 1);
+
+               return $isValid;
+       }
+
+       /** load script options in the object */
+       private function initializeOpts( $opts, $args ) {
+
+               $this->mQuick = isset( $opts['nowarn'] );
+               $this->mQuiet = isset( $opts['quiet'] );
+               $this->mDry   = isset( $opts['dry'] );
+
+               // Set object properties, specially 'mMode' used by run()
+               if( isset($opts['list']) ) {
+                       $this->mMode = 'LISTER' ;
+               } elseif( isset($opts['usage']) ) {
+                       $this->mMode = 'USAGER' ;
+                       $this->mAnOption = isset($args[0]) ? $args[0] : false ;
+               } elseif( isset($opts['old']) && isset($opts['new']) ) {
+                       $this->mMode = 'CHANGER' ;
+                       $this->mOldValue = $opts['old'] ;
+                       $this->mNewValue = $opts['new'] ;
+                       $this->mAnOption = $args[0];
+               } else {
+                       die("There is a bug in the software, this should never happen\n");
+               }
+
+               return true;
+       }
+
+       // Dumb stuff to run a mode.
+       public function run() {
+               if(!$this->mReady ) {
+                       return false;
+               }
+
+               $this->{$this->mMode}( );
+
+       }
+
+       #
+       # Modes.
+       # 
+
+       /** List default options and their value */
+       private function LISTER( ) {
+               $def = User::getDefaultOptions();
+               ksort($def);
+               $maxOpt = 0;
+               foreach( $def as $opt => $value ) {
+                       $maxOpt = max( $maxOpt, strlen($opt) );
+               }
+               foreach( $def as $opt => $value ) {
+                       printf( "%-{$maxOpt}s: %s\n", $opt, $value );
+               }
+       }
+
+       /** List options usage */
+       private function USAGER( ) {
+               $ret = array();
+               $defaultOptions = User::getDefaultOptions();
+
+               // We list user by user_id from one of the slave database
+               $dbr = wfGetDB( DB_SLAVE );
+               $result = $dbr->select( 'user',
+                       array( 'user_id' ),
+                       array(),
+                       __METHOD__
+                       );
+
+               while( $id = $dbr->fetchObject( $result ) ) {
+
+                       $user = User::newFromId( $id->user_id );
+
+                       // Get the options and update stats
+                       foreach( $defaultOptions as $name => $defaultValue ) {
+                               $userValue = $user->getOption( $name );
+                               if( $userValue <> $defaultValue ) {
+                                       @$ret[$name][$userValue]++;
+                               }
+                       }
+               }
+
+               foreach( $ret as $optionName => $usageStats ) {
+                       print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
+                       foreach( $usageStats as $value => $count ) {
+                               print " $count user(s): '$value'\n";
+                       }
+                       print "\n";
+               }
+       }
+
+
+       /** Change our users options */
+       private function CHANGER( ) {
+               $this->warn();
+
+               // We list user by user_id from one of the slave database
+               $dbr = wfGetDB( DB_SLAVE );
+               $result = $dbr->select( 'user',
+                       array( 'user_id' ),
+                       array(),
+                       __METHOD__
+                       );
+
+               while( $id = $dbr->fetchObject( $result ) ) {
+
+                       $user = User::newFromId( $id->user_id );
+
+                       $curValue = $user->getOption( $this->mAnOption );
+                       $username = $user->getName();
+
+                       if( $curValue == $this->mOldValue ) {
+
+                               if(!$this->mQuiet) {
+                                       print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
+                               }
+
+                               // Change value 
+                               $user->setOption( $this->mAnOption, $this->mNewValue );
+
+                               // Will not save the settings if run with --dry
+                               if(!$this->mDry) {
+                                       $user->saveSettings();
+                               }
+                               if( !$this->mQuiet) { print " OK\n"; }
+
+                       } elseif( !$this->mQuiet ) {
+                               print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
+                       }
+               }
+       }
+
+
+       /** Return an array of option names */
+       public static function getDefaultOptionsNames() {
+               $def = User::getDefaultOptions();
+               $ret = array();
+               foreach( $def as $optname => $defaultValue) {
+                       array_push( $ret, $optname );
+               }
+               return $ret;
+       }
+
+
+       #
+       # Helper methods
+       #
+
+       public static function showUsageAndExit() {
+print <<<USAGE
+
+This script pass through all users and change one of their options.
+The new option is NOT validated.
+
+Usage:
+    php userOptions.php --list
+    php userOptions.php <user option> --usage
+    php userOptions.php [options] <user option> --old <old value> --new <new value>
+
+Switchs:
+    --list : list available user options and their default value
+
+    --usage <option name> : report statistics about an option
+
+    --old <old value> : the value to look for
+    --new <new value> : new value to update users with
+
+Options:
+    --nowarn: hides the 5 seconds warning
+    --quiet : do not print what is happening
+       --dry   : do not save user settings back to database
+
+USAGE;
+    exit(0);
+       }
+
+       /** The warning message and countdown */
+       public function warn() {
+
+               if( $this->mQuick ) {
+                       return true;
+               }
+
+print <<<WARN
+The script is about to change the skin for ALL USERS in the database.
+Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
+
+Abort with control-c in the next five seconds....
+WARN;
+               require('counter.php');
+               for ($i=6;$i>=1;) {
+                       print_c($i, --$i);
+                       sleep(1);
+               }
+               print "\n";
+
+               return true;
+       }
+
+}
+?>
diff --git a/maintenance/userOptions.php b/maintenance/userOptions.php
new file mode 100644 (file)
index 0000000..f71d8e6
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Script to change users skins on the fly.
+ * This is for at least MediaWiki 1.10alpha (r19611) and have not been
+ * tested with previous versions. It should probably work with 1.7+.
+ *
+ * Made on an original idea by Fooey (freenode)
+ *
+ * @author Ashar Voultoiz <hashar@altern.org>
+ */
+
+// This is a command line script, load tools and parse args
+require_once( 'userOptions.inc' );
+
+// Load up our tool system, exit with usage() if options are not fine
+$uo = new userOptions( $options, $args );
+
+$uo->run();
+
+print "Done.\n";
+?>