split getHooksFromDoc() function
[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 LoggedUpdateMaintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Populates rev_parent_id";
31 }
32
33 protected function getUpdateKey() {
34 return 'populate rev_parent_id';
35 }
36
37 protected function updateSkippedMessage() {
38 return 'rev_parent_id column of revision table already populated.';
39 }
40
41 protected function doDBUpdates() {
42 $db = wfGetDB( DB_MASTER );
43 if ( !$db->tableExists( 'revision' ) ) {
44 $this->error( "revision table does not exist" );
45 return false;
46 }
47 $this->output( "Populating rev_parent_id column\n" );
48 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__ );
49 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__ );
50 if ( is_null( $start ) || is_null( $end ) ) {
51 $this->output( "...revision table seems to be empty, nothing to do.\n" );
52 return true;
53 }
54 # Do remaining chunk
55 $blockStart = intval( $start );
56 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
57 $count = 0;
58 $changed = 0;
59 while ( $blockStart <= $end ) {
60 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
61 $cond = "rev_id BETWEEN $blockStart AND $blockEnd";
62 $res = $db->select( 'revision',
63 array( 'rev_id', 'rev_page', 'rev_timestamp', 'rev_parent_id' ),
64 $cond, __METHOD__ );
65 # Go through and update rev_parent_id from these rows.
66 # Assume that the previous revision of the title was
67 # the original previous revision of the title when the
68 # edit was made...
69 foreach ( $res as $row ) {
70 # First, check rows with the same timestamp other than this one
71 # with a smaller rev ID. The highest ID "wins". This avoids loops
72 # as timestamp can only decrease and never loops with IDs (from parent to parent)
73 $previousID = $db->selectField( 'revision', 'rev_id',
74 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $row->rev_timestamp,
75 "rev_id < " . intval( $row->rev_id ) ),
76 __METHOD__,
77 array( 'ORDER BY' => 'rev_id DESC' ) );
78 # If there are none, check the the highest ID with a lower timestamp
79 if ( !$previousID ) {
80 # Get the highest older timestamp
81 $lastTimestamp = $db->selectField( 'revision', 'rev_timestamp',
82 array( 'rev_page' => $row->rev_page, "rev_timestamp < " . $db->addQuotes( $row->rev_timestamp ) ),
83 __METHOD__,
84 array( 'ORDER BY' => 'rev_timestamp DESC' ) );
85 # If there is one, let the highest rev ID win
86 if ( $lastTimestamp ) {
87 $previousID = $db->selectField( 'revision', 'rev_id',
88 array( 'rev_page' => $row->rev_page, 'rev_timestamp' => $lastTimestamp ),
89 __METHOD__,
90 array( 'ORDER BY' => 'rev_id DESC' ) );
91 }
92 }
93 $previousID = intval( $previousID );
94 if ( $previousID != $row->rev_parent_id )
95 $changed++;
96 # Update the row...
97 $db->update( 'revision',
98 array( 'rev_parent_id' => $previousID ),
99 array( 'rev_id' => $row->rev_id ),
100 __METHOD__ );
101 $count++;
102 }
103 $blockStart += $this->mBatchSize;
104 $blockEnd += $this->mBatchSize;
105 wfWaitForSlaves();
106 }
107 $this->output( "rev_parent_id population complete ... {$count} rows [{$changed} changed]\n" );
108 return true;
109 }
110 }
111
112 $maintClass = "PopulateParentId";
113 require_once( RUN_MAINTENANCE_IF_MAIN );