]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/populateParentId.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.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                 $end += $this->mBatchSize - 1;
52                 $blockStart = intval( $start );
53                 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
54                 $count = 0;
55                 $changed = 0;
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'), 
61                                 $cond, __METHOD__ );
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
65                         # edit was made...
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 ) ), 
73                                         __METHOD__,
74                                         array( 'ORDER BY' => 'rev_id DESC' ) );
75                                 # If there are none, check the the highest ID with a lower timestamp
76                                 if( !$previousID ) {
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 ) ), 
80                                                 __METHOD__,
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 ), 
86                                                         __METHOD__,
87                                                         array( 'ORDER BY' => 'rev_id DESC' ) );
88                                         }
89                                 }
90                                 $previousID = intval($previousID);
91                                 if( $previousID != $row->rev_parent_id )
92                                         $changed++;
93                                 # Update the row...
94                                 $db->update( 'revision',
95                                         array( 'rev_parent_id' => $previousID ),
96                                         array( 'rev_id' => $row->rev_id ),
97                                         __METHOD__ );
98                                 $count++;
99                         }
100                         $blockStart += $this->mBatchSize - 1;
101                         $blockEnd += $this->mBatchSize - 1;
102                         wfWaitForSlaves( 5 );
103                 }
104                 $logged = $db->insert( 'updatelog',
105                         array( 'ul_key' => 'populate rev_parent_id' ),
106                         __METHOD__,
107                         'IGNORE' );
108                 if( $logged ) {
109                         $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
110                         return true;
111                 } else {
112                         $this->output( "Could not insert rev_parent_id population row.\n" );
113                         return false;
114                 }
115         }
116 }
117
118 $maintClass = "PopulateParentId";
119 require_once( DO_MAINTENANCE );