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 $blockStart = intval( $start );
52 $blockEnd = intval( $start ) +
$this->mBatchSize
- 1;
55 while ( $blockStart <= $end ) {
56 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
57 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
58 $res = $db->select( 'revision',
59 array( 'rev_id', 'rev_page', 'rev_timestamp', 'rev_parent_id' ),
61 # Go through and update rev_parent_id from these rows.
62 # Assume that the previous revision of the title was
63 # the original previous revision of the title when the
65 foreach ( $res as $row ) {
66 # First, check rows with the same timestamp other than this one
67 # with a smaller rev ID. The highest ID "wins". This avoids loops
68 # as timestamp can only decrease and never loops with IDs (from parent to parent)
69 $previousID = $db->selectField( 'revision', 'rev_id',
70 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $row->rev_timestamp
,
71 "rev_id < " . intval( $row->rev_id
) ),
73 array( 'ORDER BY' => 'rev_id DESC' ) );
74 # If there are none, check the the highest ID with a lower timestamp
76 # Get the highest older timestamp
77 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
78 array( 'rev_page' => $row->rev_page
, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp
) ),
80 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
81 # If there is one, let the highest rev ID win
82 if ( $lastTimestamp ) {
83 $previousID = $db->selectField( 'revision', 'rev_id',
84 array( 'rev_page' => $row->rev_page
, 'rev_timestamp' => $lastTimestamp ),
86 array( 'ORDER BY' => 'rev_id DESC' ) );
89 $previousID = intval( $previousID );
90 if ( $previousID != $row->rev_parent_id
)
93 $db->update( 'revision',
94 array( 'rev_parent_id' => $previousID ),
95 array( 'rev_id' => $row->rev_id
),
99 $blockStart +
= $this->mBatchSize
;
100 $blockEnd +
= $this->mBatchSize
;
103 $logged = $db->insert( 'updatelog',
104 array( 'ul_key' => 'populate rev_parent_id' ),
108 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
111 $this->output( "Could not insert rev_parent_id population row.\n" );
117 $maintClass = "PopulateParentId";
118 require_once( RUN_MAINTENANCE_IF_MAIN
);