MediaWiki  master
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  $batchSize = $this->getBatchSize();
50  $db = $this->getDB( DB_MASTER );
51  if ( !$db->tableExists( 'revision' ) ) {
52  $this->error( "revision table does not exist" );
53 
54  return false;
55  }
56  $this->output( "Populating rev_parent_id column\n" );
57  $start = $db->selectField( 'revision', 'MIN(rev_id)', '', __FUNCTION__ );
58  $end = $db->selectField( 'revision', 'MAX(rev_id)', '', __FUNCTION__ );
59  if ( is_null( $start ) || is_null( $end ) ) {
60  $this->output( "...revision table seems to be empty, nothing to do.\n" );
61 
62  return true;
63  }
64  # Do remaining chunk
65  $blockStart = intval( $start );
66  $blockEnd = intval( $start ) + $batchSize - 1;
67  $count = 0;
68  $changed = 0;
69  while ( $blockStart <= $end ) {
70  $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
71  $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
72  $res = $db->select( 'revision',
73  [ 'rev_id', 'rev_page', 'rev_timestamp', 'rev_parent_id' ],
74  [ $cond, 'rev_parent_id' => null ], __METHOD__ );
75  # Go through and update rev_parent_id from these rows.
76  # Assume that the previous revision of the title was
77  # the original previous revision of the title when the
78  # edit was made...
79  foreach ( $res as $row ) {
80  # First, check rows with the same timestamp other than this one
81  # with a smaller rev ID. The highest ID "wins". This avoids loops
82  # as timestamp can only decrease and never loops with IDs (from parent to parent)
83  $previousID = $db->selectField( 'revision', 'rev_id',
84  [ 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
85  "rev_id < " . intval( $row->rev_id ) ],
86  __METHOD__,
87  [ 'ORDER BY' => 'rev_id DESC' ] );
88  # If there are none, check the highest ID with a lower timestamp
89  if ( !$previousID ) {
90  # Get the highest older timestamp
91  $lastTimestamp = $db->selectField(
92  'revision',
93  'rev_timestamp',
94  [
95  'rev_page' => $row->rev_page,
96  "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp )
97  ],
98  __METHOD__,
99  [ 'ORDER BY' => 'rev_timestamp DESC' ]
100  );
101  # If there is one, let the highest rev ID win
102  if ( $lastTimestamp ) {
103  $previousID = $db->selectField( 'revision', 'rev_id',
104  [ 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ],
105  __METHOD__,
106  [ 'ORDER BY' => 'rev_id DESC' ] );
107  }
108  }
109  $previousID = intval( $previousID );
110  if ( $previousID != $row->rev_parent_id ) {
111  $changed++;
112  }
113  # Update the row...
114  $db->update( 'revision',
115  [ 'rev_parent_id' => $previousID ],
116  [ 'rev_id' => $row->rev_id ],
117  __METHOD__ );
118  $count++;
119  }
120  $blockStart += $batchSize;
121  $blockEnd += $batchSize;
122  wfWaitForSlaves();
123  }
124  $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
125 
126  return true;
127  }
128 }
129 
130 $maintClass = PopulateParentId::class;
131 require_once RUN_MAINTENANCE_IF_MAIN;
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
error( $err, $die=0)
Throw an error to the user.
const DB_MASTER
Definition: defines.php:26
Maintenance script that makes the required database updates for rev_parent_id to be of any use...
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getBatchSize()
Returns batch size.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.