Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / maintenance / populateRevisionSha1.php
1 <?php
2 /**
3 * Fills the rev_sha1 and ar_sha1 columns of revision
4 * and archive tables for revisions created before MW 1.19.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
20 *
21 * @file
22 * @ingroup Maintenance
23 */
24
25 require_once __DIR__ . '/Maintenance.php';
26
27 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Maintenance script that fills the rev_sha1 and ar_sha1 columns of revision
31 * and archive tables for revisions created before MW 1.19.
32 *
33 * @ingroup Maintenance
34 */
35 class PopulateRevisionSha1 extends LoggedUpdateMaintenance {
36 public function __construct() {
37 parent::__construct();
38 $this->addDescription( 'Populates the rev_sha1 and ar_sha1 fields' );
39 $this->setBatchSize( 200 );
40 }
41
42 protected function getUpdateKey() {
43 return 'populate rev_sha1';
44 }
45
46 protected function doDBUpdates() {
47 $db = $this->getDB( DB_MASTER );
48
49 if ( !$db->tableExists( 'revision' ) ) {
50 $this->fatalError( "revision table does not exist" );
51 } elseif ( !$db->tableExists( 'archive' ) ) {
52 $this->fatalError( "archive table does not exist" );
53 } elseif ( !$db->fieldExists( 'revision', 'rev_sha1', __METHOD__ ) ) {
54 $this->output( "rev_sha1 column does not exist\n\n", true );
55 return false;
56 }
57
58 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
59
60 $this->output( "Populating rev_sha1 column\n" );
61 $rc = $this->doSha1Updates( $revStore, 'revision', 'rev_id',
62 $revStore->getQueryInfo(), 'rev'
63 );
64
65 $this->output( "Populating ar_sha1 column\n" );
66 $ac = $this->doSha1Updates( $revStore, 'archive', 'ar_rev_id',
67 $revStore->getArchiveQueryInfo(), 'ar'
68 );
69 $this->output( "Populating ar_sha1 column legacy rows\n" );
70 $ac += $this->doSha1LegacyUpdates( $revStore );
71
72 $this->output( "rev_sha1 and ar_sha1 population complete "
73 . "[$rc revision rows, $ac archive rows].\n" );
74
75 return true;
76 }
77
78 /**
79 * @param MediaWiki\Revision\RevisionStore $revStore
80 * @param string $table
81 * @param string $idCol
82 * @param array $queryInfo
83 * @param string $prefix
84 * @return int Rows changed
85 */
86 protected function doSha1Updates( $revStore, $table, $idCol, $queryInfo, $prefix ) {
87 $db = $this->getDB( DB_MASTER );
88 $batchSize = $this->getBatchSize();
89 $start = $db->selectField( $table, "MIN($idCol)", '', __METHOD__ );
90 $end = $db->selectField( $table, "MAX($idCol)", '', __METHOD__ );
91 if ( !$start || !$end ) {
92 $this->output( "...$table table seems to be empty.\n" );
93
94 return 0;
95 }
96
97 $count = 0;
98 # Do remaining chunk
99 $end += $batchSize - 1;
100 $blockStart = $start;
101 $blockEnd = $start + $batchSize - 1;
102 while ( $blockEnd <= $end ) {
103 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
104
105 $cond = "$idCol BETWEEN " . (int)$blockStart . " AND " . (int)$blockEnd .
106 " AND $idCol IS NOT NULL AND {$prefix}_sha1 = ''";
107 $res = $db->select(
108 $queryInfo['tables'], $queryInfo['fields'], $cond, __METHOD__, [], $queryInfo['joins']
109 );
110
111 $this->beginTransaction( $db, __METHOD__ );
112 foreach ( $res as $row ) {
113 if ( $this->upgradeRow( $revStore, $row, $table, $idCol, $prefix ) ) {
114 $count++;
115 }
116 }
117 $this->commitTransaction( $db, __METHOD__ );
118
119 $blockStart += $batchSize;
120 $blockEnd += $batchSize;
121 }
122
123 return $count;
124 }
125
126 /**
127 * @param MediaWiki\Revision\RevisionStore $revStore
128 * @param string $emptySha1
129 * @return int
130 */
131 protected function doSha1LegacyUpdates( $revStore ) {
132 $count = 0;
133 $db = $this->getDB( DB_MASTER );
134 $arQuery = $revStore->getArchiveQueryInfo();
135 $res = $db->select( $arQuery['tables'], $arQuery['fields'],
136 [ 'ar_rev_id IS NULL', 'ar_sha1' => '' ], __METHOD__, [], $arQuery['joins'] );
137
138 $updateSize = 0;
139 $this->beginTransaction( $db, __METHOD__ );
140 foreach ( $res as $row ) {
141 if ( $this->upgradeLegacyArchiveRow( $revStore, $row ) ) {
142 ++$count;
143 }
144 if ( ++$updateSize >= 100 ) {
145 $updateSize = 0;
146 $this->commitTransaction( $db, __METHOD__ );
147 $this->output( "Commited row with ar_timestamp={$row->ar_timestamp}\n" );
148 $this->beginTransaction( $db, __METHOD__ );
149 }
150 }
151 $this->commitTransaction( $db, __METHOD__ );
152
153 return $count;
154 }
155
156 /**
157 * @param MediaWiki\Revision\RevisionStore $revStore
158 * @param stdClass $row
159 * @param string $table
160 * @param string $idCol
161 * @param string $prefix
162 * @return bool
163 */
164 protected function upgradeRow( $revStore, $row, $table, $idCol, $prefix ) {
165 $db = $this->getDB( DB_MASTER );
166
167 // Create a revision and use it to get the sha1 from the content table, if possible.
168 try {
169 $rev = ( $table === 'archive' )
170 ? $revStore->newRevisionFromArchiveRow( $row )
171 : $revStore->newRevisionFromRow( $row );
172 $sha1 = $rev->getSha1();
173 } catch ( Exception $e ) {
174 $this->output( "Data of revision with {$idCol}={$row->$idCol} unavailable!\n" );
175 return false; // T24624? T22757?
176 }
177
178 $db->update( $table,
179 [ "{$prefix}_sha1" => $sha1 ],
180 [ $idCol => $row->$idCol ],
181 __METHOD__
182 );
183
184 return true;
185 }
186
187 /**
188 * @param MediaWiki\Revision\RevisionStore $revStore
189 * @param stdClass $row
190 * @return bool
191 */
192 protected function upgradeLegacyArchiveRow( $revStore, $row ) {
193 $db = $this->getDB( DB_MASTER );
194
195 // Create a revision and use it to get the sha1 from the content table, if possible.
196 try {
197 $rev = $revStore->newRevisionFromArchiveRow( $row );
198 $sha1 = $rev->getSha1();
199 } catch ( Exception $e ) {
200 $this->output( "Text of revision with timestamp {$row->ar_timestamp} unavailable!\n" );
201 return false; // T24624? T22757?
202 }
203
204 # Archive table has no PK, but (NS,title,time) should be near unique.
205 # Any duplicates on those should also have duplicated text anyway.
206 $db->update( 'archive',
207 [ 'ar_sha1' => $sha1 ],
208 [
209 'ar_namespace' => $row->ar_namespace,
210 'ar_title' => $row->ar_title,
211 'ar_timestamp' => $row->ar_timestamp,
212 'ar_len' => $row->ar_len // extra sanity
213 ],
214 __METHOD__
215 );
216
217 return true;
218 }
219 }
220
221 $maintClass = PopulateRevisionSha1::class;
222 require_once RUN_MAINTENANCE_IF_MAIN;