2022-12-31 23:49:18 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\OATHAuth\Maintenance;
|
|
|
|
|
|
|
|
use MediaWiki\Extension\OATHAuth\OATHAuthServices;
|
2024-07-04 22:08:57 +00:00
|
|
|
use MediaWiki\Json\FormatJson;
|
2024-10-20 08:59:39 +00:00
|
|
|
use MediaWiki\Maintenance\LoggedUpdateMaintenance;
|
2023-10-17 18:39:14 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-12-31 23:49:18 +00:00
|
|
|
|
|
|
|
if ( getenv( 'MW_INSTALL_PATH' ) ) {
|
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
} else {
|
|
|
|
$IP = __DIR__ . '/../../..';
|
|
|
|
}
|
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Taavi Väänänen <hi@taavi.wtf>
|
|
|
|
*/
|
|
|
|
class UpdateForMultipleDevicesSupport extends LoggedUpdateMaintenance {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
|
|
|
$this->requireExtension( 'OATHAuth' );
|
|
|
|
$this->setBatchSize( 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function doDBUpdates() {
|
2023-10-17 18:39:14 +00:00
|
|
|
$dbw = MediaWikiServices::getInstance()
|
|
|
|
->getDBLoadBalancerFactory()
|
|
|
|
->getPrimaryDatabase( 'virtual-oathauth' );
|
2022-12-31 23:49:18 +00:00
|
|
|
|
|
|
|
$maxId = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( 'MAX(id)' )
|
|
|
|
->from( 'oathauth_users' )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchField();
|
|
|
|
|
|
|
|
$typeIds = OATHAuthServices::getInstance()->getModuleRegistry()->getModuleIds();
|
|
|
|
|
|
|
|
$updated = 0;
|
|
|
|
|
|
|
|
for ( $min = 0; $min <= $maxId; $min += $this->getBatchSize() ) {
|
|
|
|
$max = $min + $this->getBatchSize();
|
|
|
|
$this->output( "Now processing rows with id between $min and $max... (updated $updated users so far)\n" );
|
|
|
|
|
|
|
|
$res = $dbw->newSelectQueryBuilder()
|
|
|
|
->select( [
|
|
|
|
'id',
|
|
|
|
'module',
|
|
|
|
'data',
|
|
|
|
] )
|
|
|
|
->from( 'oathauth_users' )
|
2023-08-16 02:54:57 +00:00
|
|
|
->leftJoin(
|
|
|
|
'oathauth_devices',
|
|
|
|
null,
|
|
|
|
'oad_user = id'
|
|
|
|
)
|
2022-12-31 23:49:18 +00:00
|
|
|
->where( [
|
|
|
|
$dbw->buildComparison( '>=', [ 'id' => $min ] ),
|
|
|
|
$dbw->buildComparison( '<', [ 'id' => $max ] ),
|
2023-08-16 02:54:57 +00:00
|
|
|
|
|
|
|
// Only select rows that haven't been migrated yet, so no matching
|
|
|
|
// oathauth_devices row.
|
|
|
|
'oad_id' => null,
|
2022-12-31 23:49:18 +00:00
|
|
|
] )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->fetchResultSet();
|
|
|
|
|
|
|
|
$toAdd = [];
|
|
|
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
$decodedData = FormatJson::decode( $row->data, true );
|
|
|
|
|
2023-08-16 02:54:57 +00:00
|
|
|
if ( isset( $decodedData['keys'] ) ) {
|
2023-10-10 21:42:36 +00:00
|
|
|
$updated++;
|
2022-12-31 23:49:18 +00:00
|
|
|
|
|
|
|
foreach ( $decodedData['keys'] as $keyData ) {
|
|
|
|
$toAdd[] = [
|
|
|
|
'oad_user' => (int)$row->id,
|
|
|
|
'oad_type' => $typeIds[$row->module],
|
|
|
|
'oad_data' => FormatJson::encode( $keyData ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $toAdd ) {
|
2023-11-23 18:30:01 +00:00
|
|
|
$dbw->newInsertQueryBuilder()
|
|
|
|
->insertInto( 'oathauth_devices' )
|
|
|
|
->rows( $toAdd )
|
|
|
|
->caller( __METHOD__ )
|
|
|
|
->execute();
|
2022-12-31 23:49:18 +00:00
|
|
|
}
|
|
|
|
|
2023-10-17 18:39:14 +00:00
|
|
|
$this->waitForReplication();
|
2022-12-31 23:49:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->output( "Done, updated data for $updated users.\n" );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getUpdateKey() {
|
|
|
|
return __CLASS__;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$maintClass = UpdateForMultipleDevicesSupport::class;
|
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|