summaryrefslogtreecommitdiff
path: root/maintenance/userOptions.inc
blob: 51da80d34409fb22be15937ef26885d6cdf0d4ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php
/**
 * Helper class for userOptions.php script.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 * @ingroup Maintenance
 */

// Options we will use
$options = array( 'list', 'nowarn', 'quiet', 'usage', 'dry' );
$optionsWithArgs = array( 'old', 'new' );

require_once __DIR__ . '/commandLine.inc';

/**
 * @ingroup Maintenance
 */
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
	 *
	 * @param $opts array
	 * @param $args array
	 *
	 * @return bool
	 */
	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
	 *
	 * @param $opts array
	 * @param $args array
	 *
	 * @return bool
	 */
	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 } ();
		return true;
	}

	#
	# 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__
			);

		foreach ( $result as $id ) {

			$user = User::newFromId( $id->user_id );

			// Get the options and update stats
			if ( $this->mAnOption ) {

				if ( !array_key_exists( $this->mAnOption, $defaultOptions ) ) {
					print "Invalid user option. Use --list to see valid choices\n";
					exit;
				}

				$userValue = $user->getOption( $this->mAnOption );
				if ( $userValue <> $defaultOptions[$this->mAnOption] ) {
					@$ret[$this->mAnOption][$userValue]++;
				}

			} else {

				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__
			);

		foreach ( $result as $id ) {

			$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
	 * @return array
	 */
	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 : report all options statistics or just one if you specify it.

	--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
	 * @return bool
	 */
	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;
		wfCountDown( 5 );
		return true;
	}

}