4 * Makes the required database updates for rev_parent_id
5 * to be of any use. It can be used for some simple tracking
6 * and to find new page edits by users.
9 define( 'BATCH_SIZE', 200 );
11 require_once 'commandLine.inc';
13 $db =& wfGetDB( DB_MASTER
);
14 if ( !$db->tableExists( 'revision' ) ) {
15 echo "revision table does not exist\n";
19 populate_rev_parent_id( $db );
21 function populate_rev_parent_id( $db ) {
22 echo "Populating rev_parent_id column\n";
23 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__
);
24 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__
);
26 $end +
= BATCH_SIZE
- 1;
28 $blockEnd = $start + BATCH_SIZE
- 1;
31 while( $blockEnd <= $end ) {
32 echo "...doing rev_id from $blockStart to $blockEnd\n";
33 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
34 $res = $db->select( 'revision',
35 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
36 $cond, __FUNCTION__
);
37 # Go through and update rev_parent_id from these rows.
38 # Assume that the previous revision of the title was
39 # the original previous revision of the title when the
41 foreach( $res as $row ) {
42 # First, check rows with the same timestamp other than this one
43 # with a smaller rev ID. The highest ID "wins". This avoids loops
44 # as timestamp can only decrease and never loops with IDs (from parent to parent)
45 $previousID = $db->selectField( 'revision', 'rev_id',
46 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $row->rev_timestamp
,
47 "rev_id < {$row->rev_id}" ),
49 array( 'ORDER BY' => 'rev_id DESC' ) );
50 # If there are none, check the the highest ID with a lower timestamp
52 # Get the highest older timestamp
53 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
54 array( 'rev_page' => $row->rev_page
, "rev_timestamp < '{$row->rev_timestamp}'" ),
56 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
57 # If there is one, let the highest rev ID win
58 if( $lastTimestamp ) {
59 $previousID = $db->selectField( 'revision', 'rev_id',
60 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $lastTimestamp ),
62 array( 'ORDER BY' => 'rev_id DESC' ) );
65 $previousID = intval($previousID);
66 if( $previousID != $row->rev_parent_id
)
69 $db->update( 'revision',
70 array( 'rev_parent_id' => $previousID ),
71 array( 'rev_id' => $row->rev_id
),
75 $blockStart +
= BATCH_SIZE
- 1;
76 $blockEnd +
= BATCH_SIZE
- 1;
79 $logged = $db->insert( 'updatelog',
80 array( 'ul_key' => 'populate rev_parent_id' ),
84 echo "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n";
87 echo "Could not insert rev_parent_id population row.\n";