(bug 18881) add maintenance script to populate rev_len field for old revisions
[lhc/web/wiklou.git] / maintenance / populateRevisionLength.php
1 <?php
2 /*
3 * Makes the required database updates for rev_len
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 PopulateRevisionLength extends Maintenance {
28 public function __construct() {
29 parent::__construct();
30 $this->mDescription = "Populates rev_len";
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_len 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_len' ),
46 __METHOD__,
47 'IGNORE' );
48 return;
49 }
50 # Do remaining chunks
51 $blockStart = intval( $start );
52 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
53 $count = 0;
54 while( $blockStart <= $end ) {
55 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
56 $res = $db->select( 'revision',
57 Revision::selectFields(),
58 array( "rev_id >= $blockStart",
59 "rev_id <= $blockEnd",
60 "rev_len IS NULL" ),
61 __METHOD__ );
62 # Go through and update rev_len from these rows.
63 foreach( $res as $row ) {
64 $rev = new Revision( $row );
65 $text = $rev->getRawText();
66 # Update the row...
67 $db->update( 'revision',
68 array( 'rev_len' => strlen( $text ) ),
69 array( 'rev_id' => $row->rev_id ),
70 __METHOD__ );
71 $count++;
72 }
73 $blockStart += $this->mBatchSize;
74 $blockEnd += $this->mBatchSize;
75 wfWaitForSlaves( 5 );
76 }
77 $logged = $db->insert( 'updatelog',
78 array( 'ul_key' => 'populate rev_len' ),
79 __METHOD__,
80 'IGNORE' );
81 if( $logged ) {
82 $this->output( "rev_len population complete ... {$count} rows changed\n" );
83 return true;
84 } else {
85 $this->output( "Could not insert rev_len population row.\n" );
86 return false;
87 }
88 }
89 }
90
91 $maintClass = "PopulateRevisionLength";
92 require_once( DO_MAINTENANCE );