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.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
25 require_once( dirname(__FILE__
) . '/Maintenance.php' );
27 class PopulateParentId
extends Maintenance
{
28 public function __construct() {
29 parent
::__construct();
30 $this->mDescription
= "Populates rev_parent_id";
31 $this->setBatchSize( 200 );
34 public function execute() {
35 $db = wfGetDB( DB_MASTER
);
36 if ( !$db->tableExists( 'revision' ) ) {
37 $this->error( "revision table does not exist", true );
39 $this->output( "Populating rev_parent_id column\n" );
40 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__
);
41 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__
);
42 if( is_null( $start ) ||
is_null( $end ) ){
43 $this->output( "...revision table seems to be empty.\n" );
44 $db->insert( 'updatelog',
45 array( 'ul_key' => 'populate rev_parent_id' ),
51 $end +
= $this->mBatchSize
- 1;
52 $blockStart = intval( $start );
53 $blockEnd = intval( $start ) +
$this->mBatchSize
- 1;
56 while( $blockEnd <= $end ) {
57 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
58 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
59 $res = $db->select( 'revision',
60 array('rev_id','rev_page','rev_timestamp','rev_parent_id'),
62 # Go through and update rev_parent_id from these rows.
63 # Assume that the previous revision of the title was
64 # the original previous revision of the title when the
66 foreach( $res as $row ) {
67 # First, check rows with the same timestamp other than this one
68 # with a smaller rev ID. The highest ID "wins". This avoids loops
69 # as timestamp can only decrease and never loops with IDs (from parent to parent)
70 $previousID = $db->selectField( 'revision', 'rev_id',
71 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $row->rev_timestamp
,
72 "rev_id < " . intval( $row->rev_id
) ),
74 array( 'ORDER BY' => 'rev_id DESC' ) );
75 # If there are none, check the the highest ID with a lower timestamp
77 # Get the highest older timestamp
78 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
79 array( 'rev_page' => $row->rev_page
, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp
) ),
81 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
82 # If there is one, let the highest rev ID win
83 if( $lastTimestamp ) {
84 $previousID = $db->selectField( 'revision', 'rev_id',
85 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $lastTimestamp ),
87 array( 'ORDER BY' => 'rev_id DESC' ) );
90 $previousID = intval($previousID);
91 if( $previousID != $row->rev_parent_id
)
94 $db->update( 'revision',
95 array( 'rev_parent_id' => $previousID ),
96 array( 'rev_id' => $row->rev_id
),
100 $blockStart +
= $this->mBatchSize
- 1;
101 $blockEnd +
= $this->mBatchSize
- 1;
102 wfWaitForSlaves( 5 );
104 $logged = $db->insert( 'updatelog',
105 array( 'ul_key' => 'populate rev_parent_id' ),
109 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
112 $this->output( "Could not insert rev_parent_id population row.\n" );
118 $maintClass = "PopulateParentId";
119 require_once( DO_MAINTENANCE
);