Factorise $this->getTitle() call in Article::confirmDelete()
[mediawiki.git] / maintenance / userOptions.inc
blob9b8714d188d571a33955bd6928532fa3da9b9deb
1 <?php
2 /**
3  * Helper class for userOptions.php script.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  * http://www.gnu.org/copyleft/gpl.html
19  *
20  * @file
21  * @ingroup Maintenance
22  */
24 // Options we will use
25 $options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
26 $optionsWithArgs = array( 'old', 'new' );
28 require_once __DIR__ . '/commandLine.inc';
30 /**
31  * @ingroup Maintenance
32  */
33 class UserOptions {
34         public $mQuick;
35         public $mQuiet;
36         public $mDry;
37         public $mAnOption;
38         public $mOldValue;
39         public $mNewValue;
41         private $mMode, $mReady;
43         /** Constructor. Will show usage and exit if script options are not correct */
44         function __construct( $opts, $args ) {
45                 if ( !$this->checkOpts( $opts, $args ) ) {
46                         UserOptions::showUsageAndExit();
47                 } else {
48                         $this->mReady = $this->initializeOpts( $opts, $args );
49                 }
50         }
52         /**
53          * This is used to check options. Only needed on construction
54          *
55          * @param array $opts
56          * @param array $args
57          *
58          * @return bool
59          */
60         private function checkOpts( $opts, $args ) {
61                 // The three possible ways to run the script:
62                 $list = isset( $opts['list'] );
63                 $usage = isset( $opts['usage'] ) && ( count( $args ) <= 1 );
64                 $change = isset( $opts['old'] ) && isset( $opts['new'] ) && ( count( $args ) <= 1 );
66                 // We want only one of them
67                 $isValid = ( ( $list + $usage + $change ) == 1 );
69                 return $isValid;
70         }
72         /**
73          * load script options in the object
74          *
75          * @param array $opts
76          * @param array $args
77          *
78          * @return bool
79          */
80         private function initializeOpts( $opts, $args ) {
82                 $this->mQuick = isset( $opts['nowarn'] );
83                 $this->mQuiet = isset( $opts['quiet'] );
84                 $this->mDry = isset( $opts['dry'] );
86                 // Set object properties, specially 'mMode' used by run()
87                 if ( isset( $opts['list'] ) ) {
88                         $this->mMode = 'LISTER';
89                 } elseif ( isset( $opts['usage'] ) ) {
90                         $this->mMode = 'USAGER';
91                         $this->mAnOption = isset( $args[0] ) ? $args[0] : false;
92                 } elseif ( isset( $opts['old'] ) && isset( $opts['new'] ) ) {
93                         $this->mMode = 'CHANGER';
94                         $this->mOldValue = $opts['old'];
95                         $this->mNewValue = $opts['new'];
96                         $this->mAnOption = $args[0];
97                 } else {
98                         die( "There is a bug in the software, this should never happen\n" );
99                 }
101                 return true;
102         }
104         // Dumb stuff to run a mode.
105         public function run() {
106                 if ( !$this->mReady ) {
107                         return false;
108                 }
110                 $this->{$this->mMode}();
112                 return true;
113         }
115         #
116         # Modes.
117         #
119         /** List default options and their value */
120         private function LISTER() {
121                 $def = User::getDefaultOptions();
122                 ksort( $def );
123                 $maxOpt = 0;
124                 foreach ( $def as $opt => $value ) {
125                         $maxOpt = max( $maxOpt, strlen( $opt ) );
126                 }
127                 foreach ( $def as $opt => $value ) {
128                         printf( "%-{$maxOpt}s: %s\n", $opt, $value );
129                 }
130         }
132         /** List options usage */
133         private function USAGER() {
134                 $ret = array();
135                 $defaultOptions = User::getDefaultOptions();
137                 // We list user by user_id from one of the slave database
138                 $dbr = wfGetDB( DB_SLAVE );
139                 $result = $dbr->select( 'user',
140                         array( 'user_id' ),
141                         array(),
142                         __METHOD__
143                 );
145                 foreach ( $result as $id ) {
147                         $user = User::newFromId( $id->user_id );
149                         // Get the options and update stats
150                         if ( $this->mAnOption ) {
152                                 if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
153                                         print "Invalid user option. Use --list to see valid choices\n";
154                                         exit;
155                                 }
157                                 $userValue = $user->getOption( $this->mAnOption );
158                                 if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
159                                         // @codingStandardsIgnoreStart Ignore silencing errors is discouraged warning
160                                         @$ret[$this->mAnOption][$userValue]++;
161                                         // @codingStandardsIgnoreEnd
162                                 }
163                         } else {
165                                 foreach ( $defaultOptions as $name => $defaultValue ) {
166                                         $userValue = $user->getOption( $name );
167                                         if ( $userValue <> $defaultValue ) {
168                                                 // @codingStandardsIgnoreStart Ignore silencing errors is discouraged warning
169                                                 @$ret[$name][$userValue]++;
170                                                 // @codingStandardsIgnoreEnd
171                                         }
172                                 }
173                         }
174                 }
176                 foreach ( $ret as $optionName => $usageStats ) {
177                         print "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n";
178                         foreach ( $usageStats as $value => $count ) {
179                                 print " $count user(s): '$value'\n";
180                         }
181                         print "\n";
182                 }
183         }
185         /** Change our users options */
186         private function CHANGER() {
187                 $this->warn();
189                 // We list user by user_id from one of the slave database
190                 $dbr = wfGetDB( DB_SLAVE );
191                 $result = $dbr->select( 'user',
192                         array( 'user_id' ),
193                         array(),
194                         __METHOD__
195                 );
197                 foreach ( $result as $id ) {
199                         $user = User::newFromId( $id->user_id );
201                         $curValue = $user->getOption( $this->mAnOption );
202                         $username = $user->getName();
204                         if ( $curValue == $this->mOldValue ) {
206                                 if ( !$this->mQuiet ) {
207                                         print "Setting {$this->mAnOption} for $username from '{$this->mOldValue}' " .
208                                                 "to '{$this->mNewValue}'): ";
209                                 }
211                                 // Change value
212                                 $user->setOption( $this->mAnOption, $this->mNewValue );
214                                 // Will not save the settings if run with --dry
215                                 if ( !$this->mDry ) {
216                                         $user->saveSettings();
217                                 }
218                                 if ( !$this->mQuiet ) {
219                                         print " OK\n";
220                                 }
221                         } elseif ( !$this->mQuiet ) {
222                                 print "Not changing '$username' using <{$this->mAnOption}> = '$curValue'\n";
223                         }
224                 }
225         }
227         /**
228          * Return an array of option names
229          * @return array
230          */
231         public static function getDefaultOptionsNames() {
232                 $def = User::getDefaultOptions();
233                 $ret = array();
234                 foreach ( $def as $optname => $defaultValue ) {
235                         array_push( $ret, $optname );
236                 }
238                 return $ret;
239         }
241         #
242         # Helper methods
243         #
245         public static function showUsageAndExit() {
246                 print <<<USAGE
248 This script pass through all users and change one of their options.
249 The new option is NOT validated.
251 Usage:
252         php userOptions.php --list
253         php userOptions.php [user option] --usage
254         php userOptions.php [options] <user option> --old <old value> --new <new value>
256 Switchs:
257         --list : list available user options and their default value
259         --usage : report all options statistics or just one if you specify it.
261         --old <old value> : the value to look for
262         --new <new value> : new value to update users with
264 Options:
265         --nowarn: hides the 5 seconds warning
266         --quiet : do not print what is happening
267         --dry   : do not save user settings back to database
269 USAGE;
270                 exit( 0 );
271         }
273         /**
274          * The warning message and countdown
275          * @return bool
276          */
277         public function warn() {
279                 if ( $this->mQuick ) {
280                         return true;
281                 }
283                 print <<<WARN
284 The script is about to change the skin for ALL USERS in the database.
285 Users with option <$this->mAnOption> = '$this->mOldValue' will be made to use '$this->mNewValue'.
287 Abort with control-c in the next five seconds....
288 WARN;
289                 wfCountDown( 5 );
291                 return true;
292         }