f562df472e12e844cbe69c76756dff5e8a1549d7
[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 LoggedUpdateMaintenance {
26 public function __construct() {
27 parent::__construct();
28 $this->mDescription = "Populates the rev_len field";
29 }
30
31 protected function getUpdateKey() {
32 return 'populate rev_len';
33 }
34
35 protected function updateSkippedMessage() {
36 return 'rev_len column of revision table already populated.';
37 }
38
39 public function doDBUpdates() {
40 $db = $this->getDB( DB_MASTER );
41 if ( !$db->tableExists( 'revision' ) ) {
42 $this->error( "revision table does not exist", true );
43 }
44 $this->output( "Populating rev_len column\n" );
45
46 $start = $db->selectField( 'revision', 'MIN(rev_id)', false, __METHOD__ );
47 $end = $db->selectField( 'revision', 'MAX(rev_id)', false, __METHOD__ );
48 if ( !$start || !$end ) {
49 $this->output( "...revision table seems to be empty.\n" );
50 return true;
51 }
52
53 # Do remaining chunks
54 $blockStart = intval( $start );
55 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
56 $count = 0;
57 $missing = 0;
58 while ( $blockStart <= $end ) {
59 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
60 $res = $db->select( 'revision',
61 Revision::selectFields(),
62 array( "rev_id >= $blockStart",
63 "rev_id <= $blockEnd",
64 "rev_len IS NULL" ),
65 __METHOD__ );
66 # Go through and update rev_len from these rows.
67 foreach ( $res as $row ) {
68 $rev = new Revision( $row );
69 $text = $rev->getRawText();
70 if ( !is_string( $text ) ) {
71 # This should not happen, but sometimes does (bug 20757)
72 $this->output( "Text of revision {$row->rev_id} unavailable!\n" );
73 $missing++;
74 }
75 else {
76 # Update the row...
77 $db->update( 'revision',
78 array( 'rev_len' => strlen( $text ) ),
79 array( 'rev_id' => $row->rev_id ),
80 __METHOD__ );
81 $count++;
82 }
83 }
84 $blockStart += $this->mBatchSize;
85 $blockEnd += $this->mBatchSize;
86 wfWaitForSlaves();
87 }
88
89 $this->output( "rev_len population complete ... {$count} rows changed ({$missing} missing)\n" );
90 return true;
91 }
92 }
93
94 $maintClass = "PopulateRevisionLength";
95 require_once( RUN_MAINTENANCE_IF_MAIN );