3 * Populate ar_rev_id in pre-1.5 rows
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
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\DBQueryError
;
25 use Wikimedia\Rdbms\IDatabase
;
27 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that populares archive.ar_rev_id in old rows
32 * @ingroup Maintenance
35 class PopulateArchiveRevId
extends LoggedUpdateMaintenance
{
37 /** @var array|null Dummy revision row */
38 private static $dummyRev = null;
40 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Populate ar_rev_id in pre-1.5 rows' );
43 $this->setBatchSize( 100 );
46 protected function getUpdateKey() {
50 protected function doDBUpdates() {
51 $this->output( "Populating ar_rev_id...\n" );
52 $dbw = $this->getDB( DB_MASTER
);
53 self
::checkMysqlAutoIncrementBug( $dbw );
55 // Quick exit if there are no rows needing updates.
56 $any = $dbw->selectField(
59 [ 'ar_rev_id' => null ],
63 $this->output( "Completed ar_rev_id population, 0 rows updated.\n" );
71 $arIds = $dbw->selectFieldValues(
74 [ 'ar_rev_id' => null ],
76 [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => [ 'ar_id' ] ]
79 $this->output( "Completed ar_rev_id population, $count rows updated.\n" );
83 $count +
= self
::reassignArRevIds( $dbw, $arIds, [ 'ar_rev_id' => null ] );
87 $this->output( " ... $min-$max\n" );
92 * Check for (and work around) a MySQL auto-increment bug
94 * (T202032) MySQL until 8.0 and MariaDB until some version after 10.1.34
95 * don't save the auto-increment value to disk, so on server restart it
96 * might reuse IDs from deleted revisions. We can fix that with an insert
97 * with an explicit rev_id value, if necessary.
99 * @param IDatabase $dbw
101 public static function checkMysqlAutoIncrementBug( IDatabase
$dbw ) {
102 if ( $dbw->getType() !== 'mysql' ) {
106 if ( !self
::$dummyRev ) {
107 self
::$dummyRev = self
::makeDummyRevisionRow( $dbw );
113 $dbw->doAtomicSection( __METHOD__
, function ( IDatabase
$dbw, $fname ) {
114 $dbw->insert( 'revision', self
::$dummyRev, $fname );
115 $id = $dbw->insertId();
119 (int)$dbw->selectField( 'archive', 'MAX(ar_rev_id)', [], $fname ),
120 (int)$dbw->selectField( 'slots', 'MAX(slot_revision_id)', [], $fname )
122 if ( $id <= $maxId ) {
123 $dbw->insert( 'revision', [ 'rev_id' => $maxId +
1 ] + self
::$dummyRev, $fname );
124 $toDelete[] = $maxId +
1;
127 $dbw->delete( 'revision', [ 'rev_id' => $toDelete ], $fname );
130 } catch ( DBQueryError
$e ) {
131 if ( $e->errno
!= 1062 ) { // 1062 is "duplicate entry", ignore it and retry
139 * Assign new ar_rev_ids to a set of ar_ids.
140 * @param IDatabase $dbw
141 * @param int[] $arIds
142 * @param array $conds Extra conditions for the update
143 * @return int Number of updated rows
145 public static function reassignArRevIds( IDatabase
$dbw, array $arIds, array $conds = [] ) {
146 if ( !self
::$dummyRev ) {
147 self
::$dummyRev = self
::makeDummyRevisionRow( $dbw );
150 $updates = $dbw->doAtomicSection( __METHOD__
, function ( IDatabase
$dbw, $fname ) use ( $arIds ) {
151 // Create new rev_ids by inserting dummy rows into revision and then deleting them.
152 $dbw->insert( 'revision', array_fill( 0, count( $arIds ), self
::$dummyRev ), $fname );
153 $revIds = $dbw->selectFieldValues(
156 [ 'rev_timestamp' => self
::$dummyRev['rev_timestamp'] ],
159 if ( !is_array( $revIds ) ) {
160 throw new UnexpectedValueException( 'Failed to insert dummy revisions' );
162 if ( count( $revIds ) !== count( $arIds ) ) {
163 throw new UnexpectedValueException(
164 'Tried to insert ' . count( $arIds ) . ' dummy revisions, but found '
165 . count( $revIds ) . ' matching rows.'
168 $dbw->delete( 'revision', [ 'rev_id' => $revIds ], $fname );
170 return array_combine( $arIds, $revIds );
174 foreach ( $updates as $arId => $revId ) {
177 [ 'ar_rev_id' => $revId ],
178 [ 'ar_id' => $arId ] +
$conds,
181 $count +
= $dbw->affectedRows();
187 * Construct a dummy revision table row to use for reserving IDs
189 * The row will have a wildly unlikely timestamp, and possibly a generic
190 * user and comment, but will otherwise be derived from a revision on the
191 * wiki's main page or some other revision in the database.
193 * @param IDatabase $dbw
196 private static function makeDummyRevisionRow( IDatabase
$dbw ) {
197 $ts = $dbw->timestamp( '11111111111111' );
200 $mainPage = Title
::newMainPage();
201 $pageId = $mainPage ?
$mainPage->getArticleID() : null;
203 $rev = $dbw->selectRow(
206 [ 'rev_page' => $pageId ],
208 [ 'ORDER BY' => 'rev_timestamp ASC' ]
213 // No main page? Let's see if there are any revisions at all
214 $rev = $dbw->selectRow(
219 [ 'ORDER BY' => 'rev_timestamp ASC' ]
223 // Since no revisions are available to copy, generate a dummy
224 // revision to a dummy page, then rollback the commit
225 wfDebug( __METHOD__
. ": No revisions are available to copy\n" );
229 // Make a title and revision and insert them
230 $title = Title
::newFromText( "PopulateArchiveRevId_4b05b46a81e29" );
231 $page = WikiPage
::factory( $title );
232 $updater = $page->newPageUpdater(
233 User
::newSystemUser( 'Maintenance script', [ 'steal' => true ] )
235 $updater->setContent(
237 ContentHandler
::makeContent( "Content for dummy rev", $title )
239 $updater->saveRevision(
240 CommentStoreComment
::newUnsavedComment( 'dummy rev summary' ),
241 EDIT_NEW | EDIT_SUPPRESS_RC
244 // get the revision row just inserted
245 $rev = $dbw->selectRow(
250 [ 'ORDER BY' => 'rev_timestamp ASC' ]
256 // This should never happen.
257 throw new UnexpectedValueException(
258 'No revisions are available to copy, and one couldn\'t be created'
262 unset( $rev->rev_id
);
264 $rev['rev_timestamp'] = $ts;
265 if ( isset( $rev['rev_user'] ) ) {
266 $rev['rev_user'] = 0;
267 $rev['rev_user_text'] = '0.0.0.0';
269 if ( isset( $rev['rev_comment'] ) ) {
270 $rev['rev_comment'] = 'Dummy row';
273 $any = $dbw->selectField(
276 [ 'rev_timestamp' => $ts ],
280 throw new UnexpectedValueException( "... Why does your database contain a revision dated $ts?" );
287 $maintClass = "PopulateArchiveRevId";
288 require_once RUN_MAINTENANCE_IF_MAIN
;