2010-03-03 20:23:42 +00:00
|
|
|
<?php
|
2011-10-11 18:30:50 +00:00
|
|
|
/**
|
2016-05-22 20:48:11 +00:00
|
|
|
* Populates the rev_len and ar_len fields when they are NULL.
|
2010-03-03 20:23:42 +00:00
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-20 14:55:28 +00:00
|
|
|
* @file
|
2010-03-03 20:23:42 +00:00
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
|
|
|
|
|
2018-04-23 09:07:24 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2010-03-03 20:23:42 +00:00
|
|
|
|
2012-08-20 14:55:28 +00:00
|
|
|
/**
|
2016-05-22 20:48:11 +00:00
|
|
|
* Maintenance script that populates the rev_len and ar_len fields when they are NULL.
|
|
|
|
|
* This is the case for all revisions created before MW 1.10, as well as those affected
|
|
|
|
|
* by T18748 (MW 1.10-1.13) and those affected by T135414 (MW 1.21-1.24).
|
2012-08-20 14:55:28 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2011-08-15 18:52:25 +00:00
|
|
|
class PopulateRevisionLength extends LoggedUpdateMaintenance {
|
2010-03-03 20:23:42 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
2016-01-30 02:48:47 +00:00
|
|
|
$this->addDescription( 'Populates the rev_len and ar_len fields' );
|
2011-10-27 18:32:52 +00:00
|
|
|
$this->setBatchSize( 200 );
|
2010-03-03 20:23:42 +00:00
|
|
|
}
|
|
|
|
|
|
2011-08-15 18:52:25 +00:00
|
|
|
protected function getUpdateKey() {
|
2013-03-16 01:37:27 +00:00
|
|
|
return 'populate rev_len and ar_len';
|
2011-08-15 18:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function doDBUpdates() {
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2020-06-07 16:23:08 +00:00
|
|
|
if ( !$dbw->tableExists( 'revision', __METHOD__ ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "revision table does not exist" );
|
2020-06-07 16:23:08 +00:00
|
|
|
} elseif ( !$dbw->tableExists( 'archive', __METHOD__ ) ) {
|
2017-11-20 00:36:54 +00:00
|
|
|
$this->fatalError( "archive table does not exist" );
|
2016-05-22 20:48:11 +00:00
|
|
|
} elseif ( !$dbw->fieldExists( 'revision', 'rev_len', __METHOD__ ) ) {
|
2013-03-16 14:19:41 +00:00
|
|
|
$this->output( "rev_len column does not exist\n\n", true );
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2012-11-22 03:53:24 +00:00
|
|
|
return false;
|
2010-03-03 20:23:42 +00:00
|
|
|
}
|
2012-11-22 03:53:24 +00:00
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$revisionStore = $this->getServiceContainer()->getRevisionStore();
|
2020-03-26 22:40:22 +00:00
|
|
|
|
2010-03-03 20:23:42 +00:00
|
|
|
$this->output( "Populating rev_len column\n" );
|
2020-03-26 22:40:22 +00:00
|
|
|
$rev = $this->doLenUpdates(
|
|
|
|
|
'revision',
|
|
|
|
|
'rev_id',
|
|
|
|
|
'rev',
|
2023-09-26 18:40:35 +00:00
|
|
|
$revisionStore->newSelectQueryBuilder( $this->getDB( DB_REPLICA ) )->joinComment()
|
2020-03-26 22:40:22 +00:00
|
|
|
);
|
2013-03-16 01:37:27 +00:00
|
|
|
|
|
|
|
|
$this->output( "Populating ar_len column\n" );
|
2020-03-26 22:40:22 +00:00
|
|
|
$ar = $this->doLenUpdates(
|
|
|
|
|
'archive',
|
|
|
|
|
'ar_id',
|
|
|
|
|
'ar',
|
2023-09-26 18:40:35 +00:00
|
|
|
$revisionStore->newArchiveSelectQueryBuilder( $this->getDB( DB_REPLICA ) )->joinComment()
|
2020-03-26 22:40:22 +00:00
|
|
|
);
|
2013-03-16 01:37:27 +00:00
|
|
|
|
2014-04-22 21:07:08 +00:00
|
|
|
$this->output( "rev_len and ar_len population complete "
|
|
|
|
|
. "[$rev revision rows, $ar archive rows].\n" );
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2013-03-16 01:37:27 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
2011-08-15 18:52:25 +00:00
|
|
|
|
2013-03-16 01:37:27 +00:00
|
|
|
/**
|
2013-10-28 22:08:50 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $idCol
|
|
|
|
|
* @param string $prefix
|
2023-09-26 18:40:35 +00:00
|
|
|
* @param \Wikimedia\Rdbms\SelectQueryBuilder $queryBuilder should use a replica db
|
2013-03-16 01:37:27 +00:00
|
|
|
* @return int
|
|
|
|
|
*/
|
2023-09-26 18:40:35 +00:00
|
|
|
protected function doLenUpdates( $table, $idCol, $prefix, $queryBuilder ) {
|
2016-09-05 19:55:19 +00:00
|
|
|
$dbr = $this->getDB( DB_REPLICA );
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2017-11-08 03:35:11 +00:00
|
|
|
$batchSize = $this->getBatchSize();
|
2023-07-18 22:56:37 +00:00
|
|
|
$start = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( "MIN($idCol)" )
|
|
|
|
|
->from( $table )
|
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
|
|
|
|
$end = $dbw->newSelectQueryBuilder()
|
|
|
|
|
->select( "MAX($idCol)" )
|
|
|
|
|
->from( $table )
|
|
|
|
|
->caller( __METHOD__ )->fetchField();
|
2011-08-15 18:52:25 +00:00
|
|
|
if ( !$start || !$end ) {
|
2013-03-16 01:37:27 +00:00
|
|
|
$this->output( "...$table table seems to be empty.\n" );
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2013-03-16 01:37:27 +00:00
|
|
|
return 0;
|
2010-03-03 20:23:42 +00:00
|
|
|
}
|
2011-08-15 18:52:25 +00:00
|
|
|
|
2010-03-03 20:23:42 +00:00
|
|
|
# Do remaining chunks
|
|
|
|
|
$blockStart = intval( $start );
|
2017-11-08 03:35:11 +00:00
|
|
|
$blockEnd = intval( $start ) + $batchSize - 1;
|
2010-03-03 20:23:42 +00:00
|
|
|
$count = 0;
|
2013-10-28 22:08:50 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
while ( $blockStart <= $end ) {
|
2013-10-28 22:08:50 +00:00
|
|
|
$this->output( "...doing $idCol from $blockStart to $blockEnd\n" );
|
2023-09-26 18:40:35 +00:00
|
|
|
|
|
|
|
|
$res = $queryBuilder
|
|
|
|
|
->where( [
|
2013-03-16 01:37:27 +00:00
|
|
|
"$idCol >= $blockStart",
|
|
|
|
|
"$idCol <= $blockEnd",
|
2018-04-23 09:07:24 +00:00
|
|
|
$dbr->makeList( [
|
2023-07-17 20:17:57 +00:00
|
|
|
"{$prefix}_len" => null,
|
2018-04-23 09:07:24 +00:00
|
|
|
$dbr->makeList( [
|
2023-07-17 20:17:57 +00:00
|
|
|
"{$prefix}_len" => 0,
|
2020-05-19 22:10:27 +00:00
|
|
|
// sha1( "" )
|
|
|
|
|
"{$prefix}_sha1 != " . $dbr->addQuotes( 'phoiac9h4m842xq45sp7s6u21eteeq1' ),
|
2018-04-23 09:07:24 +00:00
|
|
|
], IDatabase::LIST_AND )
|
|
|
|
|
], IDatabase::LIST_OR )
|
2023-09-26 18:40:35 +00:00
|
|
|
] )
|
|
|
|
|
->caller( __METHOD__ )->fetchResultSet();
|
2013-03-16 01:37:27 +00:00
|
|
|
|
2016-05-22 20:48:11 +00:00
|
|
|
if ( $res->numRows() > 0 ) {
|
|
|
|
|
$this->beginTransaction( $dbw, __METHOD__ );
|
|
|
|
|
# Go through and update rev_len from these rows.
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( $this->upgradeRow( $row, $table, $idCol, $prefix ) ) {
|
|
|
|
|
$count++;
|
|
|
|
|
}
|
2010-03-04 00:27:44 +00:00
|
|
|
}
|
2016-05-22 20:48:11 +00:00
|
|
|
$this->commitTransaction( $dbw, __METHOD__ );
|
2010-03-03 20:23:42 +00:00
|
|
|
}
|
2013-03-16 01:37:27 +00:00
|
|
|
|
2017-11-08 03:35:11 +00:00
|
|
|
$blockStart += $batchSize;
|
|
|
|
|
$blockEnd += $batchSize;
|
2010-03-03 20:23:42 +00:00
|
|
|
}
|
2011-08-15 18:52:25 +00:00
|
|
|
|
2013-03-16 01:37:27 +00:00
|
|
|
return $count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-04-17 20:48:32 +00:00
|
|
|
* @param stdClass $row
|
2013-10-28 22:08:50 +00:00
|
|
|
* @param string $table
|
|
|
|
|
* @param string $idCol
|
|
|
|
|
* @param string $prefix
|
2013-03-16 01:37:27 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected function upgradeRow( $row, $table, $idCol, $prefix ) {
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = $this->getDB( DB_PRIMARY );
|
2013-03-16 01:37:27 +00:00
|
|
|
|
2023-08-31 09:21:12 +00:00
|
|
|
$revFactory = $this->getServiceContainer()->getRevisionFactory();
|
2020-04-01 17:47:23 +00:00
|
|
|
if ( $table === 'archive' ) {
|
2020-04-27 22:29:43 +00:00
|
|
|
$revRecord = $revFactory->newRevisionFromArchiveRow( $row );
|
2020-04-01 17:47:23 +00:00
|
|
|
} else {
|
2020-04-27 22:29:43 +00:00
|
|
|
$revRecord = $revFactory->newRevisionFromRow( $row );
|
2020-04-01 17:47:23 +00:00
|
|
|
}
|
2013-03-16 01:37:27 +00:00
|
|
|
|
2020-04-27 22:29:43 +00:00
|
|
|
if ( !$revRecord ) {
|
2017-02-20 22:48:21 +00:00
|
|
|
# This should not happen, but sometimes does (T22757)
|
2013-03-16 01:37:27 +00:00
|
|
|
$id = $row->$idCol;
|
2020-04-27 22:29:43 +00:00
|
|
|
$this->output( "RevisionRecord of $table $id unavailable!\n" );
|
2014-04-23 18:09:13 +00:00
|
|
|
|
2013-03-16 01:37:27 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Update the row...
|
2023-09-26 18:40:35 +00:00
|
|
|
$dbw->newUpdateQueryBuilder()
|
|
|
|
|
->update( $table )
|
|
|
|
|
->set( [ "{$prefix}_len" => $revRecord->getSize() ] )
|
|
|
|
|
->where( [ $idCol => $row->$idCol ] )
|
|
|
|
|
->caller( __METHOD__ )->execute();
|
2013-03-16 01:37:27 +00:00
|
|
|
|
2011-08-15 18:52:25 +00:00
|
|
|
return true;
|
2010-03-03 20:23:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-13 00:02:09 +00:00
|
|
|
$maintClass = PopulateRevisionLength::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|