MediaWiki  REL1_31
populateArchiveRevId.php
Go to the documentation of this file.
1 <?php
25 
26 require_once __DIR__ . '/Maintenance.php';
27 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Populate ar_rev_id in pre-1.5 rows' );
38  $this->setBatchSize( 100 );
39  }
40 
41  protected function getUpdateKey() {
42  return __CLASS__;
43  }
44 
45  protected function doDBUpdates() {
46  $this->output( "Populating ar_rev_id...\n" );
47  $dbw = $this->getDB( DB_MASTER );
48 
49  // Quick exit if there are no rows needing updates.
50  $any = $dbw->selectField(
51  'archive',
52  'ar_id',
53  [ 'ar_rev_id' => null ],
54  __METHOD__
55  );
56  if ( !$any ) {
57  $this->output( "Completed ar_rev_id population, 0 rows updated.\n" );
58  return true;
59  }
60 
61  $rev = $this->makeDummyRevisionRow( $dbw );
62  $count = 0;
63  while ( true ) {
65 
66  $arIds = $dbw->selectFieldValues(
67  'archive',
68  'ar_id',
69  [ 'ar_rev_id' => null ],
70  __METHOD__,
71  [ 'LIMIT' => $this->getBatchSize(), 'ORDER BY' => [ 'ar_id' ] ]
72  );
73  if ( !$arIds ) {
74  $this->output( "Completed ar_rev_id population, $count rows updated.\n" );
75  return true;
76  }
77 
78  try {
79  $updates = $dbw->doAtomicSection( __METHOD__, function ( $dbw, $fname ) use ( $arIds, $rev ) {
80  // Create new rev_ids by inserting dummy rows into revision and then deleting them.
81  $dbw->insert( 'revision', array_fill( 0, count( $arIds ), $rev ), $fname );
82  $revIds = $dbw->selectFieldValues(
83  'revision',
84  'rev_id',
85  [ 'rev_timestamp' => $rev['rev_timestamp'] ],
86  $fname
87  );
88  if ( !is_array( $revIds ) ) {
89  throw new UnexpectedValueException( 'Failed to insert dummy revisions' );
90  }
91  if ( count( $revIds ) !== count( $arIds ) ) {
92  throw new UnexpectedValueException(
93  'Tried to insert ' . count( $arIds ) . ' dummy revisions, but found '
94  . count( $revIds ) . ' matching rows.'
95  );
96  }
97  $dbw->delete( 'revision', [ 'rev_id' => $revIds ], $fname );
98 
99  return array_combine( $arIds, $revIds );
100  } );
101  } catch ( UnexpectedValueException $ex ) {
102  $this->fatalError( $ex->getMessage() );
103  }
104 
105  foreach ( $updates as $arId => $revId ) {
106  $dbw->update(
107  'archive',
108  [ 'ar_rev_id' => $revId ],
109  [ 'ar_id' => $arId, 'ar_rev_id' => null ],
110  __METHOD__
111  );
112  $count += $dbw->affectedRows();
113  }
114 
115  $min = min( array_keys( $updates ) );
116  $max = max( array_keys( $updates ) );
117  $this->output( " ... $min-$max\n" );
118  }
119  }
120 
131  private function makeDummyRevisionRow( IDatabase $dbw ) {
132  $ts = $dbw->timestamp( '11111111111111' );
133  $mainPage = Title::newMainPage();
134  if ( !$mainPage ) {
135  $this->fatalError( 'Main page does not exist' );
136  }
137  $pageId = $mainPage->getArticleId();
138  if ( !$pageId ) {
139  $this->fatalError( $mainPage->getPrefixedText() . ' has no ID' );
140  }
141  $rev = $dbw->selectRow(
142  'revision',
143  '*',
144  [ 'rev_page' => $pageId ],
145  __METHOD__,
146  [ 'ORDER BY' => 'rev_timestamp ASC' ]
147  );
148  if ( !$rev ) {
149  $this->fatalError( $mainPage->getPrefixedText() . ' has no revisions' );
150  }
151  unset( $rev->rev_id );
152  $rev = (array)$rev;
153  $rev['rev_timestamp'] = $ts;
154  if ( isset( $rev['rev_user'] ) ) {
155  $rev['rev_user'] = 0;
156  $rev['rev_user_text'] = '0.0.0.0';
157  }
158  if ( isset( $rev['rev_comment'] ) ) {
159  $rev['rev_comment'] = 'Dummy row';
160  }
161 
162  $any = $dbw->selectField(
163  'revision',
164  'rev_id',
165  [ 'rev_timestamp' => $ts ],
166  __METHOD__
167  );
168  if ( $any ) {
169  $this->fatalError( "... Why does your database contain a revision dated $ts?" );
170  }
171 
172  return $rev;
173  }
174 }
175 
176 $maintClass = "PopulateArchiveRevId";
177 require_once RUN_MAINTENANCE_IF_MAIN;
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
array
the array() calling protocol came about after MediaWiki 1.4rc1.
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:439
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:586
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:2966
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:37
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
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:35
PopulateArchiveRevId\makeDummyRevisionRow
makeDummyRevisionRow(IDatabase $dbw)
Construct a dummy revision table row to use for reserving IDs.
Definition: populateArchiveRevId.php:131
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1640
PopulateArchiveRevId
Maintenance script that populares archive.ar_rev_id in old rows.
Definition: populateArchiveRevId.php:34
PopulateArchiveRevId\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateArchiveRevId.php:45
DB_MASTER
const DB_MASTER
Definition: defines.php:29
$fname
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition: Setup.php:112
Wikimedia\Rdbms\IDatabase\selectRow
selectRow( $table, $vars, $conds, $fname=__METHOD__, $options=[], $join_conds=[])
Single row SELECT wrapper.
$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:1777
$maintClass
$maintClass
Definition: populateArchiveRevId.php:176
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:321
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:22
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1309
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:388
PopulateArchiveRevId\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateArchiveRevId.php:41
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:329