2017-09-22 20:08:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace LoginNotify\Maintenance;
|
|
|
|
|
|
|
|
use BatchRowIterator;
|
|
|
|
use LoggedUpdateMaintenance;
|
|
|
|
use MediaWiki\MediaWikiServices;
|
2023-04-25 09:54:12 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2017-09-22 20:08:39 +00:00
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use User;
|
|
|
|
|
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
if ( $IP === false ) {
|
|
|
|
$IP = __DIR__ . '/../../..';
|
|
|
|
}
|
|
|
|
|
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans up old preference values
|
|
|
|
*/
|
|
|
|
class MigratePreferences extends LoggedUpdateMaintenance {
|
2018-03-27 18:07:54 +00:00
|
|
|
|
2017-09-22 20:08:39 +00:00
|
|
|
// Previously, these constants were used by Hooks to force different per-user defaults
|
2020-05-19 23:22:40 +00:00
|
|
|
private const OPTIONS_FAKE_TRUTH = 2;
|
|
|
|
private const OPTIONS_FAKE_FALSE = 'fake-false';
|
2017-09-22 20:08:39 +00:00
|
|
|
|
2020-12-17 19:55:01 +00:00
|
|
|
/** @var bool[] */
|
2017-09-22 20:08:39 +00:00
|
|
|
private static $mapping = [
|
|
|
|
self::OPTIONS_FAKE_FALSE => false,
|
|
|
|
self::OPTIONS_FAKE_TRUTH => true,
|
|
|
|
];
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
$this->addDescription( 'Cleans up old-style preferences used by LoginNotify' );
|
|
|
|
$this->setBatchSize( 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Do the actual work. All child classes will need to implement this.
|
|
|
|
* Return true to log the update as done or false (usually on failure).
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function doDBUpdates() {
|
|
|
|
$dbr = $this->getDB( DB_REPLICA, 'vslow' );
|
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
|
|
|
|
|
|
$iterator = new BatchRowIterator( $dbr,
|
|
|
|
[ 'user_properties', 'user' ],
|
|
|
|
[ 'up_user', 'up_property' ],
|
2017-11-08 03:38:00 +00:00
|
|
|
$this->getBatchSize()
|
2017-09-22 20:08:39 +00:00
|
|
|
);
|
|
|
|
$iterator->addConditions( [
|
|
|
|
'user_id=up_user',
|
|
|
|
'up_property' => [
|
|
|
|
'echo-subscriptions-web-login-fail',
|
|
|
|
'echo-subscriptions-web-login-success',
|
|
|
|
'echo-subscriptions-email-login-fail',
|
|
|
|
'echo-subscriptions-email-login-success',
|
|
|
|
],
|
|
|
|
'up_value' => [
|
|
|
|
self::OPTIONS_FAKE_TRUTH,
|
|
|
|
self::OPTIONS_FAKE_FALSE,
|
|
|
|
],
|
|
|
|
] );
|
|
|
|
$iterator->setFetchColumns( [ '*' ] );
|
2020-09-04 17:30:20 +00:00
|
|
|
$iterator->setCaller( __METHOD__ );
|
2017-09-22 20:08:39 +00:00
|
|
|
|
|
|
|
$lastRow = (object)[ 'user_id' => 0 ];
|
|
|
|
$optionsToUpdate = [];
|
|
|
|
$rows = 0;
|
|
|
|
$total = 0;
|
|
|
|
$iterator = new RecursiveIteratorIterator( $iterator );
|
|
|
|
foreach ( $iterator as $row ) {
|
|
|
|
$userId = $row->user_id;
|
|
|
|
$option = $row->up_property;
|
|
|
|
$value = $row->up_value;
|
|
|
|
|
|
|
|
if ( $userId != $lastRow->user_id ) {
|
|
|
|
$rows += $this->updateUser( $lastRow, $optionsToUpdate );
|
2017-11-08 03:38:00 +00:00
|
|
|
if ( $rows >= $this->getBatchSize() ) {
|
2017-09-22 20:08:39 +00:00
|
|
|
$this->output( " Updated {$rows} rows up to user ID {$lastRow->user_id}\n" );
|
2021-12-21 01:22:12 +00:00
|
|
|
$lbFactory->waitForReplication( [ 'wiki' => WikiMap::getCurrentWikiId() ] );
|
2017-09-22 20:08:39 +00:00
|
|
|
$total += $rows;
|
|
|
|
$rows = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( isset( self::$mapping[ $value ] ) ) {
|
|
|
|
$optionsToUpdate[$option] = self::$mapping[ $value ];
|
|
|
|
}
|
|
|
|
$lastRow = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
$total += $this->updateUser( $lastRow, $optionsToUpdate );
|
|
|
|
|
|
|
|
$this->output( "{$total} rows updated.\n" );
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update one user's preferences
|
|
|
|
*
|
2020-12-17 19:55:01 +00:00
|
|
|
* @param \stdClass $userRow Row from the user table
|
2020-02-29 20:28:35 +00:00
|
|
|
* @param array &$options Associative array of preference => value
|
2017-09-22 20:08:39 +00:00
|
|
|
* @return int Number of options updated
|
|
|
|
*/
|
|
|
|
private function updateUser( $userRow, array &$options ) {
|
|
|
|
if ( $userRow->user_id && $options ) {
|
|
|
|
$user = User::newFromRow( $userRow );
|
2021-04-02 19:06:06 +00:00
|
|
|
$userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager();
|
2017-09-22 20:08:39 +00:00
|
|
|
foreach ( $options as $option => $value ) {
|
2021-04-02 19:06:06 +00:00
|
|
|
$userOptionsManager->setOption( $user, $option, $value );
|
2017-09-22 20:08:39 +00:00
|
|
|
}
|
2021-04-02 19:06:06 +00:00
|
|
|
$userOptionsManager->saveOptions( $user );
|
2017-09-22 20:08:39 +00:00
|
|
|
}
|
|
|
|
$count = count( $options );
|
|
|
|
$options = [];
|
|
|
|
return $count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the update key name to go in the update log table
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getUpdateKey() {
|
|
|
|
return 'LoginNotify::migratePreferences';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maintClass = MigratePreferences::class;
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|