| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Extension\OATHAuth\Maintenance; |
| 22 |
|
| 23 |
use MediaWiki\Extension\OATHAuth\OATHAuthServices; |
| 24 |
use MediaWiki\Json\FormatJson; |
| 25 |
use MediaWiki\Maintenance\LoggedUpdateMaintenance; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
|
| 28 |
if ( getenv( 'MW_INSTALL_PATH' ) ) { |
| 29 |
$IP = getenv( 'MW_INSTALL_PATH' ); |
| 30 |
} else { |
| 31 |
$IP = __DIR__ . '/../../..'; |
| 32 |
} |
| 33 |
require_once "$IP/maintenance/Maintenance.php"; |
| 34 |
|
| 35 |
|
| 36 |
tf> |
| 37 |
|
| 38 |
class UpdateForMultipleDevicesSupport extends LoggedUpdateMaintenance { |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
$this->requireExtension( 'OATHAuth' ); |
| 42 |
$this->setBatchSize( 500 ); |
| 43 |
} |
| 44 |
|
| 45 |
protected function doDBUpdates() { |
| 46 |
$dbw = MediaWikiServices::getInstance() |
| 47 |
->getDBLoadBalancerFactory() |
| 48 |
->getPrimaryDatabase( 'virtual-oathauth' ); |
| 49 |
|
| 50 |
$maxId = $dbw->newSelectQueryBuilder() |
| 51 |
->select( 'MAX(id)' ) |
| 52 |
->from( 'oathauth_users' ) |
| 53 |
->caller( __METHOD__ ) |
| 54 |
->fetchField(); |
| 55 |
|
| 56 |
$typeIds = OATHAuthServices::getInstance()->getModuleRegistry()->getModuleIds(); |
| 57 |
|
| 58 |
$updated = 0; |
| 59 |
|
| 60 |
for ( $min = 0; $min <= $maxId; $min += $this->getBatchSize() ) { |
| 61 |
$max = $min + $this->getBatchSize(); |
| 62 |
$this->output( "Now processing rows with id between $min and $max... (updated $updated users so far)\n" ); |
| 63 |
|
| 64 |
$res = $dbw->newSelectQueryBuilder() |
| 65 |
->select( [ |
| 66 |
'id', |
| 67 |
'module', |
| 68 |
'data', |
| 69 |
] ) |
| 70 |
->from( 'oathauth_users' ) |
| 71 |
->leftJoin( |
| 72 |
'oathauth_devices', |
| 73 |
null, |
| 74 |
'oad_user = id' |
| 75 |
) |
| 76 |
->where( [ |
| 77 |
$dbw->buildComparison( '>=', [ 'id' => $min ] ), |
| 78 |
$dbw->buildComparison( '<', [ 'id' => $max ] ), |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
'oad_id' => null, |
| 83 |
] ) |
| 84 |
->caller( __METHOD__ ) |
| 85 |
->fetchResultSet(); |
| 86 |
|
| 87 |
$toAdd = []; |
| 88 |
|
| 89 |
foreach ( $res as $row ) { |
| 90 |
$decodedData = FormatJson::decode( $row->data, true ); |
| 91 |
|
| 92 |
if ( isset( $decodedData['keys'] ) ) { |
| 93 |
$updated++; |
| 94 |
|
| 95 |
foreach ( $decodedData['keys'] as $keyData ) { |
| 96 |
$toAdd[] = [ |
| 97 |
'oad_user' => (int)$row->id, |
| 98 |
'oad_type' => $typeIds[$row->module], |
| 99 |
'oad_data' => FormatJson::encode( $keyData ), |
| 100 |
]; |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ( $toAdd ) { |
| 106 |
$dbw->newInsertQueryBuilder() |
| 107 |
->insertInto( 'oathauth_devices' ) |
| 108 |
->rows( $toAdd ) |
| 109 |
->caller( __METHOD__ ) |
| 110 |
->execute(); |
| 111 |
} |
| 112 |
|
| 113 |
$this->waitForReplication(); |
| 114 |
} |
| 115 |
|
| 116 |
$this->output( "Done, updated data for $updated users.\n" ); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected function getUpdateKey() { |
| 124 |
return __CLASS__; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
$maintClass = UpdateForMultipleDevicesSupport::class; |
| 129 |
require_once RUN_MAINTENANCE_IF_MAIN; |