Drop DB updates from pre MW 1.35

updateDatabaseValueFormat and updateTOTPToMultipleKeys are from 1.34:
Iaf9facb54cd9693f20ed2f48d22b076c4b626705
I71286534d21d95083436d64d79811943c1a1d032

updateTOTPScratchTokensToArray is from 1.36:
Ie8de059888363bf1cea4f0b268a46faaa5671904

Change-Id: I6de64d95b2e4b132d321b6f8f0129c476ef00f7f
This commit is contained in:
Reedy 2023-12-22 00:33:56 +00:00
parent 809576b671
commit 57505f1976
6 changed files with 6 additions and 247 deletions

View file

@ -1,55 +0,0 @@
<?php
/**
* Converts old, TOTP specific, column values to new structure
*
* Usage: php updateDatabaseValueFormat.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
* @author Dejan Savuljesku
* @ingroup Maintenance
*/
use MediaWiki\Extension\OATHAuth\Hook\UpdateTables;
if ( getenv( 'MW_INSTALL_PATH' ) ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class UpdateDatabaseValueFormat extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Script to convert old, TOTP specific, column values to a newer structure' );
$this->requireExtension( 'OATHAuth' );
}
/**
* @throws ConfigException
*/
public function execute() {
if ( !UpdateTables::convertToGenericFields() ) {
$this->fatalError( "Failed to update the data structure rows.\n" );
}
$this->output( "Done.\n" );
}
}
$maintClass = UpdateDatabaseValueFormat::class;
require_once RUN_MAINTENANCE_IF_MAIN;

View file

@ -32,6 +32,9 @@ if ( getenv( 'MW_INSTALL_PATH' ) ) {
}
require_once "$IP/maintenance/Maintenance.php";
/**
* Merged December 2020; part of REL1_36
*/
class UpdateTOTPScratchTokensToArray extends Maintenance {
public function __construct() {
parent::__construct();

View file

@ -1,52 +0,0 @@
<?php
/**
* Update old single-key setup to multiple-keys
*
* Usage: php updateTOTPToMultipleKeys.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
* @author Dejan Savuljesku
* @ingroup Maintenance
*/
use MediaWiki\Extension\OATHAuth\Hook\UpdateTables;
if ( getenv( 'MW_INSTALL_PATH' ) ) {
$IP = getenv( 'MW_INSTALL_PATH' );
} else {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class UpdateTOTPToMultipleKeys extends Maintenance {
public function __construct() {
parent::__construct();
$this->addDescription( 'Script to update single TOTP keys to multi-key environment' );
$this->requireExtension( 'OATHAuth' );
}
public function execute() {
if ( !UpdateTables::switchTOTPToMultipleKeys() ) {
$this->fatalError( "Failed to update TOTP keys.\n" );
}
$this->output( "Done.\n" );
}
}
$maintClass = UpdateTOTPToMultipleKeys::class;
require_once RUN_MAINTENANCE_IF_MAIN;

View file

@ -1,3 +0,0 @@
ALTER TABLE /*_*/oathauth_users
ADD module VARCHAR( 255 ) NOT NULL,
ADD data BLOB NULL;

View file

@ -1,3 +0,0 @@
ALTER TABLE /*_*/oathauth_users
DROP COLUMN secret,
DROP COLUMN scratch_tokens;

View file

@ -31,26 +31,7 @@ class UpdateTables implements LoadExtensionSchemaUpdatesHook {
switch ( $type ) {
case 'mysql':
case 'sqlite':
// 1.34
$updater->addExtensionField(
'oathauth_users',
'module',
"$typePath/patch-add_generic_fields.sql"
);
$updater->addExtensionUpdate(
[ [ __CLASS__, 'schemaUpdateSubstituteForGenericFields' ] ]
);
$updater->dropExtensionField(
'oathauth_users',
'secret',
"$typePath/patch-remove_module_specific_fields.sql"
);
$updater->addExtensionUpdate(
[ [ __CLASS__, 'schemaUpdateTOTPToMultipleKeys' ] ]
);
// 1.36
$updater->addExtensionUpdate(
[ [ __CLASS__, 'schemaUpdateTOTPScratchTokensToArray' ] ]
);
@ -89,26 +70,6 @@ class UpdateTables implements LoadExtensionSchemaUpdatesHook {
return $db;
}
/**
* Helper function for converting old, TOTP specific, column values to new structure
* @param DatabaseUpdater $updater
* @return bool
* @throws ConfigException
*/
public static function schemaUpdateSubstituteForGenericFields( DatabaseUpdater $updater ) {
return self::convertToGenericFields();
}
/**
* Helper function for converting single TOTP keys to the multi-key system
* @param DatabaseUpdater $updater
* @return bool
* @throws ConfigException
*/
public static function schemaUpdateTOTPToMultipleKeys( DatabaseUpdater $updater ) {
return self::switchTOTPToMultipleKeys();
}
/**
* Helper function for converting single TOTP keys to the multi-key system
* @param DatabaseUpdater $updater
@ -119,103 +80,11 @@ class UpdateTables implements LoadExtensionSchemaUpdatesHook {
return self::switchTOTPScratchTokensToArray();
}
/**
* Converts old, TOTP specific, column values to a newer structure
* @return bool
* @throws ConfigException
*/
public static function convertToGenericFields() {
$db = self::getDatabase();
if ( !$db->fieldExists( 'oathauth_users', 'secret', __METHOD__ ) ) {
return true;
}
$services = MediaWikiServices::getInstance();
$batchSize = $services->getMainConfig()->get( 'UpdateRowsPerQuery' );
$lbFactory = $services->getDBLoadBalancerFactory();
while ( true ) {
$lbFactory->waitForReplication();
$res = $db->newSelectQueryBuilder()
->select( [ 'id', 'secret', 'scratch_tokens' ] )
->from( 'oathauth_users' )
->where( [
'module' => '',
'data IS NULL',
'secret IS NOT NULL'
] )
->limit( $batchSize )
->caller( __METHOD__ )
->fetchResultSet();
if ( $res->numRows() === 0 ) {
return true;
}
foreach ( $res as $row ) {
$db->newUpdateQueryBuilder()
->update( 'oathauth_users' )
->set( [
'module' => 'totp',
'data' => FormatJson::encode( [
'keys' => [ [
'secret' => $row->secret,
'scratch_tokens' => $row->scratch_tokens
] ]
] )
] )
->where( [ 'id' => $row->id ] )
->caller( __METHOD__ )
->execute();
}
}
}
/**
* Switch from using single keys to multi-key support
*
* @return bool
* @throws ConfigException
*/
public static function switchTOTPToMultipleKeys() {
$db = self::getDatabase();
if ( !$db->fieldExists( 'oathauth_users', 'data', __METHOD__ ) ) {
return true;
}
$res = $db->newSelectQueryBuilder()
->select( [ 'id', 'data' ] )
->from( 'oathauth_users' )
->where( [ 'module' => 'totp' ] )
->caller( __METHOD__ )
->fetchResultSet();
foreach ( $res as $row ) {
$data = FormatJson::decode( $row->data, true );
if ( isset( $data['keys'] ) ) {
continue;
}
$db->newUpdateQueryBuilder()
->update( 'oathauth_users' )
->set( [
'data' => FormatJson::encode( [
'keys' => [ $data ]
] )
] )
->where( [ 'id' => $row->id ] )
->caller( __METHOD__ )
->execute();
}
return true;
}
/**
* Switch scratch tokens from string to an array
*
* @since 1.36
*
* @return bool
* @throws ConfigException
*/