MediaWiki  1.30.0
populateParentId.php
Go to the documentation of this file.
1 <?php
26 require_once __DIR__ . '/Maintenance.php';
27 
35  public function __construct() {
36  parent::__construct();
37  $this->addDescription( 'Populates rev_parent_id' );
38  }
39 
40  protected function getUpdateKey() {
41  return 'populate rev_parent_id';
42  }
43 
44  protected function updateSkippedMessage() {
45  return 'rev_parent_id column of revision table already populated.';
46  }
47 
48  protected function doDBUpdates() {
49  $db = $this->getDB( DB_MASTER );
50  if ( !$db->tableExists( 'revision' ) ) {
51  $this->error( "revision table does not exist" );
52 
53  return false;
54  }
55  $this->output( "Populating rev_parent_id column\n" );
56  $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__ );
57  $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__ );
58  if ( is_null( $start ) || is_null( $end ) ) {
59  $this->output( "...revision table seems to be empty, nothing to do.\n" );
60 
61  return true;
62  }
63  # Do remaining chunk
64  $blockStart = intval( $start );
65  $blockEnd = intval( $start ) + $this->mBatchSize - 1;
66  $count = 0;
67  $changed = 0;
68  while ( $blockStart <= $end ) {
69  $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
70  $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
71  $res = $db->select( 'revision',
72  [ 'rev_id', 'rev_page', 'rev_timestamp', 'rev_parent_id' ],
73  [ $cond, 'rev_parent_id' => null ], __METHOD__ );
74  # Go through and update rev_parent_id from these rows.
75  # Assume that the previous revision of the title was
76  # the original previous revision of the title when the
77  # edit was made...
78  foreach ( $res as $row ) {
79  # First, check rows with the same timestamp other than this one
80  # with a smaller rev ID. The highest ID "wins". This avoids loops
81  # as timestamp can only decrease and never loops with IDs (from parent to parent)
82  $previousID = $db->selectField( 'revision', 'rev_id',
83  [ 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
84  "rev_id < " . intval( $row->rev_id ) ],
85  __METHOD__,
86  [ 'ORDER BY' => 'rev_id DESC' ] );
87  # If there are none, check the highest ID with a lower timestamp
88  if ( !$previousID ) {
89  # Get the highest older timestamp
90  $lastTimestamp = $db->selectField(
91  'revision',
92  'rev_timestamp',
93  [
94  'rev_page' => $row->rev_page,
95  "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp )
96  ],
97  __METHOD__,
98  [ 'ORDER BY' => 'rev_timestamp DESC' ]
99  );
100  # If there is one, let the highest rev ID win
101  if ( $lastTimestamp ) {
102  $previousID = $db->selectField( 'revision', 'rev_id',
103  [ 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ],
104  __METHOD__,
105  [ 'ORDER BY' => 'rev_id DESC' ] );
106  }
107  }
108  $previousID = intval( $previousID );
109  if ( $previousID != $row->rev_parent_id ) {
110  $changed++;
111  }
112  # Update the row...
113  $db->update( 'revision',
114  [ 'rev_parent_id' => $previousID ],
115  [ 'rev_id' => $row->rev_id ],
116  __METHOD__ );
117  $count++;
118  }
119  $blockStart += $this->mBatchSize;
120  $blockEnd += $this->mBatchSize;
121  wfWaitForSlaves();
122  }
123  $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
124 
125  return true;
126  }
127 }
128 
129 $maintClass = "PopulateParentId";
130 require_once RUN_MAINTENANCE_IF_MAIN;
Maintenance\$mBatchSize
int $mBatchSize
Batch size.
Definition: Maintenance.php:103
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$maintClass
$maintClass
Definition: populateParentId.php:129
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
PopulateParentId\__construct
__construct()
Default constructor.
Definition: populateParentId.php:35
PopulateParentId\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: populateParentId.php:40
$res
$res
Definition: database.txt:21
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:3010
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
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1562
PopulateParentId\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: populateParentId.php:44
DB_MASTER
const DB_MASTER
Definition: defines.php:26
PopulateParentId\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: populateParentId.php:48
PopulateParentId
Maintenance script that makes the required database updates for rev_parent_id to be of any use.
Definition: populateParentId.php:34
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:1251
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:392
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373