MediaWiki  1.32.5
populateArchiveRevId.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
36 
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  protected function getUpdateKey() {
47  return __CLASS__;
48  }
49 
50  protected function doDBUpdates() {
51  $this->output( "Populating ar_rev_id...\n" );
52  $dbw = $this->getDB( DB_MASTER );
54 
55  // Quick exit if there are no rows needing updates.
56  $any = $dbw->selectField(
57  'archive',
58  'ar_id',
59  [ 'ar_rev_id' => null ],
60  __METHOD__
61  );
62  if ( !$any ) {
63  $this->output( "Completed ar_rev_id population, 0 rows updated.\n" );
64  return true;
65  }
66 
67  $count = 0;
68  while ( true ) {
70 
71  $arIds = $dbw->selectFieldValues(
72  'archive',
73  'ar_id',
74  [ 'ar_rev_id' => null ],
75  __METHOD__,
76  [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => [ 'ar_id' ] ]
77  );
78  if ( !$arIds ) {
79  $this->output( "Completed ar_rev_id population, $count rows updated.\n" );
80  return true;
81  }
82 
83  $count += self::reassignArRevIds( $dbw, $arIds, [ 'ar_rev_id' => null ] );
84 
85  $min = min( $arIds );
86  $max = max( $arIds );
87  $this->output( " ... $min-$max\n" );
88  }
89  }
90 
101  public static function checkMysqlAutoIncrementBug( IDatabase $dbw ) {
102  if ( $dbw->getType() !== 'mysql' ) {
103  return;
104  }
105 
106  if ( !self::$dummyRev ) {
107  self::$dummyRev = self::makeDummyRevisionRow( $dbw );
108  }
109 
110  $ok = false;
111  while ( !$ok ) {
112  try {
113  $dbw->doAtomicSection( __METHOD__, function ( $dbw, $fname ) {
114  $dbw->insert( 'revision', self::$dummyRev, $fname );
115  $id = $dbw->insertId();
116  $toDelete[] = $id;
117 
118  $maxId = max(
119  (int)$dbw->selectField( 'archive', 'MAX(ar_rev_id)', [], $fname ),
120  (int)$dbw->selectField( 'slots', 'MAX(slot_revision_id)', [], $fname )
121  );
122  if ( $id <= $maxId ) {
123  $dbw->insert( 'revision', [ 'rev_id' => $maxId + 1 ] + self::$dummyRev, $fname );
124  $toDelete[] = $maxId + 1;
125  }
126 
127  $dbw->delete( 'revision', [ 'rev_id' => $toDelete ], $fname );
128  } );
129  $ok = true;
130  } catch ( DBQueryError $e ) {
131  if ( $e->errno != 1062 ) { // 1062 is "duplicate entry", ignore it and retry
132  throw $e;
133  }
134  }
135  }
136  }
137 
145  public static function reassignArRevIds( IDatabase $dbw, array $arIds, array $conds = [] ) {
146  if ( !self::$dummyRev ) {
147  self::$dummyRev = self::makeDummyRevisionRow( $dbw );
148  }
149 
150  $updates = $dbw->doAtomicSection( __METHOD__, function ( $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(
154  'revision',
155  'rev_id',
156  [ 'rev_timestamp' => self::$dummyRev['rev_timestamp'] ],
157  $fname
158  );
159  if ( !is_array( $revIds ) ) {
160  throw new UnexpectedValueException( 'Failed to insert dummy revisions' );
161  }
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.'
166  );
167  }
168  $dbw->delete( 'revision', [ 'rev_id' => $revIds ], $fname );
169 
170  return array_combine( $arIds, $revIds );
171  } );
172 
173  $count = 0;
174  foreach ( $updates as $arId => $revId ) {
175  $dbw->update(
176  'archive',
177  [ 'ar_rev_id' => $revId ],
178  [ 'ar_id' => $arId ] + $conds,
179  __METHOD__
180  );
181  $count += $dbw->affectedRows();
182  }
183  return $count;
184  }
185 
196  private static function makeDummyRevisionRow( IDatabase $dbw ) {
197  $ts = $dbw->timestamp( '11111111111111' );
198  $rev = null;
199 
200  $mainPage = Title::newMainPage();
201  $pageId = $mainPage ? $mainPage->getArticleId() : null;
202  if ( $pageId ) {
203  $rev = $dbw->selectRow(
204  'revision',
205  '*',
206  [ 'rev_page' => $pageId ],
207  __METHOD__,
208  [ 'ORDER BY' => 'rev_timestamp ASC' ]
209  );
210  }
211 
212  if ( !$rev ) {
213  // No main page? Let's see if there are any revisions at all
214  $rev = $dbw->selectRow(
215  'revision',
216  '*',
217  [],
218  __METHOD__,
219  [ 'ORDER BY' => 'rev_timestamp ASC' ]
220  );
221  }
222  if ( !$rev ) {
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" );
226 
227  $dbw->begin();
228 
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 ] )
234  );
235  $updater->setContent(
236  'main',
237  ContentHandler::makeContent( "Content for dummy rev", $title )
238  );
239  $updater->saveRevision(
240  CommentStoreComment::newUnsavedComment( 'dummy rev summary' ),
242  );
243 
244  // get the revision row just inserted
245  $rev = $dbw->selectRow(
246  'revision',
247  '*',
248  [],
249  __METHOD__,
250  [ 'ORDER BY' => 'rev_timestamp ASC' ]
251  );
252 
253  $dbw->rollback();
254  }
255  if ( !$rev ) {
256  // This should never happen.
257  throw new UnexpectedValueException(
258  'No revisions are available to copy, and one couldn\'t be created'
259  );
260  }
261 
262  unset( $rev->rev_id );
263  $rev = (array)$rev;
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';
268  }
269  if ( isset( $rev['rev_comment'] ) ) {
270  $rev['rev_comment'] = 'Dummy row';
271  }
272 
273  $any = $dbw->selectField(
274  'revision',
275  'rev_id',
276  [ 'rev_timestamp' => $ts ],
277  __METHOD__
278  );
279  if ( $any ) {
280  throw new UnexpectedValueException( "... Why does your database contain a revision dated $ts?" );
281  }
282 
283  return $rev;
284  }
285 }
286 
287 $maintClass = "PopulateArchiveRevId";
288 require_once RUN_MAINTENANCE_IF_MAIN;
CommentStoreComment\newUnsavedComment
static newUnsavedComment( $comment, array $data=null)
Create a new, unsaved CommentStoreComment.
Definition: CommentStoreComment.php:66
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:280
Wikimedia\Rdbms\IDatabase\affectedRows
affectedRows()
Get the number of rows affected by the last write query.
PopulateArchiveRevId\makeDummyRevisionRow
static makeDummyRevisionRow(IDatabase $dbw)
Construct a dummy revision table row to use for reserving IDs.
Definition: populateArchiveRevId.php:196
PopulateArchiveRevId\$dummyRev
static array null $dummyRev
Dummy revision row.
Definition: populateArchiveRevId.php:38
captcha-old.count
count
Definition: captcha-old.py:249
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:317
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:597
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
Wikimedia\Rdbms\IDatabase\selectField
selectField( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a single field from a single result row.
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:2847
Wikimedia\Rdbms\IDatabase\insert
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
User\newSystemUser
static newSystemUser( $name, $options=[])
Static factory method for creation of a "system" user from username.
Definition: User.php:819
Wikimedia\Rdbms\IDatabase\timestamp
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
PopulateArchiveRevId\__construct
__construct()
Default constructor.
Definition: populateArchiveRevId.php:40
PopulateArchiveRevId\checkMysqlAutoIncrementBug
static checkMysqlAutoIncrementBug(IDatabase $dbw)
Check for (and work around) a MySQL auto-increment bug.
Definition: populateArchiveRevId.php:101
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:127
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1679
PopulateArchiveRevId
Maintenance script that populares archive.ar_rev_id in old rows.
Definition: populateArchiveRevId.php:35
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
PopulateArchiveRevId\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateArchiveRevId.php:50
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:988
ContentHandler\makeContent
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Definition: ContentHandler.php:133
Wikimedia\Rdbms\DBQueryError
Definition: DBQueryError.php:27
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:121
PopulateArchiveRevId\reassignArRevIds
static reassignArRevIds(IDatabase $dbw, array $arIds, array $conds=[])
Assign new ar_rev_ids to a set of ar_ids.
Definition: populateArchiveRevId.php:145
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2221
Wikimedia\Rdbms\IDatabase\selectRow
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
Wikimedia\Rdbms\IDatabase\rollback
rollback( $fname=__METHOD__, $flush='')
Rollback a transaction previously started using begin().
Wikimedia\Rdbms\IDatabase\update
update( $table, $values, $conds, $fname=__METHOD__, $options=[])
UPDATE wrapper.
EDIT_NEW
const EDIT_NEW
Definition: Defines.php:152
Wikimedia\Rdbms\IDatabase\doAtomicSection
doAtomicSection( $fname, callable $callback, $cancelable=self::ATOMIC_NOT_CANCELABLE)
Perform an atomic section of reversable SQL statements from a callback.
$maintClass
$maintClass
Definition: populateArchiveRevId.php:287
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1808
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:347
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1351
Wikimedia\Rdbms\IDatabase\selectFieldValues
selectFieldValues( $table, $var, $cond='', $fname=__METHOD__, $options=[], $join_conds=[])
A SELECT wrapper which returns a list of single field values from result rows.
Wikimedia\Rdbms\IDatabase\getType
getType()
Get the type of the DBMS, as it appears in $wgDBtype.
$updater
$page->newPageUpdater($user) $updater
Definition: pageupdater.txt:63
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:414
EDIT_SUPPRESS_RC
const EDIT_SUPPRESS_RC
Definition: Defines.php:155
Wikimedia\Rdbms\IDatabase\insertId
insertId()
Get the inserted value of an auto-increment row.
PopulateArchiveRevId\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateArchiveRevId.php:46
Wikimedia\Rdbms\IDatabase\delete
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
Wikimedia\Rdbms\IDatabase\begin
begin( $fname=__METHOD__, $mode=self::TRANSACTION_EXPLICIT)
Begin a transaction.
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:355