* (bug 13820) Fix updater for rev_parent_id population
[lhc/web/wiklou.git] / maintenance / populateParentId.inc
1 <?php
2
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' ),
11 __FUNCTION__,
12 'IGNORE' );
13 return;
14 }
15 # Do remaining chunk
16 $end += BATCH_SIZE - 1;
17 $blockStart = $start;
18 $blockEnd = $start + BATCH_SIZE - 1;
19 $count = 0;
20 $changed = 0;
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
30 # edit was made...
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}" ),
38 __FUNCTION__,
39 array( 'ORDER BY' => 'rev_id DESC' ) );
40 # If there are none, check the the highest ID with a lower timestamp
41 if( !$previousID ) {
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}'" ),
45 __FUNCTION__,
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 ),
51 __FUNCTION__,
52 array( 'ORDER BY' => 'rev_id DESC' ) );
53 }
54 }
55 $previousID = intval($previousID);
56 if( $previousID != $row->rev_parent_id )
57 $changed++;
58 # Update the row...
59 $db->update( 'revision',
60 array( 'rev_parent_id' => $previousID ),
61 array( 'rev_id' => $row->rev_id ),
62 __FUNCTION__ );
63 $count++;
64 }
65 $blockStart += BATCH_SIZE - 1;
66 $blockEnd += BATCH_SIZE - 1;
67 wfWaitForSlaves( 5 );
68 }
69 $logged = $db->insert( 'updatelog',
70 array( 'ul_key' => 'populate rev_parent_id' ),
71 __FUNCTION__,
72 'IGNORE' );
73 if( $logged ) {
74 echo "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n";
75 return true;
76 } else {
77 echo "Could not insert rev_parent_id population row.\n";
78 return false;
79 }
80 }
81