Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / maintenance / populateRevisionLength.php
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
24 use MediaWiki\Revision\RevisionRecord;
25 use Wikimedia\Rdbms\IDatabase;
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
31 * This is the case for all revisions created before MW 1.10, as well as those affected
32 * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
33 *
34 * @ingroup Maintenance
35 */
36 class PopulateRevisionLength extends LoggedUpdateMaintenance {
37 public function __construct() {
38 parent::__construct();
39 $this->addDescription( 'Populates the rev_len and ar_len fields' );
40 $this->setBatchSize( 200 );
41 }
42
43 protected function getUpdateKey() {
44 return 'populate rev_len and ar_len';
45 }
46
47 public function doDBUpdates() {
48 $dbw = $this->getDB( DB_MASTER );
49 if ( !$dbw->tableExists( 'revision' ) ) {
50 $this->fatalError( "revision table does not exist" );
51 } elseif ( !$dbw->tableExists( 'archive' ) ) {
52 $this->fatalError( "archive table does not exist" );
53 } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
54 $this->output( "rev_len column does not exist\n\n", true );
55
56 return false;
57 }
58
59 $this->output( "Populating rev_len column\n" );
60 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::getQueryInfo() );
61
62 $this->output( "Populating ar_len column\n" );
63 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::getArchiveQueryInfo() );
64
65 $this->output( "rev_len and ar_len population complete "
66 . "[$rev revision rows, $ar archive rows].\n" );
67
68 return true;
69 }
70
71 /**
72 * @param string $table
73 * @param string $idCol
74 * @param string $prefix
75 * @param array $queryInfo
76 * @return int
77 */
78 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
79 $dbr = $this->getDB( DB_REPLICA );
80 $dbw = $this->getDB( DB_MASTER );
81 $batchSize = $this->getBatchSize();
82 $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__ );
83 $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__ );
84 if ( !$start || !$end ) {
85 $this->output( "...$table table seems to be empty.\n" );
86
87 return 0;
88 }
89
90 # Do remaining chunks
91 $blockStart = intval( $start );
92 $blockEnd = intval( $start ) + $batchSize - 1;
93 $count = 0;
94
95 while ( $blockStart <= $end ) {
96 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
97 $res = $dbr->select(
98 $queryInfo['tables'],
99 $queryInfo['fields'],
100 [
101 "$idCol >= $blockStart",
102 "$idCol <= $blockEnd",
103 $dbr->makeList( [
104 "{$prefix}_len IS NULL",
105 $dbr->makeList( [
106 "{$prefix}_len = 0",
107 "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ), // sha1( "" )
108 ], IDatabase::LIST_AND )
109 ], IDatabase::LIST_OR )
110 ],
111 __METHOD__,
112 [],
113 $queryInfo['joins']
114 );
115
116 if ( $res->numRows() > 0 ) {
117 $this->beginTransaction( $dbw, __METHOD__ );
118 # Go through and update rev_len from these rows.
119 foreach ( $res as $row ) {
120 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
121 $count++;
122 }
123 }
124 $this->commitTransaction( $dbw, __METHOD__ );
125 }
126
127 $blockStart += $batchSize;
128 $blockEnd += $batchSize;
129 }
130
131 return $count;
132 }
133
134 /**
135 * @param stdClass $row
136 * @param string $table
137 * @param string $idCol
138 * @param string $prefix
139 * @return bool
140 */
141 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
142 $dbw = $this->getDB( DB_MASTER );
143
144 $rev = ( $table === 'archive' )
145 ? Revision::newFromArchiveRow( $row )
146 : new Revision( $row );
147
148 $content = $rev->getContent( RevisionRecord::RAW );
149 if ( !$content ) {
150 # This should not happen, but sometimes does (T22757)
151 $id = $row->$idCol;
152 $this->output( "Content of $table $id unavailable!\n" );
153
154 return false;
155 }
156
157 # Update the row...
158 $dbw->update( $table,
159 [ "{$prefix}_len" => $content->getSize() ],
160 [ $idCol => $row->$idCol ],
161 __METHOD__
162 );
163
164 return true;
165 }
166 }
167
168 $maintClass = PopulateRevisionLength::class;
169 require_once RUN_MAINTENANCE_IF_MAIN;