From 57505f1976ab0a54ee52d2317dd8c1d3d8e99f34 Mon Sep 17 00:00:00 2001 From: Reedy Date: Fri, 22 Dec 2023 00:33:56 +0000 Subject: [PATCH] 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 --- maintenance/updateDatabaseValueFormat.php | 55 ------- .../updateTOTPScratchTokensToArray.php | 3 + maintenance/updateTOTPToMultipleKeys.php | 52 ------- sql/mysql/patch-add_generic_fields.sql | 3 - .../patch-remove_module_specific_fields.sql | 3 - src/Hook/UpdateTables.php | 137 +----------------- 6 files changed, 6 insertions(+), 247 deletions(-) delete mode 100644 maintenance/updateDatabaseValueFormat.php delete mode 100644 maintenance/updateTOTPToMultipleKeys.php delete mode 100644 sql/mysql/patch-add_generic_fields.sql delete mode 100644 sql/mysql/patch-remove_module_specific_fields.sql diff --git a/maintenance/updateDatabaseValueFormat.php b/maintenance/updateDatabaseValueFormat.php deleted file mode 100644 index 1a47928a..00000000 --- a/maintenance/updateDatabaseValueFormat.php +++ /dev/null @@ -1,55 +0,0 @@ -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; diff --git a/maintenance/updateTOTPScratchTokensToArray.php b/maintenance/updateTOTPScratchTokensToArray.php index b23c78f6..98e7774b 100644 --- a/maintenance/updateTOTPScratchTokensToArray.php +++ b/maintenance/updateTOTPScratchTokensToArray.php @@ -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(); diff --git a/maintenance/updateTOTPToMultipleKeys.php b/maintenance/updateTOTPToMultipleKeys.php deleted file mode 100644 index efc4cb74..00000000 --- a/maintenance/updateTOTPToMultipleKeys.php +++ /dev/null @@ -1,52 +0,0 @@ -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; diff --git a/sql/mysql/patch-add_generic_fields.sql b/sql/mysql/patch-add_generic_fields.sql deleted file mode 100644 index 5772e5ff..00000000 --- a/sql/mysql/patch-add_generic_fields.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE /*_*/oathauth_users - ADD module VARCHAR( 255 ) NOT NULL, - ADD data BLOB NULL; diff --git a/sql/mysql/patch-remove_module_specific_fields.sql b/sql/mysql/patch-remove_module_specific_fields.sql deleted file mode 100644 index accbc79e..00000000 --- a/sql/mysql/patch-remove_module_specific_fields.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE /*_*/oathauth_users - DROP COLUMN secret, - DROP COLUMN scratch_tokens; diff --git a/src/Hook/UpdateTables.php b/src/Hook/UpdateTables.php index 66b03cb3..d4160450 100644 --- a/src/Hook/UpdateTables.php +++ b/src/Hook/UpdateTables.php @@ -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 */