dd114fdd26748a826f3f757150b7df7ef6ba66b4
3 * Makes the required database updates for rev_parent_id
4 * to be of any use. It can be used for some simple tracking
5 * and to find new page edits by users.
8 require_once( "Maintenance.php" );
10 class PopulateParentId
extends Maintenance
{
13 const BATCH_SIZE
= 200;
15 public function __construct() {
16 parent
::__construct();
17 $this->mDescription
= "Populates rev_parent_id";
20 public function execute() {
21 $db = wfGetDB( DB_MASTER
);
22 if ( !$db->tableExists( 'revision' ) ) {
23 $this->error( "revision table does not exist\n", true );
25 $this->output( "Populating rev_parent_id column\n" );
26 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__
);
27 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__
);
28 if( is_null( $start ) ||
is_null( $end ) ){
29 $this->output( "...revision table seems to be empty.\n" );
30 $db->insert( 'updatelog',
31 array( 'ul_key' => 'populate rev_parent_id' ),
37 $end +
= self
::BATCH_SIZE
- 1;
38 $blockStart = intval( $start );
39 $blockEnd = intval( $start ) + self
::BATCH_SIZE
- 1;
42 while( $blockEnd <= $end ) {
43 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
44 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
45 $res = $db->select( 'revision',
46 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
47 $cond, __FUNCTION__
);
48 # Go through and update rev_parent_id from these rows.
49 # Assume that the previous revision of the title was
50 # the original previous revision of the title when the
52 foreach( $res as $row ) {
53 # First, check rows with the same timestamp other than this one
54 # with a smaller rev ID. The highest ID "wins". This avoids loops
55 # as timestamp can only decrease and never loops with IDs (from parent to parent)
56 $previousID = $db->selectField( 'revision', 'rev_id',
57 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $row->rev_timestamp
,
58 "rev_id < " . intval( $row->rev_id
) ),
60 array( 'ORDER BY' => 'rev_id DESC' ) );
61 # If there are none, check the the highest ID with a lower timestamp
63 # Get the highest older timestamp
64 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
65 array( 'rev_page' => $row->rev_page
, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp
) ),
67 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
68 # If there is one, let the highest rev ID win
69 if( $lastTimestamp ) {
70 $previousID = $db->selectField( 'revision', 'rev_id',
71 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $lastTimestamp ),
73 array( 'ORDER BY' => 'rev_id DESC' ) );
76 $previousID = intval($previousID);
77 if( $previousID != $row->rev_parent_id
)
80 $db->update( 'revision',
81 array( 'rev_parent_id' => $previousID ),
82 array( 'rev_id' => $row->rev_id
),
86 $blockStart +
= self
::BATCH_SIZE
- 1;
87 $blockEnd +
= self
::BATCH_SIZE
- 1;
90 $logged = $db->insert( 'updatelog',
91 array( 'ul_key' => 'populate rev_parent_id' ),
95 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
98 $this->output( "Could not insert rev_parent_id population row.\n" );
104 $maintClass = "PopulateParentId";
105 require_once( DO_MAINTENANCE
);