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