r66153 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66152‎ | r66153 | r66154 >
Date:19:09, 10 May 2010
Author:jeluf
Status:deferred
Tags:
Comment:
Backport from HEAD
Modified paths:
  • /branches/wmf/1.16wmf4/maintenance/populateRevisionLength.php (added) (history)

Diff [purge]

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

Status & tagging log