Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[lhc/web/wiklou.git] / maintenance / userOptions.php
1 <?php
2 /**
3 * Script to change users preferences on the fly.
4 *
5 * Made on an original idea by Fooey (freenode)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 * @author Antoine Musso <hashar at free dot fr>
25 */
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * @ingroup Maintenance
31 */
32 class UserOptionsMaintenance extends Maintenance {
33
34 function __construct() {
35 parent::__construct();
36
37 $this->addDescription( 'Pass through all users and change one of their options.
38 The new option is NOT validated.' );
39
40 $this->addOption( 'list', 'List available user options and their default value' );
41 $this->addOption( 'usage', 'Report all options statistics or just one if you specify it' );
42 $this->addOption( 'old', 'The value to look for', false, true );
43 $this->addOption( 'new', 'Rew value to update users with', false, true );
44 $this->addOption( 'nowarn', 'Hides the 5 seconds warning' );
45 $this->addOption( 'dry', 'Do not save user settings back to database' );
46 $this->addArg( 'option name', 'Name of the option to change or provide statistics about', false );
47 }
48
49 /**
50 * Do the actual work
51 */
52 public function execute() {
53 if ( $this->hasOption( 'list' ) ) {
54 $this->listAvailableOptions();
55 } elseif ( $this->hasOption( 'usage' ) ) {
56 $this->showUsageStats();
57 } elseif ( $this->hasOption( 'old' )
58 && $this->hasOption( 'new' )
59 && $this->hasArg( 0 )
60 ) {
61 $this->updateOptions();
62 } else {
63 $this->maybeHelp( /* force = */ true );
64 }
65 }
66
67 /**
68 * List default options and their value
69 */
70 private function listAvailableOptions() {
71 $def = User::getDefaultOptions();
72 ksort( $def );
73 $maxOpt = 0;
74 foreach ( $def as $opt => $value ) {
75 $maxOpt = max( $maxOpt, strlen( $opt ) );
76 }
77 foreach ( $def as $opt => $value ) {
78 $this->output( sprintf( "%-{$maxOpt}s: %s\n", $opt, $value ) );
79 }
80 }
81
82 /**
83 * List options usage
84 */
85 private function showUsageStats() {
86 $option = $this->getArg( 0 );
87
88 $ret = [];
89 $defaultOptions = User::getDefaultOptions();
90
91 // We list user by user_id from one of the replica DBs
92 $dbr = wfGetDB( DB_REPLICA );
93 $result = $dbr->select( 'user',
94 [ 'user_id' ],
95 [],
96 __METHOD__
97 );
98
99 foreach ( $result as $id ) {
100 $user = User::newFromId( $id->user_id );
101
102 // Get the options and update stats
103 if ( $option ) {
104 if ( !array_key_exists( $option, $defaultOptions ) ) {
105 $this->fatalError( "Invalid user option. Use --list to see valid choices\n" );
106 }
107
108 $userValue = $user->getOption( $option );
109 if ( $userValue <> $defaultOptions[$option] ) {
110 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
111 @$ret[$option][$userValue]++;
112 }
113 } else {
114
115 foreach ( $defaultOptions as $name => $defaultValue ) {
116 $userValue = $user->getOption( $name );
117 if ( $userValue != $defaultValue ) {
118 // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged
119 @$ret[$name][$userValue]++;
120 }
121 }
122 }
123 }
124
125 foreach ( $ret as $optionName => $usageStats ) {
126 $this->output( "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n" );
127 foreach ( $usageStats as $value => $count ) {
128 $this->output( " $count user(s): '$value'\n" );
129 }
130 print "\n";
131 }
132 }
133
134 /**
135 * Change our users options
136 */
137 private function updateOptions() {
138 $dryRun = $this->hasOption( 'dry' );
139 $option = $this->getArg( 0 );
140 $from = $this->getOption( 'old' );
141 $to = $this->getOption( 'new' );
142
143 if ( !$dryRun ) {
144 $this->warn( $option, $from, $to );
145 }
146
147 // We list user by user_id from one of the replica DBs
148 // @todo: getting all users in one query does not scale
149 $dbr = wfGetDB( DB_REPLICA );
150 $result = $dbr->select( 'user',
151 [ 'user_id' ],
152 [],
153 __METHOD__
154 );
155
156 foreach ( $result as $id ) {
157 $user = User::newFromId( $id->user_id );
158
159 $curValue = $user->getOption( $option );
160 $username = $user->getName();
161
162 if ( $curValue == $from ) {
163 $this->output( "Setting {$option} for $username from '{$from}' to '{$to}'): " );
164
165 // Change value
166 $user->setOption( $option, $to );
167
168 // Will not save the settings if run with --dry
169 if ( !$dryRun ) {
170 $user->saveSettings();
171 }
172 $this->output( " OK\n" );
173 } else {
174 $this->output( "Not changing '$username' using <{$option}> = '$curValue'\n" );
175 }
176 }
177 }
178
179 /**
180 * The warning message and countdown
181 *
182 * @param string $option
183 * @param string $from
184 * @param string $to
185 */
186 private function warn( $option, $from, $to ) {
187 if ( $this->hasOption( 'nowarn' ) ) {
188 return;
189 }
190
191 $this->output( <<<WARN
192 The script is about to change the options for ALL USERS in the database.
193 Users with option <$option> = '$from' will be made to use '$to'.
194
195 Abort with control-c in the next five seconds....
196 WARN
197 );
198 $this->countDown( 5 );
199 }
200 }
201
202 $maintClass = UserOptionsMaintenance::class;
203 require RUN_MAINTENANCE_IF_MAIN;