Implement extension registration from an extension.json file
[mediawiki.git] / maintenance / populateRevisionLength.php
blobb73ac7f270da219b956756f6398a7a16f10ae023
1 <?php
2 /**
3 * Populates the rev_len and ar_len fields for old revisions created
4 * before MW 1.10.
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
21 * @file
22 * @ingroup Maintenance
25 require_once __DIR__ . '/Maintenance.php';
27 /**
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 );
53 return false;
56 $this->output( "Populating rev_len column\n" );
57 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision::selectFields() );
59 $this->output( "Populating ar_len column\n" );
60 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision::selectArchiveFields() );
62 $this->output( "rev_len and ar_len population complete "
63 . "[$rev revision rows, $ar archive rows].\n" );
65 return true;
68 /**
69 * @param string $table
70 * @param string $idCol
71 * @param string $prefix
72 * @param array $fields
73 * @return int
75 protected function doLenUpdates( $table, $idCol, $prefix, $fields ) {
76 $db = $this->getDB( DB_MASTER );
77 $start = $db->selectField( $table, "MIN($idCol)", false, __METHOD__ );
78 $end = $db->selectField( $table, "MAX($idCol)", false, __METHOD__ );
79 if ( !$start || !$end ) {
80 $this->output( "...$table table seems to be empty.\n" );
82 return 0;
85 # Do remaining chunks
86 $blockStart = intval( $start );
87 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
88 $count = 0;
90 while ( $blockStart <= $end ) {
91 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
92 $res = $db->select(
93 $table,
94 $fields,
95 array(
96 "$idCol >= $blockStart",
97 "$idCol <= $blockEnd",
98 "{$prefix}_len IS NULL"
100 __METHOD__
103 $db->begin( __METHOD__ );
104 # Go through and update rev_len from these rows.
105 foreach ( $res as $row ) {
106 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
107 $count++;
110 $db->commit( __METHOD__ );
112 $blockStart += $this->mBatchSize;
113 $blockEnd += $this->mBatchSize;
114 wfWaitForSlaves();
117 return $count;
121 * @param stdClass $row
122 * @param string $table
123 * @param string $idCol
124 * @param string $prefix
125 * @return bool
127 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
128 $db = $this->getDB( DB_MASTER );
130 $rev = ( $table === 'archive' )
131 ? Revision::newFromArchiveRow( $row )
132 : new Revision( $row );
134 $content = $rev->getContent();
135 if ( !$content ) {
136 # This should not happen, but sometimes does (bug 20757)
137 $id = $row->$idCol;
138 $this->output( "Content of $table $id unavailable!\n" );
140 return false;
143 # Update the row...
144 $db->update( $table,
145 array( "{$prefix}_len" => $content->getSize() ),
146 array( $idCol => $row->$idCol ),
147 __METHOD__
150 return true;
154 $maintClass = "PopulateRevisionLength";
155 require_once RUN_MAINTENANCE_IF_MAIN;