Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateForMultipleDevicesSupport
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 3
72
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 / 52
0.00% covered (danger)
0.00%
0 / 1
42
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\OATHAuth\Maintenance;
22
23use LoggedUpdateMaintenance;
24use MediaWiki\Extension\OATHAuth\OATHAuthServices;
25use MediaWiki\Json\FormatJson;
26use MediaWiki\MediaWikiServices;
27
28if ( getenv( 'MW_INSTALL_PATH' ) ) {
29    $IP = getenv( 'MW_INSTALL_PATH' );
30} else {
31    $IP = __DIR__ . '/../../..';
32}
33require_once "$IP/maintenance/Maintenance.php";
34
35/**
36 * @author Taavi Väänänen <hi@taavi.wtf>
37 */
38class UpdateForMultipleDevicesSupport extends LoggedUpdateMaintenance {
39    public function __construct() {
40        parent::__construct();
41        $this->requireExtension( 'OATHAuth' );
42        $this->setBatchSize( 500 );
43    }
44
45    protected function doDBUpdates() {
46        $dbw = MediaWikiServices::getInstance()
47            ->getDBLoadBalancerFactory()
48            ->getPrimaryDatabase( 'virtual-oathauth' );
49
50        $maxId = $dbw->newSelectQueryBuilder()
51            ->select( 'MAX(id)' )
52            ->from( 'oathauth_users' )
53            ->caller( __METHOD__ )
54            ->fetchField();
55
56        $typeIds = OATHAuthServices::getInstance()->getModuleRegistry()->getModuleIds();
57
58        $updated = 0;
59
60        for ( $min = 0; $min <= $maxId; $min += $this->getBatchSize() ) {
61            $max = $min + $this->getBatchSize();
62            $this->output( "Now processing rows with id between $min and $max... (updated $updated users so far)\n" );
63
64            $res = $dbw->newSelectQueryBuilder()
65                ->select( [
66                    'id',
67                    'module',
68                    'data',
69                ] )
70                ->from( 'oathauth_users' )
71                ->leftJoin(
72                    'oathauth_devices',
73                    null,
74                    'oad_user = id'
75                )
76                ->where( [
77                    $dbw->buildComparison( '>=', [ 'id' => $min ] ),
78                    $dbw->buildComparison( '<', [ 'id' => $max ] ),
79
80                    // Only select rows that haven't been migrated yet, so no matching
81                    // oathauth_devices row.
82                    'oad_id' => null,
83                ] )
84                ->caller( __METHOD__ )
85                ->fetchResultSet();
86
87            $toAdd = [];
88
89            foreach ( $res as $row ) {
90                $decodedData = FormatJson::decode( $row->data, true );
91
92                if ( isset( $decodedData['keys'] ) ) {
93                    $updated++;
94
95                    foreach ( $decodedData['keys'] as $keyData ) {
96                        $toAdd[] = [
97                            'oad_user' => (int)$row->id,
98                            'oad_type' => $typeIds[$row->module],
99                            'oad_data' => FormatJson::encode( $keyData ),
100                        ];
101                    }
102                }
103            }
104
105            if ( $toAdd ) {
106                $dbw->newInsertQueryBuilder()
107                    ->insertInto( 'oathauth_devices' )
108                    ->rows( $toAdd )
109                    ->caller( __METHOD__ )
110                    ->execute();
111            }
112
113            $this->waitForReplication();
114        }
115
116        $this->output( "Done, updated data for $updated users.\n" );
117        return true;
118    }
119
120    /**
121     * @return string
122     */
123    protected function getUpdateKey() {
124        return __CLASS__;
125    }
126}
127
128$maintClass = UpdateForMultipleDevicesSupport::class;
129require_once RUN_MAINTENANCE_IF_MAIN;