Update git submodules
[mediawiki.git] / maintenance / populateRevisionLength.php
blob715089ea311697c99827f1d469ecc70a72ae6b83
1 <?php
2 /**
3 * Populates the rev_len and ar_len fields when they are NULL.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\IDatabase;
26 require_once __DIR__ . '/Maintenance.php';
28 /**
29 * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
30 * This is the case for all revisions created before MW 1.10, as well as those affected
31 * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
33 * @ingroup Maintenance
35 class PopulateRevisionLength extends LoggedUpdateMaintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Populates the rev_len and ar_len fields' );
39 $this->setBatchSize( 200 );
42 protected function getUpdateKey() {
43 return 'populate rev_len and ar_len';
46 public function doDBUpdates() {
47 $dbw = $this->getDB( DB_PRIMARY );
48 if ( !$dbw->tableExists( 'revision', __METHOD__ ) ) {
49 $this->fatalError( "revision table does not exist" );
50 } elseif ( !$dbw->tableExists( 'archive', __METHOD__ ) ) {
51 $this->fatalError( "archive table does not exist" );
52 } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
53 $this->output( "rev_len column does not exist\n\n", true );
55 return false;
58 $revisionStore = $this->getServiceContainer()->getRevisionStore();
60 $this->output( "Populating rev_len column\n" );
61 $rev = $this->doLenUpdates(
62 'revision',
63 'rev_id',
64 'rev',
65 $revisionStore->getQueryInfo()
68 $this->output( "Populating ar_len column\n" );
69 $ar = $this->doLenUpdates(
70 'archive',
71 'ar_id',
72 'ar',
73 $revisionStore->getArchiveQueryInfo()
76 $this->output( "rev_len and ar_len population complete "
77 . "[$rev revision rows, $ar archive rows].\n" );
79 return true;
82 /**
83 * @param string $table
84 * @param string $idCol
85 * @param string $prefix
86 * @param array $queryInfo
87 * @return int
89 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
90 $dbr = $this->getDB( DB_REPLICA );
91 $dbw = $this->getDB( DB_PRIMARY );
92 $batchSize = $this->getBatchSize();
93 $start = $dbw->newSelectQueryBuilder()
94 ->select( "MIN($idCol)" )
95 ->from( $table )
96 ->caller( __METHOD__ )->fetchField();
97 $end = $dbw->newSelectQueryBuilder()
98 ->select( "MAX($idCol)" )
99 ->from( $table )
100 ->caller( __METHOD__ )->fetchField();
101 if ( !$start || !$end ) {
102 $this->output( "...$table table seems to be empty.\n" );
104 return 0;
107 # Do remaining chunks
108 $blockStart = intval( $start );
109 $blockEnd = intval( $start ) + $batchSize - 1;
110 $count = 0;
112 while ( $blockStart <= $end ) {
113 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
114 $res = $dbr->select(
115 $queryInfo['tables'],
116 $queryInfo['fields'],
118 "$idCol >= $blockStart",
119 "$idCol <= $blockEnd",
120 $dbr->makeList( [
121 "{$prefix}_len" => null,
122 $dbr->makeList( [
123 "{$prefix}_len" => 0,
124 // sha1( "" )
125 "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ),
126 ], IDatabase::LIST_AND )
127 ], IDatabase::LIST_OR )
129 __METHOD__,
131 $queryInfo['joins']
134 if ( $res->numRows() > 0 ) {
135 $this->beginTransaction( $dbw, __METHOD__ );
136 # Go through and update rev_len from these rows.
137 foreach ( $res as $row ) {
138 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
139 $count++;
142 $this->commitTransaction( $dbw, __METHOD__ );
145 $blockStart += $batchSize;
146 $blockEnd += $batchSize;
149 return $count;
153 * @param stdClass $row
154 * @param string $table
155 * @param string $idCol
156 * @param string $prefix
157 * @return bool
159 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
160 $dbw = $this->getDB( DB_PRIMARY );
162 $revFactory = $this->getServiceContainer()->getRevisionFactory();
163 if ( $table === 'archive' ) {
164 $revRecord = $revFactory->newRevisionFromArchiveRow( $row );
165 } else {
166 $revRecord = $revFactory->newRevisionFromRow( $row );
169 if ( !$revRecord ) {
170 # This should not happen, but sometimes does (T22757)
171 $id = $row->$idCol;
172 $this->output( "RevisionRecord of $table $id unavailable!\n" );
174 return false;
177 # Update the row...
178 $dbw->update( $table,
179 [ "{$prefix}_len" => $revRecord->getSize() ],
180 [ $idCol => $row->$idCol ],
181 __METHOD__
184 return true;
188 $maintClass = PopulateRevisionLength::class;
189 require_once RUN_MAINTENANCE_IF_MAIN;