From cecb985bee3bdd252e1b8dc0bd500b37cd52be01 Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Wed, 16 May 2007 20:58:53 +0000 Subject: Aktualisierung auf MediaWiki 1.10.0 Plugins angepasst und verbessert kleine Korrekturen am Design --- maintenance/userOptions.inc | 237 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) create mode 100644 maintenance/userOptions.inc (limited to 'maintenance/userOptions.inc') diff --git a/maintenance/userOptions.inc b/maintenance/userOptions.inc new file mode 100644 index 00000000..702ab715 --- /dev/null +++ b/maintenance/userOptions.inc @@ -0,0 +1,237 @@ +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 + php userOptions.php [options]  --old --new + +Switchs: + --list : list available user options and their default value + + --usage