3 * Migrate comments from pre-1.30 columns to the 'comment' table
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 MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IDatabase
;
27 require_once __DIR__
. '/Maintenance.php';
30 * Maintenance script that migrates comments from pre-1.30 columns to the
33 * @ingroup Maintenance
35 class MigrateComments
extends LoggedUpdateMaintenance
{
36 public function __construct() {
37 parent
::__construct();
38 $this->addDescription( 'Migrates comments from pre-1.30 columns to the \'comment\' table' );
39 $this->setBatchSize( 100 );
42 protected function getUpdateKey() {
46 protected function updateSkippedMessage() {
47 return 'comments already migrated.';
50 protected function doDBUpdates() {
52 'revision', 'rev_id', 'rev_comment', 'revcomment_rev', 'revcomment_comment_id'
54 $this->migrate( 'archive', 'ar_id', 'ar_comment' );
55 $this->migrate( 'ipblocks', 'ipb_id', 'ipb_reason' );
56 $this->migrate( 'image', 'img_name', 'img_description' );
57 $this->migrate( 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_description' );
58 $this->migrate( 'filearchive', 'fa_id', 'fa_deleted_reason' );
59 $this->migrate( 'filearchive', 'fa_id', 'fa_description' );
60 $this->migrate( 'recentchanges', 'rc_id', 'rc_comment' );
61 $this->migrate( 'logging', 'log_id', 'log_comment' );
62 $this->migrate( 'protected_titles', [ 'pt_namespace', 'pt_title' ], 'pt_reason' );
67 * Fetch comment IDs for a set of comments
68 * @param IDatabase $dbw
69 * @param array &$comments Keys are comment names, values will be set to IDs.
70 * @return int Count of added comments
72 private function loadCommentIDs( IDatabase
$dbw, array &$comments ) {
74 $needComments = $comments;
78 foreach ( $needComments as $need => $dummy ) {
79 $where[] = $dbw->makeList(
81 'comment_hash' => CommentStore
::hash( $need, null ),
82 'comment_text' => $need,
90 [ 'comment_id', 'comment_text' ],
92 $dbw->makeList( $where, LIST_OR
),
93 'comment_data' => null,
97 foreach ( $res as $row ) {
98 $comments[$row->comment_text
] = $row->comment_id
;
99 unset( $needComments[$row->comment_text
] );
102 if ( !$needComments ) {
108 array_map( function ( $v ) {
110 'comment_hash' => CommentStore
::hash( $v, null ),
111 'comment_text' => $v,
113 }, array_keys( $needComments ) ),
116 $count +
= $dbw->affectedRows();
122 * Migrate comments in a table.
124 * Assumes any row with the ID field non-zero have already been migrated.
125 * Assumes the new field name is the same as the old with '_id' appended.
126 * Blanks the old fields while migrating.
128 * @param string $table Table to migrate
129 * @param string|string[] $primaryKey Primary key of the table.
130 * @param string $oldField Old comment field name
132 protected function migrate( $table, $primaryKey, $oldField ) {
133 $dbw = $this->getDB( DB_MASTER
);
134 if ( !$dbw->fieldExists( $table, $oldField, __METHOD__
) ) {
135 $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
139 $newField = $oldField . '_id';
140 $primaryKey = (array)$primaryKey;
141 $pkFilter = array_flip( $primaryKey );
142 $this->output( "Beginning migration of $table.$oldField to $table.$newField\n" );
143 $lbFactory = MediaWikiServices
::getInstance()->getDBLoadBalancerFactory();
144 $lbFactory->waitForReplication();
150 // Fetch the rows needing update
153 array_merge( $primaryKey, [ $oldField ] ),
160 'ORDER BY' => $primaryKey,
161 'LIMIT' => $this->getBatchSize(),
164 if ( !$res->numRows() ) {
168 // Collect the distinct comments from those rows
170 foreach ( $res as $row ) {
171 $comments[$row->$oldField] = 0;
173 $countComments +
= $this->loadCommentIDs( $dbw, $comments );
175 // Update the existing rows
176 foreach ( $res as $row ) {
180 $newField => $comments[$row->$oldField],
183 array_intersect_key( (array)$row, $pkFilter ) +
[
188 $countUpdated +
= $dbw->affectedRows();
191 // Calculate the "next" condition
194 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
195 $field = $primaryKey[$i];
196 $prompt[] = $row->$field;
197 $value = $dbw->addQuotes( $row->$field );
198 if ( $next === '' ) {
199 $next = "$field > $value";
201 $next = "$field > $value OR $field = $value AND ($next)";
204 $prompt = implode( ' ', array_reverse( $prompt ) );
205 $this->output( "... $prompt\n" );
206 $lbFactory->waitForReplication();
210 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
215 * Migrate comments in a table to a temporary table.
217 * Assumes any row with the ID field non-zero have already been migrated.
218 * Assumes the new table is named "{$table}_comment_temp", and it has two
219 * columns, in order, being the primary key of the original table and the
221 * Blanks the old fields while migrating.
223 * @param string $table Table to migrate
224 * @param string $primaryKey Primary key of the table.
225 * @param string $oldField Old comment field name
226 * @param string $newPrimaryKey Primary key of the new table.
227 * @param string $newField New comment field name
229 protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
230 $dbw = $this->getDB( DB_MASTER
);
231 if ( !$dbw->fieldExists( $table, $oldField, __METHOD__
) ) {
232 $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
236 $newTable = $table . '_comment_temp';
237 $this->output( "Beginning migration of $table.$oldField to $newTable.$newField\n" );
238 MediaWikiServices
::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
240 $dbw = $this->getDB( DB_MASTER
);
245 // Fetch the rows needing update
247 [ $table, $newTable ],
248 [ $primaryKey, $oldField ],
249 // @phan-suppress-next-line PhanSuspiciousBinaryAddLists
250 [ $newPrimaryKey => null ] +
$next,
253 'ORDER BY' => $primaryKey,
254 'LIMIT' => $this->getBatchSize(),
256 [ $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ] ]
258 if ( !$res->numRows() ) {
262 // Collect the distinct comments from those rows
264 foreach ( $res as $row ) {
265 $comments[$row->$oldField] = 0;
267 $countComments +
= $this->loadCommentIDs( $dbw, $comments );
272 foreach ( $res as $row ) {
274 $newPrimaryKey => $row->$primaryKey,
275 $newField => $comments[$row->$oldField]
277 $updates[] = $row->$primaryKey;
279 $this->beginTransaction( $dbw, __METHOD__
);
280 $dbw->insert( $newTable, $inserts, __METHOD__
);
281 $dbw->update( $table, [ $oldField => '' ], [ $primaryKey => $updates ], __METHOD__
);
282 $countUpdated +
= $dbw->affectedRows();
283 $this->commitTransaction( $dbw, __METHOD__
);
285 // Calculate the "next" condition
286 $next = [ $primaryKey . ' > ' . $dbw->addQuotes( $row->$primaryKey ) ];
287 $this->output( "... {$row->$primaryKey}\n" );
291 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
296 $maintClass = MigrateComments
::class;
297 require_once RUN_MAINTENANCE_IF_MAIN
;