3 function populate_rev_parent_id( $db ) {
4 echo "Populating rev_parent_id column\n";
5 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__ );
6 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__ );
7 if( is_null( $start ) || is_null( $end ) ){
8 echo "...revision table seems to be empty.\n";
9 $db->insert( 'updatelog',
10 array( 'ul_key' => 'populate rev_parent_id' ),
16 $end += BATCH_SIZE - 1;
18 $blockEnd = $start + BATCH_SIZE - 1;
21 while( $blockEnd <= $end ) {
22 echo "...doing rev_id from $blockStart to $blockEnd\n";
23 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
24 $res = $db->select( 'revision',
25 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
26 $cond, __FUNCTION__ );
27 # Go through and update rev_parent_id from these rows.
28 # Assume that the previous revision of the title was
29 # the original previous revision of the title when the
31 foreach( $res as $row ) {
32 # First, check rows with the same timestamp other than this one
33 # with a smaller rev ID. The highest ID "wins". This avoids loops
34 # as timestamp can only decrease and never loops with IDs (from parent to parent)
35 $previousID = $db->selectField( 'revision', 'rev_id',
36 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
37 "rev_id < {$row->rev_id}" ),
39 array( 'ORDER BY' => 'rev_id DESC' ) );
40 # If there are none, check the the highest ID with a lower timestamp
42 # Get the highest older timestamp
43 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
44 array( 'rev_page' => $row->rev_page, "rev_timestamp < '{$row->rev_timestamp}'" ),
46 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
47 # If there is one, let the highest rev ID win
48 if( $lastTimestamp ) {
49 $previousID = $db->selectField( 'revision', 'rev_id',
50 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ),
52 array( 'ORDER BY' => 'rev_id DESC' ) );
55 $previousID = intval($previousID);
56 if( $previousID != $row->rev_parent_id )
59 $db->update( 'revision',
60 array( 'rev_parent_id' => $previousID ),
61 array( 'rev_id' => $row->rev_id ),
65 $blockStart += BATCH_SIZE - 1;
66 $blockEnd += BATCH_SIZE - 1;
69 $logged = $db->insert( 'updatelog',
70 array( 'ul_key' => 'populate rev_parent_id' ),
74 echo "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n";
77 echo "Could not insert rev_parent_id population row.\n";