Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateCucComment
0.00% covered (danger)
0.00%
0 / 73
0.00% covered (danger)
0.00%
0 / 3
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 62
0.00% covered (danger)
0.00%
0 / 1
72
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\CheckUser\Maintenance;
22
23use LoggedUpdateMaintenance;
24use MediaWiki\MediaWikiServices;
25
26$IP = getenv( 'MW_INSTALL_PATH' );
27if ( $IP === false ) {
28    $IP = __DIR__ . '/../../..';
29}
30require_once "$IP/maintenance/Maintenance.php";
31
32/**
33 * Maintenance script for filling up cuc_comment_id.
34 *
35 * @author Zabe
36 */
37class PopulateCucComment extends LoggedUpdateMaintenance {
38
39    public function __construct() {
40        parent::__construct();
41        $this->requireExtension( 'CheckUser' );
42        $this->addDescription( 'Populate the cuc_comment_id column.' );
43        $this->addOption(
44            'sleep',
45            'Sleep time (in seconds) between every batch. Default: 0',
46            false,
47            true
48        );
49        $this->addOption( 'start', 'Start after this cuc_id', false, true );
50    }
51
52    /**
53     * @inheritDoc
54     */
55    protected function getUpdateKey() {
56        return 'PopulateCucComment';
57    }
58
59    /**
60     * @inheritDoc
61     */
62    protected function doDBUpdates() {
63        $services = MediaWikiServices::getInstance();
64        $commentStore = $services->getCommentStore();
65        $mainLb = $services->getDBLoadBalancerFactory()->getMainLB();
66        $dbr = $mainLb->getConnection( DB_REPLICA, 'vslow' );
67        $dbw = $mainLb->getMaintenanceConnectionRef( DB_PRIMARY );
68        $batchSize = $this->getBatchSize();
69
70        $start = (int)$this->getOption( 'start', 0 );
71
72        if ( $start > 0 ) {
73            $prevId = $start;
74        } else {
75            $prevId = (int)$dbr->newSelectQueryBuilder()
76                ->field( 'MIN(cuc_id)' )
77                ->table( 'cu_changes' )
78                ->caller( __METHOD__ )
79                ->fetchField();
80        }
81        $curId = $prevId + $batchSize;
82        $maxId = (int)$dbr->newSelectQueryBuilder()
83            ->field( 'MAX(cuc_id)' )
84            ->table( 'cu_changes' )
85            ->caller( __METHOD__ )
86            ->fetchField();
87
88        if ( !$maxId ) {
89            $this->output( "The cu_changes table seems to be empty.\n" );
90            return true;
91        }
92
93        if ( !$dbw->fieldExists( 'cu_changes', 'cuc_comment' ) ) {
94            $this->output( "cuc_comment has already been dropped.\n" );
95            return true;
96        }
97
98        $this->output( "Populating the cuc_comment_id column...\n" );
99
100        $diff = $maxId - $prevId;
101        if ( $batchSize > $diff ) {
102            $batchSize = $diff;
103        }
104        $failed = 0;
105        $sleep = (int)$this->getOption( 'sleep', 0 );
106
107        do {
108            $res = $dbr->newSelectQueryBuilder()
109                ->fields( [ 'cuc_id', 'cuc_comment' ] )
110                ->table( 'cu_changes' )
111                ->conds( [
112                    'cuc_comment_id' => 0,
113                    $dbr->expr( 'cuc_id', '>=', $prevId ),
114                    $dbr->expr( 'cuc_id', '<=', $curId ),
115                ] )
116                ->caller( __METHOD__ )
117                ->fetchResultSet();
118
119            foreach ( $res as $row ) {
120                $commentId = $commentStore->createComment( $dbw, $row->cuc_comment )->id;
121
122                if ( !$commentId ) {
123                    $failed++;
124                    continue;
125                }
126
127                $dbw->newUpdateQueryBuilder()
128                    ->update( 'cu_changes' )
129                    ->set( [ 'cuc_comment_id' => $commentId ] )
130                    ->where( [ 'cuc_id' => $row->cuc_id ] )
131                    ->caller( __METHOD__ )
132                    ->execute();
133            }
134
135            $this->waitForReplication();
136
137            if ( $sleep > 0 ) {
138                sleep( $sleep );
139            }
140
141            $this->output( "Processed $batchSize rows out of $diff.\n" );
142
143            $prevId = $curId;
144            $curId += $batchSize;
145        } while ( $prevId <= $maxId );
146
147        $this->output( "Done. Migration failed for $failed row(s).\n" );
148        return true;
149    }
150}
151
152$maintClass = PopulateCucComment::class;
153require_once RUN_MAINTENANCE_IF_MAIN;