Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
MigrateRevisionCommentTemp
0.00% covered (danger)
0.00%
0 / 56
0.00% covered (danger)
0.00%
0 / 3
90
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 / 45
0.00% covered (danger)
0.00%
0 / 1
56
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
21require_once __DIR__ . '/Maintenance.php';
22
23/**
24 * Maintenance script that merges the revision_comment_temp table into the
25 * revision table.
26 *
27 * @ingroup Maintenance
28 * @since 1.40
29 */
30class MigrateRevisionCommentTemp extends LoggedUpdateMaintenance {
31    public function __construct() {
32        parent::__construct();
33        $this->addDescription(
34            'Copy the data from the revision_comment_temp into the revision table'
35        );
36        $this->addOption(
37            'sleep',
38            'Sleep time (in seconds) between every batch. Default: 0',
39            false,
40            true
41        );
42    }
43
44    protected function getUpdateKey() {
45        return __CLASS__;
46    }
47
48    protected function doDBUpdates() {
49        $batchSize = $this->getBatchSize();
50
51        $dbw = $this->getDB( DB_PRIMARY );
52        if ( !$dbw->fieldExists( 'revision', 'rev_comment_id', __METHOD__ ) ) {
53            $this->output( "Run update.php to create rev_comment_id.\n" );
54            return false;
55        }
56        if ( !$dbw->tableExists( 'revision_comment_temp', __METHOD__ ) ) {
57            $this->output( "revision_comment_temp does not exist, so nothing to do.\n" );
58            return true;
59        }
60
61        $this->output( "Merging the revision_comment_temp table into the revision table...\n" );
62        $conds = [];
63        $updated = 0;
64        $sleep = (int)$this->getOption( 'sleep', 0 );
65        while ( true ) {
66            $res = $dbw->newSelectQueryBuilder()
67                ->select( [ 'rev_id', 'revcomment_comment_id' ] )
68                ->from( 'revision' )
69                ->join( 'revision_comment_temp', null, 'rev_id=revcomment_rev' )
70                ->where( [ 'rev_comment_id' => 0 ] )
71                ->andWhere( $conds )
72                ->limit( $batchSize )
73                ->orderBy( 'rev_id' )
74                ->caller( __METHOD__ )
75                ->fetchResultSet();
76
77            $numRows = $res->numRows();
78
79            $last = null;
80            foreach ( $res as $row ) {
81                $last = $row->rev_id;
82                $dbw->newUpdateQueryBuilder()
83                    ->update( 'revision' )
84                    ->set( [ 'rev_comment_id' => $row->revcomment_comment_id ] )
85                    ->where( [ 'rev_id' => $row->rev_id ] )
86                    ->caller( __METHOD__ )->execute();
87                $updated += $dbw->affectedRows();
88            }
89
90            if ( $numRows < $batchSize ) {
91                // We must have reached the end
92                break;
93            }
94
95            // @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
96            $this->output( "... rev_id=$last, updated $updated\n" );
97            $conds = [ $dbw->expr( 'rev_id', '>', $last ) ];
98
99            // Sleep between batches for replication to catch up
100            $this->waitForReplication();
101            if ( $sleep > 0 ) {
102                sleep( $sleep );
103            }
104        }
105        $this->output(
106            "Completed merge of revision_comment_temp into the revision table, "
107            . "$updated rows updated.\n"
108        );
109        return true;
110    }
111}
112
113$maintClass = MigrateRevisionCommentTemp::class;
114require_once RUN_MAINTENANCE_IF_MAIN;