Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 88
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PopulateRevisionLength
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 5
272
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
 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 / 26
0.00% covered (danger)
0.00%
0 / 1
20
 doLenUpdates
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
56
 upgradeRow
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Populates the rev_len and ar_len fields when they are NULL.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24require_once __DIR__ . '/Maintenance.php';
25
26/**
27 * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
28 * This is the case for all revisions created before MW 1.10, as well as those affected
29 * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
30 *
31 * @ingroup Maintenance
32 */
33class PopulateRevisionLength extends LoggedUpdateMaintenance {
34    public function __construct() {
35        parent::__construct();
36        $this->addDescription( 'Populates the rev_len and ar_len fields' );
37        $this->setBatchSize( 200 );
38    }
39
40    protected function getUpdateKey() {
41        return 'populate rev_len and ar_len';
42    }
43
44    public function doDBUpdates() {
45        $dbw = $this->getDB( DB_PRIMARY );
46        if ( !$dbw->tableExists( 'revision', __METHOD__ ) ) {
47            $this->fatalError( "revision table does not exist" );
48        } elseif ( !$dbw->tableExists( 'archive', __METHOD__ ) ) {
49            $this->fatalError( "archive table does not exist" );
50        } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
51            $this->output( "rev_len column does not exist\n\n", true );
52
53            return false;
54        }
55
56        $revisionStore = $this->getServiceContainer()->getRevisionStore();
57
58        $this->output( "Populating rev_len column\n" );
59        $rev = $this->doLenUpdates(
60            'revision',
61            'rev_id',
62            'rev',
63            $revisionStore->newSelectQueryBuilder( $this->getReplicaDB() )->joinComment()
64        );
65
66        $this->output( "Populating ar_len column\n" );
67        $ar = $this->doLenUpdates(
68            'archive',
69            'ar_id',
70            'ar',
71            $revisionStore->newArchiveSelectQueryBuilder( $this->getReplicaDB() )->joinComment()
72        );
73
74        $this->output( "rev_len and ar_len population complete "
75            . "[$rev revision rows, $ar archive rows].\n" );
76
77        return true;
78    }
79
80    /**
81     * @param string $table
82     * @param string $idCol
83     * @param string $prefix
84     * @param \Wikimedia\Rdbms\SelectQueryBuilder $queryBuilder should use a replica db
85     * @return int
86     */
87    protected function doLenUpdates( $table, $idCol, $prefix, $queryBuilder ) {
88        $dbr = $this->getReplicaDB();
89        $dbw = $this->getPrimaryDB();
90        $batchSize = $this->getBatchSize();
91        $start = $dbw->newSelectQueryBuilder()
92            ->select( "MIN($idCol)" )
93            ->from( $table )
94            ->caller( __METHOD__ )->fetchField();
95        $end = $dbw->newSelectQueryBuilder()
96            ->select( "MAX($idCol)" )
97            ->from( $table )
98            ->caller( __METHOD__ )->fetchField();
99        if ( !$start || !$end ) {
100            $this->output( "...$table table seems to be empty.\n" );
101
102            return 0;
103        }
104
105        # Do remaining chunks
106        $blockStart = intval( $start );
107        $blockEnd = intval( $start ) + $batchSize - 1;
108        $count = 0;
109
110        while ( $blockStart <= $end ) {
111            $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
112
113            $res = $queryBuilder
114                ->where( [
115                    "$idCol >= $blockStart",
116                    "$idCol <= $blockEnd",
117                    $dbr->expr( "{$prefix}_len", '=', null )
118                        ->orExpr(
119                            $dbr->expr( "{$prefix}_len", '=', 0 )
120                                // sha1( "" )
121                                ->and( "{$prefix}_sha1", '!=', 'phoiac9h4m842xq45sp7s6u21eteeq1' )
122                        ),
123                ] )
124                ->caller( __METHOD__ )->fetchResultSet();
125
126            if ( $res->numRows() > 0 ) {
127                $this->beginTransaction( $dbw, __METHOD__ );
128                # Go through and update rev_len from these rows.
129                foreach ( $res as $row ) {
130                    if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
131                        $count++;
132                    }
133                }
134                $this->commitTransaction( $dbw, __METHOD__ );
135            }
136
137            $blockStart += $batchSize;
138            $blockEnd += $batchSize;
139        }
140
141        return $count;
142    }
143
144    /**
145     * @param stdClass $row
146     * @param string $table
147     * @param string $idCol
148     * @param string $prefix
149     * @return bool
150     */
151    protected function upgradeRow( $row, $table, $idCol, $prefix ) {
152        $dbw = $this->getPrimaryDB();
153
154        $revFactory = $this->getServiceContainer()->getRevisionFactory();
155        if ( $table === 'archive' ) {
156            $revRecord = $revFactory->newRevisionFromArchiveRow( $row );
157        } else {
158            $revRecord = $revFactory->newRevisionFromRow( $row );
159        }
160
161        if ( !$revRecord ) {
162            # This should not happen, but sometimes does (T22757)
163            $id = $row->$idCol;
164            $this->output( "RevisionRecord of $table $id unavailable!\n" );
165
166            return false;
167        }
168
169        # Update the row...
170        $dbw->newUpdateQueryBuilder()
171            ->update( $table )
172            ->set( [ "{$prefix}_len" => $revRecord->getSize() ] )
173            ->where( [ $idCol => $row->$idCol ] )
174            ->caller( __METHOD__ )->execute();
175
176        return true;
177    }
178}
179
180$maintClass = PopulateRevisionLength::class;
181require_once RUN_MAINTENANCE_IF_MAIN;