Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[lhc/web/wiklou.git] / maintenance / populateArchiveRevId.php
1 <?php
2 /**
3 * Populate ar_rev_id in pre-1.5 rows
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\DBQueryError;
25 use Wikimedia\Rdbms\IDatabase;
26
27 require_once __DIR__ . '/Maintenance.php';
28
29 /**
30 * Maintenance script that populares archive.ar_rev_id in old rows
31 *
32 * @ingroup Maintenance
33 * @since 1.31
34 */
35 class PopulateArchiveRevId extends LoggedUpdateMaintenance {
36
37 /** @var array|null Dummy revision row */
38 private static $dummyRev = null;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Populate ar_rev_id in pre-1.5 rows' );
43 $this->setBatchSize( 100 );
44 }
45
46 /**
47 * @param IDatabase $dbw
48 * @return bool
49 */
50 public static function isNewInstall( IDatabase $dbw ) {
51 return $dbw->selectRowCount( 'archive' ) === 0 &&
52 $dbw->selectRowCount( 'revision' ) === 1;
53 }
54
55 protected function getUpdateKey() {
56 return __CLASS__;
57 }
58
59 protected function doDBUpdates() {
60 $this->output( "Populating ar_rev_id...\n" );
61 $dbw = $this->getDB( DB_MASTER );
62 self::checkMysqlAutoIncrementBug( $dbw );
63
64 // Quick exit if there are no rows needing updates.
65 $any = $dbw->selectField(
66 'archive',
67 'ar_id',
68 [ 'ar_rev_id' => null ],
69 __METHOD__
70 );
71 if ( !$any ) {
72 $this->output( "Completed ar_rev_id population, 0 rows updated.\n" );
73 return true;
74 }
75
76 $count = 0;
77 while ( true ) {
78 wfWaitForSlaves();
79
80 $arIds = $dbw->selectFieldValues(
81 'archive',
82 'ar_id',
83 [ 'ar_rev_id' => null ],
84 __METHOD__,
85 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => [ 'ar_id' ] ]
86 );
87 if ( !$arIds ) {
88 $this->output( "Completed ar_rev_id population, $count rows updated.\n" );
89 return true;
90 }
91
92 $count += self::reassignArRevIds( $dbw, $arIds, [ 'ar_rev_id' => null ] );
93
94 $min = min( $arIds );
95 $max = max( $arIds );
96 $this->output( " ... $min-$max\n" );
97 }
98 }
99
100 /**
101 * Check for (and work around) a MySQL auto-increment bug
102 *
103 * (T202032) MySQL until 8.0 and MariaDB until some version after 10.1.34
104 * don't save the auto-increment value to disk, so on server restart it
105 * might reuse IDs from deleted revisions. We can fix that with an insert
106 * with an explicit rev_id value, if necessary.
107 *
108 * @param IDatabase $dbw
109 */
110 public static function checkMysqlAutoIncrementBug( IDatabase $dbw ) {
111 if ( $dbw->getType() !== 'mysql' ) {
112 return;
113 }
114
115 if ( !self::$dummyRev ) {
116 self::$dummyRev = self::makeDummyRevisionRow( $dbw );
117 }
118
119 $ok = false;
120 while ( !$ok ) {
121 try {
122 $dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) {
123 $dbw->insert( 'revision', self::$dummyRev, $fname );
124 $id = $dbw->insertId();
125 $toDelete = [ $id ];
126
127 $maxId = max(
128 (int)$dbw->selectField( 'archive', 'MAX(ar_rev_id)', [], $fname ),
129 (int)$dbw->selectField( 'slots', 'MAX(slot_revision_id)', [], $fname )
130 );
131 if ( $id <= $maxId ) {
132 $dbw->insert( 'revision', [ 'rev_id' => $maxId + 1 ] + self::$dummyRev, $fname );
133 $toDelete[] = $maxId + 1;
134 }
135
136 $dbw->delete( 'revision', [ 'rev_id' => $toDelete ], $fname );
137 } );
138 $ok = true;
139 } catch ( DBQueryError $e ) {
140 if ( $e->errno != 1062 ) { // 1062 is "duplicate entry", ignore it and retry
141 throw $e;
142 }
143 }
144 }
145 }
146
147 /**
148 * Assign new ar_rev_ids to a set of ar_ids.
149 * @param IDatabase $dbw
150 * @param int[] $arIds
151 * @param array $conds Extra conditions for the update
152 * @return int Number of updated rows
153 */
154 public static function reassignArRevIds( IDatabase $dbw, array $arIds, array $conds = [] ) {
155 if ( !self::$dummyRev ) {
156 self::$dummyRev = self::makeDummyRevisionRow( $dbw );
157 }
158
159 $updates = $dbw->doAtomicSection( __METHOD__, function ( IDatabase $dbw, $fname ) use ( $arIds ) {
160 // Create new rev_ids by inserting dummy rows into revision and then deleting them.
161 $dbw->insert( 'revision', array_fill( 0, count( $arIds ), self::$dummyRev ), $fname );
162 $revIds = $dbw->selectFieldValues(
163 'revision',
164 'rev_id',
165 [ 'rev_timestamp' => self::$dummyRev['rev_timestamp'] ],
166 $fname
167 );
168 if ( !is_array( $revIds ) ) {
169 throw new UnexpectedValueException( 'Failed to insert dummy revisions' );
170 }
171 if ( count( $revIds ) !== count( $arIds ) ) {
172 throw new UnexpectedValueException(
173 'Tried to insert ' . count( $arIds ) . ' dummy revisions, but found '
174 . count( $revIds ) . ' matching rows.'
175 );
176 }
177 $dbw->delete( 'revision', [ 'rev_id' => $revIds ], $fname );
178
179 return array_combine( $arIds, $revIds );
180 } );
181
182 $count = 0;
183 foreach ( $updates as $arId => $revId ) {
184 $dbw->update(
185 'archive',
186 [ 'ar_rev_id' => $revId ],
187 [ 'ar_id' => $arId ] + $conds,
188 __METHOD__
189 );
190 $count += $dbw->affectedRows();
191 }
192 return $count;
193 }
194
195 /**
196 * Construct a dummy revision table row to use for reserving IDs
197 *
198 * The row will have a wildly unlikely timestamp, and possibly a generic
199 * user and comment, but will otherwise be derived from a revision on the
200 * wiki's main page or some other revision in the database.
201 *
202 * @param IDatabase $dbw
203 * @return array
204 */
205 private static function makeDummyRevisionRow( IDatabase $dbw ) {
206 $ts = $dbw->timestamp( '11111111111111' );
207 $rev = null;
208
209 $mainPage = Title::newMainPage();
210 $pageId = $mainPage ? $mainPage->getArticleID() : null;
211 if ( $pageId ) {
212 $rev = $dbw->selectRow(
213 'revision',
214 '*',
215 [ 'rev_page' => $pageId ],
216 __METHOD__,
217 [ 'ORDER BY' => 'rev_timestamp ASC' ]
218 );
219 }
220
221 if ( !$rev ) {
222 // No main page? Let's see if there are any revisions at all
223 $rev = $dbw->selectRow(
224 'revision',
225 '*',
226 [],
227 __METHOD__,
228 [ 'ORDER BY' => 'rev_timestamp ASC' ]
229 );
230 }
231 if ( !$rev ) {
232 // Since no revisions are available to copy, generate a dummy
233 // revision to a dummy page, then rollback the commit
234 wfDebug( __METHOD__ . ": No revisions are available to copy\n" );
235
236 $dbw->begin();
237
238 // Make a title and revision and insert them
239 $title = Title::newFromText( "PopulateArchiveRevId_4b05b46a81e29" );
240 $page = WikiPage::factory( $title );
241 $updater = $page->newPageUpdater(
242 User::newSystemUser( 'Maintenance script', [ 'steal' => true ] )
243 );
244 $updater->setContent(
245 'main',
246 ContentHandler::makeContent( "Content for dummy rev", $title )
247 );
248 $updater->saveRevision(
249 CommentStoreComment::newUnsavedComment( 'dummy rev summary' ),
250 EDIT_NEW | EDIT_SUPPRESS_RC
251 );
252
253 // get the revision row just inserted
254 $rev = $dbw->selectRow(
255 'revision',
256 '*',
257 [],
258 __METHOD__,
259 [ 'ORDER BY' => 'rev_timestamp ASC' ]
260 );
261
262 $dbw->rollback();
263 }
264 if ( !$rev ) {
265 // This should never happen.
266 throw new UnexpectedValueException(
267 'No revisions are available to copy, and one couldn\'t be created'
268 );
269 }
270
271 unset( $rev->rev_id );
272 $rev = (array)$rev;
273 $rev['rev_timestamp'] = $ts;
274 if ( isset( $rev['rev_user'] ) ) {
275 $rev['rev_user'] = 0;
276 $rev['rev_user_text'] = '0.0.0.0';
277 }
278 if ( isset( $rev['rev_comment'] ) ) {
279 $rev['rev_comment'] = 'Dummy row';
280 }
281
282 $any = $dbw->selectField(
283 'revision',
284 'rev_id',
285 [ 'rev_timestamp' => $ts ],
286 __METHOD__
287 );
288 if ( $any ) {
289 throw new UnexpectedValueException( "... Why does your database contain a revision dated $ts?" );
290 }
291
292 return $rev;
293 }
294 }
295
296 $maintClass = "PopulateArchiveRevId";
297 require_once RUN_MAINTENANCE_IF_MAIN;