addDescription( 'Script to update TOTP Scratch Tokens to an array' ); $this->requireExtension( 'OATHAuth' ); } protected function doDBUpdates() { $dbw = MediaWikiServices::getInstance() ->getDBLoadBalancerFactory() ->getPrimaryDatabase( 'virtual-oathauth' ); $res = $dbw->newSelectQueryBuilder() ->select( [ 'id', 'data' ] ) ->from( 'oathauth_users' ) ->where( [ 'module' => 'totp' ] ) ->caller( __METHOD__ ) ->fetchResultSet(); foreach ( $res as $row ) { $data = FormatJson::decode( $row->data, true ); $updated = false; foreach ( $data['keys'] as &$k ) { if ( is_string( $k['scratch_tokens'] ) ) { $k['scratch_tokens'] = explode( ',', $k['scratch_tokens'] ); $updated = true; } } unset( $k ); if ( !$updated ) { continue; } $dbw->newUpdateQueryBuilder() ->update( 'oathauth_users' ) ->set( [ 'data' => FormatJson::encode( $data ) ] ) ->where( [ 'id' => $row->id ] ) ->caller( __METHOD__ ) ->execute(); } $this->output( "Done.\n" ); return true; } /** * @return string */ protected function getUpdateKey() { return __CLASS__; } } $maintClass = UpdateTOTPScratchTokensToArray::class; require_once RUN_MAINTENANCE_IF_MAIN;