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 Wikimedia\Rdbms\IDatabase
;
26 require_once __DIR__
. '/Maintenance.php';
29 * Maintenance script that migrates comments from pre-1.30 columns to the
32 * @ingroup Maintenance
34 class MigrateComments
extends LoggedUpdateMaintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->addDescription( 'Migrates comments from pre-1.30 columns to the \'comment\' table' );
38 $this->setBatchSize( 100 );
41 protected function getUpdateKey() {
45 protected function updateSkippedMessage() {
46 return 'comments already migrated.';
49 protected function doDBUpdates() {
51 'revision', 'rev_id', 'rev_comment', 'revcomment_rev', 'revcomment_comment_id'
53 $this->migrate( 'archive', 'ar_id', 'ar_comment' );
54 $this->migrate( 'ipblocks', 'ipb_id', 'ipb_reason' );
55 $this->migrate( 'image', 'img_name', 'img_description' );
56 $this->migrate( 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_description' );
57 $this->migrate( 'filearchive', 'fa_id', 'fa_deleted_reason' );
58 $this->migrate( 'filearchive', 'fa_id', 'fa_description' );
59 $this->migrate( 'recentchanges', 'rc_id', 'rc_comment' );
60 $this->migrate( 'logging', 'log_id', 'log_comment' );
61 $this->migrate( 'protected_titles', [ 'pt_namespace', 'pt_title' ], 'pt_reason' );
66 * Fetch comment IDs for a set of comments
67 * @param IDatabase $dbw
68 * @param array &$comments Keys are comment names, values will be set to IDs.
69 * @return int Count of added comments
71 private function loadCommentIDs( IDatabase
$dbw, array &$comments ) {
73 $needComments = $comments;
77 foreach ( $needComments as $need => $dummy ) {
78 $where[] = $dbw->makeList(
80 'comment_hash' => CommentStore
::hash( $need, null ),
81 'comment_text' => $need,
89 [ 'comment_id', 'comment_text' ],
91 $dbw->makeList( $where, LIST_OR
),
92 'comment_data' => null,
96 foreach ( $res as $row ) {
97 $comments[$row->comment_text
] = $row->comment_id
;
98 unset( $needComments[$row->comment_text
] );
101 if ( !$needComments ) {
107 array_map( function ( $v ) {
109 'comment_hash' => CommentStore
::hash( $v, null ),
110 'comment_text' => $v,
112 }, array_keys( $needComments ) ),
115 $count +
= $dbw->affectedRows();
121 * Migrate comments in a table.
123 * Assumes any row with the ID field non-zero have already been migrated.
124 * Assumes the new field name is the same as the old with '_id' appended.
125 * Blanks the old fields while migrating.
127 * @param string $table Table to migrate
128 * @param string|string[] $primaryKey Primary key of the table.
129 * @param string $oldField Old comment field name
131 protected function migrate( $table, $primaryKey, $oldField ) {
132 $dbw = $this->getDB( DB_MASTER
);
133 if ( !$dbw->fieldExists( $table, $oldField, __METHOD__
) ) {
134 $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
138 $newField = $oldField . '_id';
139 $primaryKey = (array)$primaryKey;
140 $pkFilter = array_flip( $primaryKey );
141 $this->output( "Beginning migration of $table.$oldField to $table.$newField\n" );
148 // Fetch the rows needing update
151 array_merge( $primaryKey, [ $oldField ] ),
158 'ORDER BY' => $primaryKey,
159 'LIMIT' => $this->getBatchSize(),
162 if ( !$res->numRows() ) {
166 // Collect the distinct comments from those rows
168 foreach ( $res as $row ) {
169 $comments[$row->$oldField] = 0;
171 $countComments +
= $this->loadCommentIDs( $dbw, $comments );
173 // Update the existing rows
174 foreach ( $res as $row ) {
178 $newField => $comments[$row->$oldField],
181 array_intersect_key( (array)$row, $pkFilter ) +
[
186 $countUpdated +
= $dbw->affectedRows();
189 // Calculate the "next" condition
192 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
193 $field = $primaryKey[$i];
194 $prompt[] = $row->$field;
195 $value = $dbw->addQuotes( $row->$field );
196 if ( $next === '' ) {
197 $next = "$field > $value";
199 $next = "$field > $value OR $field = $value AND ($next)";
202 $prompt = implode( ' ', array_reverse( $prompt ) );
203 $this->output( "... $prompt\n" );
208 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
213 * Migrate comments in a table to a temporary table.
215 * Assumes any row with the ID field non-zero have already been migrated.
216 * Assumes the new table is named "{$table}_comment_temp", and it has two
217 * columns, in order, being the primary key of the original table and the
219 * Blanks the old fields while migrating.
221 * @param string $table Table to migrate
222 * @param string $primaryKey Primary key of the table.
223 * @param string $oldField Old comment field name
224 * @param string $newPrimaryKey Primary key of the new table.
225 * @param string $newField New comment field name
227 protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
228 $dbw = $this->getDB( DB_MASTER
);
229 if ( !$dbw->fieldExists( $table, $oldField, __METHOD__
) ) {
230 $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
234 $newTable = $table . '_comment_temp';
235 $this->output( "Beginning migration of $table.$oldField to $newTable.$newField\n" );
238 $dbw = $this->getDB( DB_MASTER
);
243 // Fetch the rows needing update
245 [ $table, $newTable ],
246 [ $primaryKey, $oldField ],
247 [ $newPrimaryKey => null ] +
$next,
250 'ORDER BY' => $primaryKey,
251 'LIMIT' => $this->getBatchSize(),
253 [ $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ] ]
255 if ( !$res->numRows() ) {
259 // Collect the distinct comments from those rows
261 foreach ( $res as $row ) {
262 $comments[$row->$oldField] = 0;
264 $countComments +
= $this->loadCommentIDs( $dbw, $comments );
269 foreach ( $res as $row ) {
271 $newPrimaryKey => $row->$primaryKey,
272 $newField => $comments[$row->$oldField]
274 $updates[] = $row->$primaryKey;
276 $this->beginTransaction( $dbw, __METHOD__
);
277 $dbw->insert( $newTable, $inserts, __METHOD__
);
278 $dbw->update( $table, [ $oldField => '' ], [ $primaryKey => $updates ], __METHOD__
);
279 $countUpdated +
= $dbw->affectedRows();
280 $this->commitTransaction( $dbw, __METHOD__
);
282 // Calculate the "next" condition
283 $next = [ $primaryKey . ' > ' . $dbw->addQuotes( $row->$primaryKey ) ];
284 $this->output( "... {$row->$primaryKey}\n" );
288 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
293 $maintClass = MigrateComments
::class;
294 require_once RUN_MAINTENANCE_IF_MAIN
;