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
21 * @ingroup Maintenance
24 use Wikimedia\Rdbms\IDatabase
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that populates the rev_len and ar_len fields when they are NULL.
30 * This is the case for all revisions created before MW 1.10, as well as those affected
31 * by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
33 * @ingroup Maintenance
35 class PopulateRevisionLength
extends LoggedUpdateMaintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( 'Populates the rev_len and ar_len fields' );
39 $this->setBatchSize( 200 );
42 protected function getUpdateKey() {
43 return 'populate rev_len and ar_len';
46 public function doDBUpdates() {
47 $dbw = $this->getDB( DB_MASTER
);
48 if ( !$dbw->tableExists( 'revision' ) ) {
49 $this->fatalError( "revision table does not exist" );
50 } elseif ( !$dbw->tableExists( 'archive' ) ) {
51 $this->fatalError( "archive table does not exist" );
52 } elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__
) ) {
53 $this->output( "rev_len column does not exist\n\n", true );
58 $this->output( "Populating rev_len column\n" );
59 $rev = $this->doLenUpdates( 'revision', 'rev_id', 'rev', Revision
::getQueryInfo() );
61 $this->output( "Populating ar_len column\n" );
62 $ar = $this->doLenUpdates( 'archive', 'ar_id', 'ar', Revision
::getArchiveQueryInfo() );
64 $this->output( "rev_len and ar_len population complete "
65 . "[$rev revision rows, $ar archive rows].\n" );
71 * @param string $table
72 * @param string $idCol
73 * @param string $prefix
74 * @param array $queryInfo
77 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
78 $dbr = $this->getDB( DB_REPLICA
);
79 $dbw = $this->getDB( DB_MASTER
);
80 $batchSize = $this->getBatchSize();
81 $start = $dbw->selectField( $table, "MIN($idCol)", '', __METHOD__
);
82 $end = $dbw->selectField( $table, "MAX($idCol)", '', __METHOD__
);
83 if ( !$start ||
!$end ) {
84 $this->output( "...$table table seems to be empty.\n" );
90 $blockStart = intval( $start );
91 $blockEnd = intval( $start ) +
$batchSize - 1;
94 while ( $blockStart <= $end ) {
95 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
100 "$idCol >= $blockStart",
101 "$idCol <= $blockEnd",
103 "{$prefix}_len IS NULL",
106 "{$prefix}_sha1 != \"phoiac9h4m842xq45sp7s6u21eteeq1\"", // sha1( "" )
107 ], IDatabase
::LIST_AND
)
108 ], IDatabase
::LIST_OR
)
115 if ( $res->numRows() > 0 ) {
116 $this->beginTransaction( $dbw, __METHOD__
);
117 # Go through and update rev_len from these rows.
118 foreach ( $res as $row ) {
119 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
123 $this->commitTransaction( $dbw, __METHOD__
);
126 $blockStart +
= $batchSize;
127 $blockEnd +
= $batchSize;
134 * @param stdClass $row
135 * @param string $table
136 * @param string $idCol
137 * @param string $prefix
140 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
141 $dbw = $this->getDB( DB_MASTER
);
143 $rev = ( $table === 'archive' )
144 ? Revision
::newFromArchiveRow( $row )
145 : new Revision( $row );
147 $content = $rev->getContent( Revision
::RAW
);
149 # This should not happen, but sometimes does (T22757)
151 $this->output( "Content of $table $id unavailable!\n" );
157 $dbw->update( $table,
158 [ "{$prefix}_len" => $content->getSize() ],
159 [ $idCol => $row->$idCol ],
167 $maintClass = PopulateRevisionLength
::class;
168 require_once RUN_MAINTENANCE_IF_MAIN
;