| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\Extension\OATHAuth\Maintenance; |
| 27 |
|
| 28 |
use MediaWiki\Json\FormatJson; |
| 29 |
use MediaWiki\Maintenance\LoggedUpdateMaintenance; |
| 30 |
use MediaWiki\MediaWikiServices; |
| 31 |
|
| 32 |
if ( getenv( 'MW_INSTALL_PATH' ) ) { |
| 33 |
$IP = getenv( 'MW_INSTALL_PATH' ); |
| 34 |
} else { |
| 35 |
$IP = __DIR__ . '/../../..'; |
| 36 |
} |
| 37 |
require_once "$IP/maintenance/Maintenance.php"; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class UpdateTOTPScratchTokensToArray extends LoggedUpdateMaintenance { |
| 43 |
public function __construct() { |
| 44 |
parent::__construct(); |
| 45 |
$this->addDescription( 'Script to update TOTP Recovery Codes to an array' ); |
| 46 |
$this->requireExtension( 'OATHAuth' ); |
| 47 |
} |
| 48 |
|
| 49 |
protected function doDBUpdates() { |
| 50 |
$dbw = MediaWikiServices::getInstance() |
| 51 |
->getDBLoadBalancerFactory() |
| 52 |
->getPrimaryDatabase( 'virtual-oathauth' ); |
| 53 |
|
| 54 |
$res = $dbw->newSelectQueryBuilder() |
| 55 |
->select( [ 'id', 'data' ] ) |
| 56 |
->from( 'oathauth_users' ) |
| 57 |
->where( [ 'module' => 'totp' ] ) |
| 58 |
->caller( __METHOD__ ) |
| 59 |
->fetchResultSet(); |
| 60 |
|
| 61 |
foreach ( $res as $row ) { |
| 62 |
$data = FormatJson::decode( $row->data, true ); |
| 63 |
|
| 64 |
$updated = false; |
| 65 |
foreach ( $data['keys'] as &$k ) { |
| 66 |
if ( is_string( $k['scratch_tokens'] ) ) { |
| 67 |
$k['scratch_tokens'] = explode( ',', $k['scratch_tokens'] ); |
| 68 |
$updated = true; |
| 69 |
} |
| 70 |
} |
| 71 |
unset( $k ); |
| 72 |
|
| 73 |
if ( !$updated ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
$dbw->newUpdateQueryBuilder() |
| 78 |
->update( 'oathauth_users' ) |
| 79 |
->set( [ 'data' => FormatJson::encode( $data ) ] ) |
| 80 |
->where( [ 'id' => $row->id ] ) |
| 81 |
->caller( __METHOD__ ) |
| 82 |
->execute(); |
| 83 |
} |
| 84 |
|
| 85 |
$this->output( "Done.\n" ); |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
protected function getUpdateKey() { |
| 93 |
return __CLASS__; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$maintClass = UpdateTOTPScratchTokensToArray::class; |
| 98 |
require_once RUN_MAINTENANCE_IF_MAIN; |