00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 require_once( dirname(__FILE__) . '/Maintenance.php' );
00026
00027 class PopulateParentId extends Maintenance {
00028 public function __construct() {
00029 parent::__construct();
00030 $this->mDescription = "Populates rev_parent_id";
00031 $this->setBatchSize( 200 );
00032 }
00033
00034 public function execute() {
00035 $db = wfGetDB( DB_MASTER );
00036 if ( !$db->tableExists( 'revision' ) ) {
00037 $this->error( "revision table does not exist", true );
00038 }
00039 $this->output( "Populating rev_parent_id column\n" );
00040 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__ );
00041 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__ );
00042 if( is_null( $start ) || is_null( $end ) ){
00043 $this->output( "...revision table seems to be empty.\n" );
00044 $db->insert( 'updatelog',
00045 array( 'ul_key' => 'populate rev_parent_id' ),
00046 __METHOD__,
00047 'IGNORE' );
00048 return;
00049 }
00050 # Do remaining chunk
00051 $end += $this->mBatchSize - 1;
00052 $blockStart = intval( $start );
00053 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
00054 $count = 0;
00055 $changed = 0;
00056 while( $blockEnd <= $end ) {
00057 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
00058 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
00059 $res = $db->select( 'revision',
00060 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
00061 $cond, __METHOD__ );
00062 # Go through and update rev_parent_id from these rows.
00063 # Assume that the previous revision of the title was
00064 # the original previous revision of the title when the
00065 # edit was made...
00066 foreach( $res as $row ) {
00067 # First, check rows with the same timestamp other than this one
00068 # with a smaller rev ID. The highest ID "wins". This avoids loops
00069 # as timestamp can only decrease and never loops with IDs (from parent to parent)
00070 $previousID = $db->selectField( 'revision', 'rev_id',
00071 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
00072 "rev_id < " . intval( $row->rev_id ) ),
00073 __METHOD__,
00074 array( 'ORDER BY' => 'rev_id DESC' ) );
00075 # If there are none, check the the highest ID with a lower timestamp
00076 if( !$previousID ) {
00077 # Get the highest older timestamp
00078 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
00079 array( 'rev_page' => $row->rev_page, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp ) ),
00080 __METHOD__,
00081 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
00082 # If there is one, let the highest rev ID win
00083 if( $lastTimestamp ) {
00084 $previousID = $db->selectField( 'revision', 'rev_id',
00085 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ),
00086 __METHOD__,
00087 array( 'ORDER BY' => 'rev_id DESC' ) );
00088 }
00089 }
00090 $previousID = intval($previousID);
00091 if( $previousID != $row->rev_parent_id )
00092 $changed++;
00093 # Update the row...
00094 $db->update( 'revision',
00095 array( 'rev_parent_id' => $previousID ),
00096 array( 'rev_id' => $row->rev_id ),
00097 __METHOD__ );
00098 $count++;
00099 }
00100 $blockStart += $this->mBatchSize - 1;
00101 $blockEnd += $this->mBatchSize - 1;
00102 wfWaitForSlaves( 5 );
00103 }
00104 $logged = $db->insert( 'updatelog',
00105 array( 'ul_key' => 'populate rev_parent_id' ),
00106 __METHOD__,
00107 'IGNORE' );
00108 if( $logged ) {
00109 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
00110 return true;
00111 } else {
00112 $this->output( "Could not insert rev_parent_id population row.\n" );
00113 return false;
00114 }
00115 }
00116 }
00117
00118 $maintClass = "PopulateParentId";
00119 require_once( DO_MAINTENANCE );