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