Add pf_memory field
[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', 200 );
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 if( is_null( $start ) || is_null( $end ) ){
26 echo "...revision table seems to be empty.\n";
27 $db->insert( 'updatelog',
28 array( 'ul_key' => 'populate rev_parent_id' ),
29 __FUNCTION__,
30 'IGNORE' );
31 return;
32 }
33 # Do remaining chunk
34 $end += BATCH_SIZE - 1;
35 $blockStart = $start;
36 $blockEnd = $start + BATCH_SIZE - 1;
37 $count = 0;
38 $changed = 0;
39 while( $blockEnd <= $end ) {
40 echo "...doing rev_id from $blockStart to $blockEnd\n";
41 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
42 $res = $db->select( 'revision',
43 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
44 $cond, __FUNCTION__ );
45 # Go through and update rev_parent_id from these rows.
46 # Assume that the previous revision of the title was
47 # the original previous revision of the title when the
48 # edit was made...
49 foreach( $res as $row ) {
50 # First, check rows with the same timestamp other than this one
51 # with a smaller rev ID. The highest ID "wins". This avoids loops
52 # as timestamp can only decrease and never loops with IDs (from parent to parent)
53 $previousID = $db->selectField( 'revision', 'rev_id',
54 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
55 "rev_id < {$row->rev_id}" ),
56 __FUNCTION__,
57 array( 'ORDER BY' => 'rev_id DESC' ) );
58 # If there are none, check the the highest ID with a lower timestamp
59 if( !$previousID ) {
60 # Get the highest older timestamp
61 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
62 array( 'rev_page' => $row->rev_page, "rev_timestamp < '{$row->rev_timestamp}'" ),
63 __FUNCTION__,
64 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
65 # If there is one, let the highest rev ID win
66 if( $lastTimestamp ) {
67 $previousID = $db->selectField( 'revision', 'rev_id',
68 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ),
69 __FUNCTION__,
70 array( 'ORDER BY' => 'rev_id DESC' ) );
71 }
72 }
73 $previousID = intval($previousID);
74 if( $previousID != $row->rev_parent_id )
75 $changed++;
76 # Update the row...
77 $db->update( 'revision',
78 array( 'rev_parent_id' => $previousID ),
79 array( 'rev_id' => $row->rev_id ),
80 __FUNCTION__ );
81 $count++;
82 }
83 $blockStart += BATCH_SIZE - 1;
84 $blockEnd += BATCH_SIZE - 1;
85 wfWaitForSlaves( 5 );
86 }
87 $logged = $db->insert( 'updatelog',
88 array( 'ul_key' => 'populate rev_parent_id' ),
89 __FUNCTION__,
90 'IGNORE' );
91 if( $logged ) {
92 echo "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n";
93 return true;
94 } else {
95 echo "Could not insert rev_parent_id population row.\n";
96 return false;
97 }
98 }
99
100