123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- /**
- * Migrate comments from pre-1.30 columns to the 'comment' table
- *
- * 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
- *
- * @file
- * @ingroup Maintenance
- */
- use Wikimedia\Rdbms\IDatabase;
- require_once __DIR__ . '/Maintenance.php';
- /**
- * Maintenance script that migrates comments from pre-1.30 columns to the
- * 'comment' table
- *
- * @ingroup Maintenance
- */
- class MigrateComments extends LoggedUpdateMaintenance {
- public function __construct() {
- parent::__construct();
- $this->addDescription( 'Migrates comments from pre-1.30 columns to the \'comment\' table' );
- $this->setBatchSize( 100 );
- }
- protected function getUpdateKey() {
- return __CLASS__;
- }
- protected function updateSkippedMessage() {
- return 'comments already migrated.';
- }
- protected function doDBUpdates() {
- global $wgCommentTableSchemaMigrationStage;
- if ( $wgCommentTableSchemaMigrationStage < MIGRATION_WRITE_NEW ) {
- $this->output(
- "...cannot update while \$wgCommentTableSchemaMigrationStage < MIGRATION_WRITE_NEW\n"
- );
- return false;
- }
- $this->migrateToTemp(
- 'revision', 'rev_id', 'rev_comment', 'revcomment_rev', 'revcomment_comment_id'
- );
- $this->migrate( 'archive', 'ar_id', 'ar_comment' );
- $this->migrate( 'ipblocks', 'ipb_id', 'ipb_reason' );
- $this->migrateToTemp(
- 'image', 'img_name', 'img_description', 'imgcomment_name', 'imgcomment_description_id'
- );
- $this->migrate( 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_description' );
- $this->migrate( 'filearchive', 'fa_id', 'fa_deleted_reason' );
- $this->migrate( 'filearchive', 'fa_id', 'fa_description' );
- $this->migrate( 'recentchanges', 'rc_id', 'rc_comment' );
- $this->migrate( 'logging', 'log_id', 'log_comment' );
- $this->migrate( 'protected_titles', [ 'pt_namespace', 'pt_title' ], 'pt_reason' );
- return true;
- }
- /**
- * Fetch comment IDs for a set of comments
- * @param IDatabase $dbw
- * @param array &$comments Keys are comment names, values will be set to IDs.
- * @return int Count of added comments
- */
- private function loadCommentIDs( IDatabase $dbw, array &$comments ) {
- $count = 0;
- $needComments = $comments;
- while ( true ) {
- $where = [];
- foreach ( $needComments as $need => $dummy ) {
- $where[] = $dbw->makeList(
- [
- 'comment_hash' => CommentStore::hash( $need, null ),
- 'comment_text' => $need,
- ],
- LIST_AND
- );
- }
- $res = $dbw->select(
- 'comment',
- [ 'comment_id', 'comment_text' ],
- [
- $dbw->makeList( $where, LIST_OR ),
- 'comment_data' => null,
- ],
- __METHOD__
- );
- foreach ( $res as $row ) {
- $comments[$row->comment_text] = $row->comment_id;
- unset( $needComments[$row->comment_text] );
- }
- if ( !$needComments ) {
- break;
- }
- $dbw->insert(
- 'comment',
- array_map( function ( $v ) {
- return [
- 'comment_hash' => CommentStore::hash( $v, null ),
- 'comment_text' => $v,
- ];
- }, array_keys( $needComments ) ),
- __METHOD__
- );
- $count += $dbw->affectedRows();
- }
- return $count;
- }
- /**
- * Migrate comments in a table.
- *
- * Assumes any row with the ID field non-zero have already been migrated.
- * Assumes the new field name is the same as the old with '_id' appended.
- * Blanks the old fields while migrating.
- *
- * @param string $table Table to migrate
- * @param string|string[] $primaryKey Primary key of the table.
- * @param string $oldField Old comment field name
- */
- protected function migrate( $table, $primaryKey, $oldField ) {
- $newField = $oldField . '_id';
- $primaryKey = (array)$primaryKey;
- $pkFilter = array_flip( $primaryKey );
- $this->output( "Beginning migration of $table.$oldField to $table.$newField\n" );
- wfWaitForSlaves();
- $dbw = $this->getDB( DB_MASTER );
- $next = '1=1';
- $countUpdated = 0;
- $countComments = 0;
- while ( true ) {
- // Fetch the rows needing update
- $res = $dbw->select(
- $table,
- array_merge( $primaryKey, [ $oldField ] ),
- [
- $newField => 0,
- $next,
- ],
- __METHOD__,
- [
- 'ORDER BY' => $primaryKey,
- 'LIMIT' => $this->getBatchSize(),
- ]
- );
- if ( !$res->numRows() ) {
- break;
- }
- // Collect the distinct comments from those rows
- $comments = [];
- foreach ( $res as $row ) {
- $comments[$row->$oldField] = 0;
- }
- $countComments += $this->loadCommentIDs( $dbw, $comments );
- // Update the existing rows
- foreach ( $res as $row ) {
- $dbw->update(
- $table,
- [
- $newField => $comments[$row->$oldField],
- $oldField => '',
- ],
- array_intersect_key( (array)$row, $pkFilter ) + [
- $newField => 0
- ],
- __METHOD__
- );
- $countUpdated += $dbw->affectedRows();
- }
- // Calculate the "next" condition
- $next = '';
- $prompt = [];
- for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
- $field = $primaryKey[$i];
- $prompt[] = $row->$field;
- $value = $dbw->addQuotes( $row->$field );
- if ( $next === '' ) {
- $next = "$field > $value";
- } else {
- $next = "$field > $value OR $field = $value AND ($next)";
- }
- }
- $prompt = implode( ' ', array_reverse( $prompt ) );
- $this->output( "... $prompt\n" );
- wfWaitForSlaves();
- }
- $this->output(
- "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
- );
- }
- /**
- * Migrate comments in a table to a temporary table.
- *
- * Assumes any row with the ID field non-zero have already been migrated.
- * Assumes the new table is named "{$table}_comment_temp", and it has two
- * columns, in order, being the primary key of the original table and the
- * comment ID field.
- * Blanks the old fields while migrating.
- *
- * @param string $table Table to migrate
- * @param string $primaryKey Primary key of the table.
- * @param string $oldField Old comment field name
- * @param string $newPrimaryKey Primary key of the new table.
- * @param string $newField New comment field name
- */
- protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
- $newTable = $table . '_comment_temp';
- $this->output( "Beginning migration of $table.$oldField to $newTable.$newField\n" );
- wfWaitForSlaves();
- $dbw = $this->getDB( DB_MASTER );
- $next = [];
- $countUpdated = 0;
- $countComments = 0;
- while ( true ) {
- // Fetch the rows needing update
- $res = $dbw->select(
- [ $table, $newTable ],
- [ $primaryKey, $oldField ],
- [ $newPrimaryKey => null ] + $next,
- __METHOD__,
- [
- 'ORDER BY' => $primaryKey,
- 'LIMIT' => $this->getBatchSize(),
- ],
- [ $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ] ]
- );
- if ( !$res->numRows() ) {
- break;
- }
- // Collect the distinct comments from those rows
- $comments = [];
- foreach ( $res as $row ) {
- $comments[$row->$oldField] = 0;
- }
- $countComments += $this->loadCommentIDs( $dbw, $comments );
- // Update rows
- $inserts = [];
- $updates = [];
- foreach ( $res as $row ) {
- $inserts[] = [
- $newPrimaryKey => $row->$primaryKey,
- $newField => $comments[$row->$oldField]
- ];
- $updates[] = $row->$primaryKey;
- }
- $this->beginTransaction( $dbw, __METHOD__ );
- $dbw->insert( $newTable, $inserts, __METHOD__ );
- $dbw->update( $table, [ $oldField => '' ], [ $primaryKey => $updates ], __METHOD__ );
- $countUpdated += $dbw->affectedRows();
- $this->commitTransaction( $dbw, __METHOD__ );
- // Calculate the "next" condition
- $next = [ $primaryKey . ' > ' . $dbw->addQuotes( $row->$primaryKey ) ];
- $this->output( "... {$row->$primaryKey}\n" );
- }
- $this->output(
- "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
- );
- }
- }
- $maintClass = MigrateComments::class;
- require_once RUN_MAINTENANCE_IF_MAIN;
|