2007-01-24 22:13:12 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-09-03 18:10:09 +00:00
|
|
|
* Script to change users preferences on the fly.
|
2007-01-24 22:13:12 +00:00
|
|
|
*
|
|
|
|
|
* Made on an original idea by Fooey (freenode)
|
|
|
|
|
*
|
2010-12-16 19:15:12 +00:00
|
|
|
* 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
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @file
|
|
|
|
|
* @ingroup Maintenance
|
2011-10-24 09:08:13 +00:00
|
|
|
* @author Antoine Musso <hashar at free dot fr>
|
2007-01-24 22:13:12 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-03-16 15:16:18 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-06-02 17:08:13 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
|
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2021-03-16 15:16:18 +00:00
|
|
|
|
2017-10-06 01:53:57 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2007-01-24 22:13:12 +00:00
|
|
|
|
2017-10-06 01:53:57 +00:00
|
|
|
/**
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
class UserOptionsMaintenance extends Maintenance {
|
|
|
|
|
|
2019-10-09 18:41:33 +00:00
|
|
|
public function __construct() {
|
2017-10-06 01:53:57 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
|
|
$this->addDescription( 'Pass through all users and change one of their options.
|
|
|
|
|
The new option is NOT validated.' );
|
|
|
|
|
|
|
|
|
|
$this->addOption( 'list', 'List available user options and their default value' );
|
|
|
|
|
$this->addOption( 'usage', 'Report all options statistics or just one if you specify it' );
|
|
|
|
|
$this->addOption( 'old', 'The value to look for', false, true );
|
2019-10-22 06:52:14 +00:00
|
|
|
$this->addOption( 'new', 'New value to update users with', false, true );
|
2021-06-02 18:09:54 +00:00
|
|
|
$this->addOption( 'fromuserid', 'Start from this user ID when changing options',
|
|
|
|
|
false, true );
|
|
|
|
|
$this->addOption( 'touserid', 'Do not go beyond this user ID when changing options',
|
|
|
|
|
false, true );
|
2017-10-06 01:53:57 +00:00
|
|
|
$this->addOption( 'nowarn', 'Hides the 5 seconds warning' );
|
|
|
|
|
$this->addOption( 'dry', 'Do not save user settings back to database' );
|
|
|
|
|
$this->addArg( 'option name', 'Name of the option to change or provide statistics about', false );
|
2021-06-02 17:08:13 +00:00
|
|
|
$this->setBatchSize( 100 );
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Do the actual work
|
|
|
|
|
*/
|
|
|
|
|
public function execute() {
|
|
|
|
|
if ( $this->hasOption( 'list' ) ) {
|
|
|
|
|
$this->listAvailableOptions();
|
|
|
|
|
} elseif ( $this->hasOption( 'usage' ) ) {
|
|
|
|
|
$this->showUsageStats();
|
|
|
|
|
} elseif ( $this->hasOption( 'old' )
|
|
|
|
|
&& $this->hasOption( 'new' )
|
|
|
|
|
&& $this->hasArg( 0 )
|
|
|
|
|
) {
|
|
|
|
|
$this->updateOptions();
|
|
|
|
|
} else {
|
2020-05-19 22:10:27 +00:00
|
|
|
$this->maybeHelp( true );
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List default options and their value
|
|
|
|
|
*/
|
|
|
|
|
private function listAvailableOptions() {
|
2021-03-16 15:16:18 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
|
$def = $userOptionsLookup->getDefaultOptions();
|
2017-10-06 01:53:57 +00:00
|
|
|
ksort( $def );
|
|
|
|
|
$maxOpt = 0;
|
|
|
|
|
foreach ( $def as $opt => $value ) {
|
|
|
|
|
$maxOpt = max( $maxOpt, strlen( $opt ) );
|
|
|
|
|
}
|
|
|
|
|
foreach ( $def as $opt => $value ) {
|
|
|
|
|
$this->output( sprintf( "%-{$maxOpt}s: %s\n", $opt, $value ) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* List options usage
|
|
|
|
|
*/
|
|
|
|
|
private function showUsageStats() {
|
|
|
|
|
$option = $this->getArg( 0 );
|
|
|
|
|
|
|
|
|
|
$ret = [];
|
2021-03-16 15:16:18 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
|
$defaultOptions = $userOptionsLookup->getDefaultOptions();
|
2017-10-06 01:53:57 +00:00
|
|
|
|
|
|
|
|
// We list user by user_id from one of the replica DBs
|
|
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
|
|
|
|
$result = $dbr->select( 'user',
|
|
|
|
|
[ 'user_id' ],
|
|
|
|
|
[],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ( $result as $id ) {
|
|
|
|
|
$user = User::newFromId( $id->user_id );
|
|
|
|
|
|
|
|
|
|
// Get the options and update stats
|
|
|
|
|
if ( $option ) {
|
|
|
|
|
if ( !array_key_exists( $option, $defaultOptions ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "Invalid user option. Use --list to see valid choices\n" );
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$userValue = $user->getOption( $option );
|
|
|
|
|
if ( $userValue <> $defaultOptions[$option] ) {
|
2019-06-12 12:47:06 +00:00
|
|
|
$ret[$option][$userValue] = ( $ret[$option][$userValue] ?? 0 ) + 1;
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ( $defaultOptions as $name => $defaultValue ) {
|
|
|
|
|
$userValue = $user->getOption( $name );
|
|
|
|
|
if ( $userValue != $defaultValue ) {
|
2019-09-10 08:46:46 +00:00
|
|
|
$ret[$name][$userValue] = ( $ret[$name][$userValue] ?? 0 ) + 1;
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach ( $ret as $optionName => $usageStats ) {
|
|
|
|
|
$this->output( "Usage for <$optionName> (default: '{$defaultOptions[$optionName]}'):\n" );
|
|
|
|
|
foreach ( $usageStats as $value => $count ) {
|
|
|
|
|
$this->output( " $count user(s): '$value'\n" );
|
|
|
|
|
}
|
|
|
|
|
print "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Change our users options
|
|
|
|
|
*/
|
|
|
|
|
private function updateOptions() {
|
|
|
|
|
$dryRun = $this->hasOption( 'dry' );
|
2021-06-02 17:08:13 +00:00
|
|
|
$settingWord = $dryRun ? 'Would set' : 'Setting';
|
2017-10-06 01:53:57 +00:00
|
|
|
$option = $this->getArg( 0 );
|
|
|
|
|
$from = $this->getOption( 'old' );
|
|
|
|
|
$to = $this->getOption( 'new' );
|
|
|
|
|
|
|
|
|
|
if ( !$dryRun ) {
|
|
|
|
|
$this->warn( $option, $from, $to );
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-02 17:08:13 +00:00
|
|
|
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
|
2017-10-06 01:53:57 +00:00
|
|
|
$dbr = wfGetDB( DB_REPLICA );
|
2021-06-02 18:09:54 +00:00
|
|
|
// The fromuserid parameter is inclusive, but iterating is easier with an exclusive
|
|
|
|
|
// range so convert it.
|
|
|
|
|
$fromUserId = (int)$this->getOption( 'fromuserid', 1 ) - 1;
|
|
|
|
|
$toUserId = (int)$this->getOption( 'touserid', 0 ) ?: null;
|
2021-06-02 17:08:13 +00:00
|
|
|
$queryBuilderTemplate = new SelectQueryBuilder( $dbr );
|
|
|
|
|
$queryBuilderTemplate
|
|
|
|
|
->table( 'user' )
|
|
|
|
|
->join( 'user_properties', null, [
|
|
|
|
|
'user_id = up_user',
|
|
|
|
|
'up_property' => $option,
|
|
|
|
|
] )
|
|
|
|
|
->fields( [ 'user_id', 'user_name' ] )
|
|
|
|
|
// up_value is unindexed so this can be slow, but should be acceptable in a script
|
|
|
|
|
->where( [ 'up_value' => $from ] )
|
|
|
|
|
// need to order by ID so we can use ID ranges for query continuation
|
2021-06-02 18:09:54 +00:00
|
|
|
// also needed for the fromuserid / touserid parameters to work
|
2021-06-02 17:08:13 +00:00
|
|
|
->orderBy( 'user_id', SelectQueryBuilder::SORT_ASC )
|
|
|
|
|
->limit( $this->getBatchSize() )
|
|
|
|
|
->caller( __METHOD__ );
|
2021-06-02 18:09:54 +00:00
|
|
|
if ( $toUserId ) {
|
|
|
|
|
$queryBuilderTemplate->andWhere( "user_id <= $toUserId " );
|
|
|
|
|
}
|
2021-06-02 17:08:13 +00:00
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
$queryBuilder = clone $queryBuilderTemplate;
|
2021-06-02 18:09:54 +00:00
|
|
|
$queryBuilder->andWhere( "user_id > $fromUserId" );
|
2021-06-02 17:08:13 +00:00
|
|
|
$result = $queryBuilder->fetchResultSet();
|
|
|
|
|
foreach ( $result as $row ) {
|
|
|
|
|
$this->output( "$settingWord {$option} for {$row->user_name} from '{$from}' to '{$to}'\n" );
|
|
|
|
|
$user = UserIdentityValue::newRegistered( $row->user_id, $row->user_name );
|
2017-10-06 01:53:57 +00:00
|
|
|
if ( !$dryRun ) {
|
2021-06-02 17:08:13 +00:00
|
|
|
$userOptionsManager->setOption( $user, $option, $to );
|
|
|
|
|
$userOptionsManager->saveOptions( $user );
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
2021-06-02 18:09:54 +00:00
|
|
|
$fromUserId = (int)$row->user_id;
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
2021-06-02 17:08:13 +00:00
|
|
|
$this->waitForReplication();
|
|
|
|
|
} while ( $result->numRows() );
|
2017-10-06 01:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The warning message and countdown
|
|
|
|
|
*
|
|
|
|
|
* @param string $option
|
|
|
|
|
* @param string $from
|
|
|
|
|
* @param string $to
|
|
|
|
|
*/
|
|
|
|
|
private function warn( $option, $from, $to ) {
|
|
|
|
|
if ( $this->hasOption( 'nowarn' ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->output( <<<WARN
|
|
|
|
|
The script is about to change the options for ALL USERS in the database.
|
|
|
|
|
Users with option <$option> = '$from' will be made to use '$to'.
|
2007-01-24 22:13:12 +00:00
|
|
|
|
2017-10-06 01:53:57 +00:00
|
|
|
Abort with control-c in the next five seconds....
|
|
|
|
|
WARN
|
|
|
|
|
);
|
|
|
|
|
$this->countDown( 5 );
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-24 22:13:12 +00:00
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = UserOptionsMaintenance::class;
|
2021-01-08 02:16:02 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|