Standardised file description headers:
[lhc/web/wiklou.git] / maintenance / populateParentId.php
1 <?php
2 /*
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.
6 *
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.
11 *
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.
16 *
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
21 *
22 * @ingroup Maintenance
23 */
24
25 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
26
27 class PopulateParentId extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Populates rev_parent_id";
31 $this->setBatchSize( 200 );
32 }
33
34 public function execute() {
35 $db = wfGetDB( DB_MASTER );
36 if ( !$db->tableExists( 'revision' ) ) {
37 $this->error( "revision table does not exist", true );
38 }
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' ),
46 __METHOD__,
47 'IGNORE' );
48 return;
49 }
50 # Do remaining chunk
51 $blockStart = intval( $start );
52 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
53 $count = 0;
54 $changed = 0;
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' ),
60 $cond, __METHOD__ );
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
64 # edit was made...
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 ) ),
72 __METHOD__,
73 array( 'ORDER BY' => 'rev_id DESC' ) );
74 # If there are none, check the the highest ID with a lower timestamp
75 if ( !$previousID ) {
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 ) ),
79 __METHOD__,
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 ),
85 __METHOD__,
86 array( 'ORDER BY' => 'rev_id DESC' ) );
87 }
88 }
89 $previousID = intval( $previousID );
90 if ( $previousID != $row->rev_parent_id )
91 $changed++;
92 # Update the row...
93 $db->update( 'revision',
94 array( 'rev_parent_id' => $previousID ),
95 array( 'rev_id' => $row->rev_id ),
96 __METHOD__ );
97 $count++;
98 }
99 $blockStart += $this->mBatchSize;
100 $blockEnd += $this->mBatchSize;
101 wfWaitForSlaves( 5 );
102 }
103 $logged = $db->insert( 'updatelog',
104 array( 'ul_key' => 'populate rev_parent_id' ),
105 __METHOD__,
106 'IGNORE' );
107 if ( $logged ) {
108 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
109 return true;
110 } else {
111 $this->output( "Could not insert rev_parent_id population row.\n" );
112 return false;
113 }
114 }
115 }
116
117 $maintClass = "PopulateParentId";
118 require_once( DO_MAINTENANCE );