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_PRIMARY
);
48 if ( !$dbw->tableExists( 'revision', __METHOD__
) ) {
49 $this->fatalError( "revision table does not exist" );
50 } elseif ( !$dbw->tableExists( 'archive', __METHOD__
) ) {
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 $revisionStore = $this->getServiceContainer()->getRevisionStore();
60 $this->output( "Populating rev_len column\n" );
61 $rev = $this->doLenUpdates(
65 $revisionStore->getQueryInfo()
68 $this->output( "Populating ar_len column\n" );
69 $ar = $this->doLenUpdates(
73 $revisionStore->getArchiveQueryInfo()
76 $this->output( "rev_len and ar_len population complete "
77 . "[$rev revision rows, $ar archive rows].\n" );
83 * @param string $table
84 * @param string $idCol
85 * @param string $prefix
86 * @param array $queryInfo
89 protected function doLenUpdates( $table, $idCol, $prefix, $queryInfo ) {
90 $dbr = $this->getDB( DB_REPLICA
);
91 $dbw = $this->getDB( DB_PRIMARY
);
92 $batchSize = $this->getBatchSize();
93 $start = $dbw->newSelectQueryBuilder()
94 ->select( "MIN($idCol)" )
96 ->caller( __METHOD__
)->fetchField();
97 $end = $dbw->newSelectQueryBuilder()
98 ->select( "MAX($idCol)" )
100 ->caller( __METHOD__
)->fetchField();
101 if ( !$start ||
!$end ) {
102 $this->output( "...$table table seems to be empty.\n" );
107 # Do remaining chunks
108 $blockStart = intval( $start );
109 $blockEnd = intval( $start ) +
$batchSize - 1;
112 while ( $blockStart <= $end ) {
113 $this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
115 $queryInfo['tables'],
116 $queryInfo['fields'],
118 "$idCol >= $blockStart",
119 "$idCol <= $blockEnd",
121 "{$prefix}_len" => null,
123 "{$prefix}_len" => 0,
125 "{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ),
126 ], IDatabase
::LIST_AND
)
127 ], IDatabase
::LIST_OR
)
134 if ( $res->numRows() > 0 ) {
135 $this->beginTransaction( $dbw, __METHOD__
);
136 # Go through and update rev_len from these rows.
137 foreach ( $res as $row ) {
138 if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
142 $this->commitTransaction( $dbw, __METHOD__
);
145 $blockStart +
= $batchSize;
146 $blockEnd +
= $batchSize;
153 * @param stdClass $row
154 * @param string $table
155 * @param string $idCol
156 * @param string $prefix
159 protected function upgradeRow( $row, $table, $idCol, $prefix ) {
160 $dbw = $this->getDB( DB_PRIMARY
);
162 $revFactory = $this->getServiceContainer()->getRevisionFactory();
163 if ( $table === 'archive' ) {
164 $revRecord = $revFactory->newRevisionFromArchiveRow( $row );
166 $revRecord = $revFactory->newRevisionFromRow( $row );
170 # This should not happen, but sometimes does (T22757)
172 $this->output( "RevisionRecord of $table $id unavailable!\n" );
178 $dbw->update( $table,
179 [ "{$prefix}_len" => $revRecord->getSize() ],
180 [ $idCol => $row->$idCol ],
188 $maintClass = PopulateRevisionLength
::class;
189 require_once RUN_MAINTENANCE_IF_MAIN
;