3 * Populates the rev_len and ar_len fields for old revisions created
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.
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.
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
22 * @ingroup Maintenance
25 require_once __DIR__
. '/Maintenance.php';
28 * Maintenance script that populates the rev_len and ar_len fields
29 * for old revisions created before MW 1.10.
31 * @ingroup Maintenance
33 class PopulateRevisionLength
extends LoggedUpdateMaintenance
{
34 public function __construct() {
35 parent
::__construct();
36 $this->mDescription
= "Populates the rev_len and ar_len fields";
37 $this->setBatchSize( 200 );
40 protected function getUpdateKey() {
41 return 'populate rev_len and ar_len';
44 public function doDBUpdates() {
45 $db = $this->getDB( DB_MASTER
);
46 if ( !$db->tableExists( 'revision' ) ) {
47 $this->error( "revision table does not exist", true );
48 } elseif ( !$db->tableExists( 'archive' ) ) {
49 $this->error( "archive table does not exist", true );
50 } elseif ( !$db->fieldExists( 'revision', 'rev_len', __METHOD__
) ) {
51 $this->output( "rev_len column does not exist\n\n", true );
55 $this->output( "Populating rev_len column\n" );
56 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev' );
58 $this->output( "Populating ar_len column\n" );
59 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar' );
61 $this->output( "rev_len and ar_len population complete [$rev revision rows, $ar archive rows].\n" );
71 protected function doLenUpdates( $table, $idCol, $prefix ) {
72 $db = $this->getDB( DB_MASTER
);
73 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__
);
74 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__
);
75 if ( !$start ||
!$end ) {
76 $this->output( "...$table table seems to be empty.\n" );
81 $blockStart = intval( $start );
82 $blockEnd = intval( $start ) +
$this->mBatchSize
- 1;
84 $fields = Revision
::selectFields();
85 while ( $blockStart <= $end ) {
86 $this->output( "...doing rev_id from $blockStart to $blockEnd\n" );
91 "$idCol >= $blockStart",
92 "$idCol <= $blockEnd",
93 "{$prefix}_len IS NULL"
98 $db->begin( __METHOD__
);
99 # Go through and update rev_len from these rows.
100 foreach ( $res as $row ) {
101 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
105 $db->commit( __METHOD__
);
107 $blockStart +
= $this->mBatchSize
;
108 $blockEnd +
= $this->mBatchSize
;
122 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
123 $db = $this->getDB( DB_MASTER
);
125 $rev = ( $table === 'archive' )
126 ? Revision
::newFromArchiveRow( $row )
127 : new Revision( $row );
129 $content = $rev->getContent();
131 # This should not happen, but sometimes does (bug 20757)
133 $this->output( "Content of $table $id unavailable!\n" );
139 array( "{$prefix}_len" => $content->getSize() ),
140 array( $idCol => $row->$idCol ),
148 $maintClass = "PopulateRevisionLength";
149 require_once RUN_MAINTENANCE_IF_MAIN
;