8 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
9 $optionsWithArgs = array( 'old', 'new' );
11 require_once( 'commandLine.inc' );
14 * @ingroup Maintenance
24 private $mMode, $mReady ;
26 /** Constructor. Will show usage and exit if script options are not correct */
27 function __construct( $opts, $args ) {
28 if( !$this->checkOpts( $opts, $args ) ) {
29 userOptions::showUsageAndExit();
31 $this->mReady = $this->initializeOpts( $opts, $args );
36 /** This is used to check options. Only needed on construction */
37 private function checkOpts( $opts, $args ) {
38 // The three possible ways to run the script:
39 $list = isset( $opts['list'] );
40 $usage = isset( $opts['usage'] ) && (count($args) <= 1);
41 $change = isset( $opts['old']) && isset($opts['new']) && (count($args) <= 1) ;
43 // We want only one of them
44 $isValid = (($list + $usage + $change) == 1);
49 /** load script options in the object */
50 private function initializeOpts( $opts, $args ) {
52 $this->mQuick = isset( $opts['nowarn'] );
53 $this->mQuiet = isset( $opts['quiet'] );
54 $this->mDry = isset( $opts['dry'] );
56 // Set object properties, specially 'mMode' used by run()
57 if( isset($opts['list']) ) {
58 $this->mMode = 'LISTER' ;
59 } elseif( isset($opts['usage']) ) {
60 $this->mMode = 'USAGER' ;
61 $this->mAnOption = isset($args[0]) ? $args[0] : false ;
62 } elseif( isset($opts['old']) && isset($opts['new']) ) {
63 $this->mMode = 'CHANGER' ;
64 $this->mOldValue = $opts['old'] ;
65 $this->mNewValue = $opts['new'] ;
66 $this->mAnOption = $args[0];
68 die("There is a bug in the software, this should never happen\n");
74 // Dumb stuff to run a mode.
75 public function run() {
80 $this->{$this->mMode}( );
88 /** List default options and their value */
89 private function LISTER( ) {
90 $def = User::getDefaultOptions();
93 foreach( $def as $opt => $value ) {
94 $maxOpt = max( $maxOpt, strlen($opt) );
96 foreach( $def as $opt => $value ) {
97 printf( "%-{$maxOpt}s: %s\n", $opt, $value );
101 /** List options usage */
102 private function USAGER( ) {
104 $defaultOptions = User::getDefaultOptions();
106 // We list user by user_id from one of the slave database
107 $dbr = wfGetDB( DB_SLAVE );
108 $result = $dbr->select( 'user',
114 while( $id = $dbr->fetchObject( $result ) ) {
116 $user = User::newFromId( $id->user_id );
118 // Get the options and update stats
119 if( $this->mAnOption ) {
121 if(!array_key_exists( $this->mAnOption, $defaultOptions ) ) {
122 print "Invalid user option. Use --list to see valid choices\n";
126 $userValue = $user->getOption( $this->mAnOption );
127 if( $userValue <> $defaultOptions[$this->mAnOption] ) {
128 @$ret[$this->mAnOption][$userValue]++;
133 foreach( $defaultOptions as $name => $defaultValue ) {
134 $userValue = $user->getOption( $name );
135 if( $userValue <> $defaultValue ) {
136 @$ret[$name][$userValue]++;
142 foreach( $ret as $optionName => $usageStats ) {
143 print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
144 foreach( $usageStats as $value => $count ) {
145 print " $count user(s): '$value'\n";
152 /** Change our users options */
153 private function CHANGER( ) {
156 // We list user by user_id from one of the slave database
157 $dbr = wfGetDB( DB_SLAVE );
158 $result = $dbr->select( 'user',
164 while( $id = $dbr->fetchObject( $result ) ) {
166 $user = User::newFromId( $id->user_id );
168 $curValue = $user->getOption( $this->mAnOption );
169 $username = $user->getName();
171 if( $curValue == $this->mOldValue ) {
174 print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' to '{$this->mNewValue}'): ";
178 $user->setOption( $this->mAnOption, $this->mNewValue );
180 // Will not save the settings if run with --dry
182 $user->saveSettings();
184 if( !$this->mQuiet) { print " OK\n"; }
186 } elseif( !$this->mQuiet ) {
187 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
193 /** Return an array of option names */
194 public static function getDefaultOptionsNames() {
195 $def = User::getDefaultOptions();
197 foreach( $def as $optname => $defaultValue) {
198 array_push( $ret, $optname );
208 public static function showUsageAndExit() {
211 This script pass through all users and change one of their options.
212 The new option is NOT validated.
215 php userOptions.php --list
216 php userOptions.php [user option] --usage
217 php userOptions.php [options] <user option> --old <old value> --new <new value>
220 --list : list available user options and their default value
222 --usage : report all options statistics or just one if you specify it.
224 --old <old value> : the value to look for
225 --new <new value> : new value to update users with
228 --nowarn: hides the 5 seconds warning
229 --quiet : do not print what is happening
230 --dry : do not save user settings back to database
236 /** The warning message and countdown */
237 public function warn() {
239 if( $this->mQuick ) {
244 The script is about to change the skin for ALL USERS in the database.
245 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
247 Abort with control-c in the next five seconds....
249 require('counter.php');