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 FormatJson;
29use 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    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     * @return string
91     */
92    protected function getUpdateKey() {
93        return __CLASS__;
94    }
95}
96
97$maintClass = UpdateTOTPScratchTokensToArray::class;
98require_once RUN_MAINTENANCE_IF_MAIN;