3 define( 'BATCH_SIZE', 200 );
5 function populate_rev_parent_id( $db ) {
6 wfOut( "Populating rev_parent_id column\n" );
7 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__ );
8 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__ );
9 if( is_null( $start ) || is_null( $end ) ){
10 wfOut( "...revision table seems to be empty.\n" );
11 $db->insert( 'updatelog',
12 array( 'ul_key' => 'populate rev_parent_id' ),
18 $end += BATCH_SIZE - 1;
19 $blockStart = intval( $start );
20 $blockEnd = intval( $start ) + BATCH_SIZE - 1;
23 while( $blockEnd <= $end ) {
24 wfOut( "...doing rev_id from $blockStart to $blockEnd\n" );
25 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
26 $res = $db->select( 'revision',
27 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
28 $cond, __FUNCTION__ );
29 # Go through and update rev_parent_id from these rows.
30 # Assume that the previous revision of the title was
31 # the original previous revision of the title when the
33 foreach( $res as $row ) {
34 # First, check rows with the same timestamp other than this one
35 # with a smaller rev ID. The highest ID "wins". This avoids loops
36 # as timestamp can only decrease and never loops with IDs (from parent to parent)
37 $previousID = $db->selectField( 'revision', 'rev_id',
38 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
39 "rev_id < " . intval( $row->rev_id ) ),
41 array( 'ORDER BY' => 'rev_id DESC' ) );
42 # If there are none, check the the highest ID with a lower timestamp
44 # Get the highest older timestamp
45 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
46 array( 'rev_page' => $row->rev_page, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp ) ),
48 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
49 # If there is one, let the highest rev ID win
50 if( $lastTimestamp ) {
51 $previousID = $db->selectField( 'revision', 'rev_id',
52 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ),
54 array( 'ORDER BY' => 'rev_id DESC' ) );
57 $previousID = intval($previousID);
58 if( $previousID != $row->rev_parent_id )
61 $db->update( 'revision',
62 array( 'rev_parent_id' => $previousID ),
63 array( 'rev_id' => $row->rev_id ),
67 $blockStart += BATCH_SIZE - 1;
68 $blockEnd += BATCH_SIZE - 1;
71 $logged = $db->insert( 'updatelog',
72 array( 'ul_key' => 'populate rev_parent_id' ),
76 wfOut( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
79 wfOut( "Could not insert rev_parent_id population row.\n" );