Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateTOTPScratchTokensToArray
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
30
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Updates TOTP Recovery Codes to an array
4 *
5 * Usage: php updateTOTPScratchTokensToArray.php
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @ingroup Maintenance
24 */
25
26namespace MediaWiki\Extension\OATHAuth\Maintenance;
27
28use MediaWiki\Json\FormatJson;
29use MediaWiki\Maintenance\LoggedUpdateMaintenance;
30use MediaWiki\MediaWikiServices;
31
32if ( getenv( 'MW_INSTALL_PATH' ) ) {
33    $IP = getenv( 'MW_INSTALL_PATH' );
34} else {
35    $IP = __DIR__ . '/../../..';
36}
37require_once "$IP/maintenance/Maintenance.php";
38
39/**
40 * Merged December 2020; part of REL1_36
41 */
42class 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    /** @inheritDoc */
50    protected function doDBUpdates() {
51        $dbw = MediaWikiServices::getInstance()
52            ->getDBLoadBalancerFactory()
53            ->getPrimaryDatabase( 'virtual-oathauth' );
54
55        $res = $dbw->newSelectQueryBuilder()
56            ->select( [ 'id', 'data' ] )
57            ->from( 'oathauth_users' )
58            ->where( [ 'module' => 'totp' ] )
59            ->caller( __METHOD__ )
60            ->fetchResultSet();
61
62        foreach ( $res as $row ) {
63            $data = FormatJson::decode( $row->data, true );
64
65            $updated = false;
66            foreach ( $data['keys'] as &$k ) {
67                if ( is_string( $k['scratch_tokens'] ) ) {
68                    $k['scratch_tokens'] = explode( ',', $k['scratch_tokens'] );
69                    $updated = true;
70                }
71            }
72            unset( $k );
73
74            if ( !$updated ) {
75                continue;
76            }
77
78            $dbw->newUpdateQueryBuilder()
79                ->update( 'oathauth_users' )
80                ->set( [ 'data' => FormatJson::encode( $data ) ] )
81                ->where( [ 'id' => $row->id ] )
82                ->caller( __METHOD__ )
83                ->execute();
84        }
85
86        $this->output( "Done.\n" );
87        return true;
88    }
89
90    /**
91     * @return string
92     */
93    protected function getUpdateKey() {
94        return __CLASS__;
95    }
96}
97
98$maintClass = UpdateTOTPScratchTokensToArray::class;
99require_once RUN_MAINTENANCE_IF_MAIN;