84e99dd882c6157617315f006c0867d0654f47c6
[lhc/web/wiklou.git] / maintenance / populateParentId.php
1 <?php
2
3 /*
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.
7 */
8
9 define( 'BATCH_SIZE', 100 );
10
11 require_once 'commandLine.inc';
12
13 $db =& wfGetDB( DB_MASTER );
14 if ( !$db->tableExists( 'revision' ) ) {
15 echo "revision table does not exist\n";
16 exit( 1 );
17 }
18
19 populate_rev_parent_id( $db );
20
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__ );
25 $blockStart = $start;
26 $blockEnd = $start + BATCH_SIZE - 1;
27 while( $blockEnd <= $end ) {
28 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
29 $res = $db->select( 'revision', array('rev_id', 'rev_page'), $cond, __FUNCTION__ );
30 # Go through and update rev_parent_id from these rows.
31 # Assume that the previous revision of the title was
32 # the original previous revision of the title when the
33 # edit was made...
34 while( $row = $db->fetchObject( $res ) ) {
35 $previousID = $db->selectField( 'revision', 'rev_id',
36 array( 'rev_page' => $row->rev_page, 'rev_id < ' . $row->rev_id ),
37 __FUNCTION__,
38 array( 'ORDER BY' => 'rev_id DESC' ) );
39 # Update the row...
40 $db->update( 'revision',
41 array( 'rev_parent_id' => intval($previousID) ),
42 array( 'rev_page' => $row->rev_page, 'rev_id' => $row->rev_id ),
43 __FUNCTION__ );
44 }
45 $blockStart += BATCH_SIZE;
46 $blockEnd += BATCH_SIZE;
47 wfWaitForSlaves( 5 );
48 }
49 $logged = $db->insert( 'updatelog',
50 array( 'ul_key' => 'populate rev_parent_id' ),
51 __FUNCTION__,
52 'IGNORE' );
53 if( $logged ) {
54 echo "rev_parent_id population complete\n";
55 return true;
56 } else {
57 echo "Could not insert rev_parent_id population row.\n";
58 return false;
59 }
60 }
61
62