]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - maintenance/populateRevisionLength.php
MediaWiki 1.17.0-scripts
[autoinstalls/mediawiki.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                 $missing = 0;
53                 while ( $blockStart <= $end ) {
54                         $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
55                         $res = $db->select( 'revision',
56                                                 Revision::selectFields(),
57                                                 array( "rev_id >= $blockStart",
58                                                    "rev_id <= $blockEnd",
59                                                    "rev_len IS NULL" ),
60                                                 __METHOD__ );
61                         # Go through and update rev_len from these rows.
62                         foreach ( $res as $row ) {
63                                 $rev = new Revision( $row );
64                                 $text = $rev->getRawText();
65                                 if ( !is_string( $text ) ) {
66                                         # This should not happen, but sometimes does (bug 20757)
67                                         $this->output( "Text of revision {$row->rev_id} unavailable!\n" );
68                                         $missing++;
69                                 }
70                                 else {
71                                         # Update the row...
72                                         $db->update( 'revision',
73                                                          array( 'rev_len' => strlen( $text ) ),
74                                                          array( 'rev_id' => $row->rev_id ),
75                                                          __METHOD__ );
76                                         $count++;
77                                 }
78                         }
79                         $blockStart += $this->mBatchSize;
80                         $blockEnd += $this->mBatchSize;
81                         wfWaitForSlaves( 5 );
82                 }
83                 $logged = $db->insert( 'updatelog',
84                         array( 'ul_key' => 'populate rev_len' ),
85                         __METHOD__,
86                         'IGNORE' );
87                 if ( $logged ) {
88                         $this->output( "rev_len population complete ... {$count} rows changed ({$missing} missing)\n" );
89                         return true;
90                 } else {
91                         $this->output( "Could not insert rev_len population row.\n" );
92                         return false;
93                 }
94         }
95 }
96
97 $maintClass = "PopulateRevisionLength";
98 require_once( RUN_MAINTENANCE_IF_MAIN );