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