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