summaryrefslogtreecommitdiff
path: root/www/wiki/maintenance/migrateComments.php
blob: cdecab03c3fdcb9bd2d6be74d61ee8f02aa7df80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?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;