call populateRevisionLength.php from updaters.inc; make updaters.inc use populate...
[lhc/web/wiklou.git] / maintenance / populateRevisionLength.php
1 <?php
2 /*
3 * Populates the rev_len field for old revisions created before MW 1.10.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @ingroup Maintenance
21 */
22
23 require_once( dirname(__FILE__) . '/Maintenance.php' );
24
25 class PopulateRevisionLength extends Maintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Populates rev_len";
29 $this->setBatchSize( 200 );
30 }
31
32 public function execute() {
33 $db = wfGetDB( DB_MASTER );
34 if ( !$db->tableExists( 'revision' ) ) {
35 $this->error( "revision table does not exist", true );
36 }
37 $this->output( "Populating rev_len column\n" );
38 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __FUNCTION__ );
39 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __FUNCTION__ );
40 if( is_null( $start ) || is_null( $end ) ){
41 $this->output( "...revision table seems to be empty.\n" );
42 $db->insert( 'updatelog',
43 array( 'ul_key' => 'populate rev_len' ),
44 __METHOD__,
45 'IGNORE' );
46 return;
47 }
48 # Do remaining chunks
49 $blockStart = intval( $start );
50 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
51 $count = 0;
52 while( $blockStart <= $end ) {
53 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
54 $res = $db->select( 'revision',
55 Revision::selectFields(),
56 array( "rev_id >= $blockStart",
57 "rev_id <= $blockEnd",
58 "rev_len IS NULL" ),
59 __METHOD__ );
60 # Go through and update rev_len from these rows.
61 foreach( $res as $row ) {
62 $rev = new Revision( $row );
63 $text = $rev->getRawText();
64 # Update the row...
65 $db->update( 'revision',
66 array( 'rev_len' => strlen( $text ) ),
67 array( 'rev_id' => $row->rev_id ),
68 __METHOD__ );
69 $count++;
70 }
71 $blockStart += $this->mBatchSize;
72 $blockEnd += $this->mBatchSize;
73 wfWaitForSlaves( 5 );
74 }
75 $logged = $db->insert( 'updatelog',
76 array( 'ul_key' => 'populate rev_len' ),
77 __METHOD__,
78 'IGNORE' );
79 if( $logged ) {
80 $this->output( "rev_len population complete ... {$count} rows changed\n" );
81 return true;
82 } else {
83 $this->output( "Could not insert rev_len population row.\n" );
84 return false;
85 }
86 }
87 }
88
89 $maintClass = "PopulateRevisionLength";
90 require_once( DO_MAINTENANCE );