Merge "Improve sorting on SpecialWanted*-Pages"
[mediawiki.git] / maintenance / populateRevisionLength.php
bloba9457c2a1cd13f65b078bc4b734b4174f526fba2
1 <?php
2 /**
3 * Populates the rev_len and ar_len fields when they are NULL.
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.
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.
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
20 * @file
21 * @ingroup Maintenance
24 require_once __DIR__ . '/Maintenance.php';
26 /**
27 * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
28 * This is the case for all revisions created before MW 1.10, as well as those affected
29 * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
31 * @ingroup Maintenance
33 class PopulateRevisionLength extends LoggedUpdateMaintenance {
34 public function __construct() {
35 parent::__construct();
36 $this->addDescription( '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 $dbw = $this->getDB( DB_MASTER );
46 if ( !$dbw->tableExists( 'revision' ) ) {
47 $this->error( "revision table does not exist", true );
48 } elseif ( !$dbw->tableExists( 'archive' ) ) {
49 $this->error( "archive table does not exist", true );
50 } elseif ( !$dbw->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 $dbr = $this->getDB( DB_REPLICA );
77 $dbw = $this->getDB( DB_MASTER );
78 $start = $dbw->selectField( $table, "MIN($idCol)", false, __METHOD__ );
79 $end = $dbw->selectField( $table, "MAX($idCol)", false, __METHOD__ );
80 if ( !$start || !$end ) {
81 $this->output( "...$table table seems to be empty.\n" );
83 return 0;
86 # Do remaining chunks
87 $blockStart = intval( $start );
88 $blockEnd = intval( $start ) + $this->mBatchSize - 1;
89 $count = 0;
91 while ( $blockStart <= $end ) {
92 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
93 $res = $dbr->select(
94 $table,
95 $fields,
97 "$idCol >= $blockStart",
98 "$idCol <= $blockEnd",
99 "{$prefix}_len IS NULL"
101 __METHOD__
104 if ( $res->numRows() > 0 ) {
105 $this->beginTransaction( $dbw, __METHOD__ );
106 # Go through and update rev_len from these rows.
107 foreach ( $res as $row ) {
108 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
109 $count++;
112 $this->commitTransaction( $dbw, __METHOD__ );
115 $blockStart += $this->mBatchSize;
116 $blockEnd += $this->mBatchSize;
117 wfWaitForSlaves();
120 return $count;
124 * @param stdClass $row
125 * @param string $table
126 * @param string $idCol
127 * @param string $prefix
128 * @return bool
130 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
131 $dbw = $this->getDB( DB_MASTER );
133 $rev = ( $table === 'archive' )
134 ? Revision::newFromArchiveRow( $row )
135 : new Revision( $row );
137 $content = $rev->getContent();
138 if ( !$content ) {
139 # This should not happen, but sometimes does (T22757)
140 $id = $row->$idCol;
141 $this->output( "Content of $table $id unavailable!\n" );
143 return false;
146 # Update the row...
147 $dbw->update( $table,
148 [ "{$prefix}_len" => $content->getSize() ],
149 [ $idCol => $row->$idCol ],
150 __METHOD__
153 return true;
157 $maintClass = "PopulateRevisionLength";
158 require_once RUN_MAINTENANCE_IF_MAIN;