Merge "Disallow overriding services that were set"
[lhc/web/wiklou.git] / maintenance / migrateComments.php
1 <?php
2 /**
3 * Migrate comments from pre-1.30 columns to the 'comment' table
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23
24 use Wikimedia\Rdbms\IDatabase;
25
26 require_once __DIR__ . '/Maintenance.php';
27
28 /**
29 * Maintenance script that migrates comments from pre-1.30 columns to the
30 * 'comment' table
31 *
32 * @ingroup Maintenance
33 */
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 );
39 }
40
41 protected function getUpdateKey() {
42 return __CLASS__;
43 }
44
45 protected function updateSkippedMessage() {
46 return 'comments already migrated.';
47 }
48
49 protected function doDBUpdates() {
50 global $wgCommentTableSchemaMigrationStage;
51
52 if ( $wgCommentTableSchemaMigrationStage < MIGRATION_WRITE_NEW ) {
53 $this->output(
54 "...cannot update while \$wgCommentTableSchemaMigrationStage < MIGRATION_WRITE_NEW\n"
55 );
56 return false;
57 }
58
59 $this->migrateToTemp(
60 'revision', 'rev_id', 'rev_comment', 'revcomment_rev', 'revcomment_comment_id'
61 );
62 $this->migrate( 'archive', 'ar_id', 'ar_comment' );
63 $this->migrate( 'ipblocks', 'ipb_id', 'ipb_reason' );
64 $this->migrate( 'image', 'img_name', 'img_description' );
65 $this->migrate( 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_description' );
66 $this->migrate( 'filearchive', 'fa_id', 'fa_deleted_reason' );
67 $this->migrate( 'filearchive', 'fa_id', 'fa_description' );
68 $this->migrate( 'recentchanges', 'rc_id', 'rc_comment' );
69 $this->migrate( 'logging', 'log_id', 'log_comment' );
70 $this->migrate( 'protected_titles', [ 'pt_namespace', 'pt_title' ], 'pt_reason' );
71 return true;
72 }
73
74 /**
75 * Fetch comment IDs for a set of comments
76 * @param IDatabase $dbw
77 * @param array &$comments Keys are comment names, values will be set to IDs.
78 * @return int Count of added comments
79 */
80 private function loadCommentIDs( IDatabase $dbw, array &$comments ) {
81 $count = 0;
82 $needComments = $comments;
83
84 while ( true ) {
85 $where = [];
86 foreach ( $needComments as $need => $dummy ) {
87 $where[] = $dbw->makeList(
88 [
89 'comment_hash' => CommentStore::hash( $need, null ),
90 'comment_text' => $need,
91 ],
92 LIST_AND
93 );
94 }
95
96 $res = $dbw->select(
97 'comment',
98 [ 'comment_id', 'comment_text' ],
99 [
100 $dbw->makeList( $where, LIST_OR ),
101 'comment_data' => null,
102 ],
103 __METHOD__
104 );
105 foreach ( $res as $row ) {
106 $comments[$row->comment_text] = $row->comment_id;
107 unset( $needComments[$row->comment_text] );
108 }
109
110 if ( !$needComments ) {
111 break;
112 }
113
114 $dbw->insert(
115 'comment',
116 array_map( function ( $v ) {
117 return [
118 'comment_hash' => CommentStore::hash( $v, null ),
119 'comment_text' => $v,
120 ];
121 }, array_keys( $needComments ) ),
122 __METHOD__
123 );
124 $count += $dbw->affectedRows();
125 }
126 return $count;
127 }
128
129 /**
130 * Migrate comments in a table.
131 *
132 * Assumes any row with the ID field non-zero have already been migrated.
133 * Assumes the new field name is the same as the old with '_id' appended.
134 * Blanks the old fields while migrating.
135 *
136 * @param string $table Table to migrate
137 * @param string|string[] $primaryKey Primary key of the table.
138 * @param string $oldField Old comment field name
139 */
140 protected function migrate( $table, $primaryKey, $oldField ) {
141 $newField = $oldField . '_id';
142 $primaryKey = (array)$primaryKey;
143 $pkFilter = array_flip( $primaryKey );
144 $this->output( "Beginning migration of $table.$oldField to $table.$newField\n" );
145 wfWaitForSlaves();
146
147 $dbw = $this->getDB( DB_MASTER );
148 $next = '1=1';
149 $countUpdated = 0;
150 $countComments = 0;
151 while ( true ) {
152 // Fetch the rows needing update
153 $res = $dbw->select(
154 $table,
155 array_merge( $primaryKey, [ $oldField ] ),
156 [
157 $newField => 0,
158 $next,
159 ],
160 __METHOD__,
161 [
162 'ORDER BY' => $primaryKey,
163 'LIMIT' => $this->getBatchSize(),
164 ]
165 );
166 if ( !$res->numRows() ) {
167 break;
168 }
169
170 // Collect the distinct comments from those rows
171 $comments = [];
172 foreach ( $res as $row ) {
173 $comments[$row->$oldField] = 0;
174 }
175 $countComments += $this->loadCommentIDs( $dbw, $comments );
176
177 // Update the existing rows
178 foreach ( $res as $row ) {
179 $dbw->update(
180 $table,
181 [
182 $newField => $comments[$row->$oldField],
183 $oldField => '',
184 ],
185 array_intersect_key( (array)$row, $pkFilter ) + [
186 $newField => 0
187 ],
188 __METHOD__
189 );
190 $countUpdated += $dbw->affectedRows();
191 }
192
193 // Calculate the "next" condition
194 $next = '';
195 $prompt = [];
196 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
197 $field = $primaryKey[$i];
198 $prompt[] = $row->$field;
199 $value = $dbw->addQuotes( $row->$field );
200 if ( $next === '' ) {
201 $next = "$field > $value";
202 } else {
203 $next = "$field > $value OR $field = $value AND ($next)";
204 }
205 }
206 $prompt = implode( ' ', array_reverse( $prompt ) );
207 $this->output( "... $prompt\n" );
208 wfWaitForSlaves();
209 }
210
211 $this->output(
212 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
213 );
214 }
215
216 /**
217 * Migrate comments in a table to a temporary table.
218 *
219 * Assumes any row with the ID field non-zero have already been migrated.
220 * Assumes the new table is named "{$table}_comment_temp", and it has two
221 * columns, in order, being the primary key of the original table and the
222 * comment ID field.
223 * Blanks the old fields while migrating.
224 *
225 * @param string $table Table to migrate
226 * @param string $primaryKey Primary key of the table.
227 * @param string $oldField Old comment field name
228 * @param string $newPrimaryKey Primary key of the new table.
229 * @param string $newField New comment field name
230 */
231 protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
232 $newTable = $table . '_comment_temp';
233 $this->output( "Beginning migration of $table.$oldField to $newTable.$newField\n" );
234 wfWaitForSlaves();
235
236 $dbw = $this->getDB( DB_MASTER );
237 $next = [];
238 $countUpdated = 0;
239 $countComments = 0;
240 while ( true ) {
241 // Fetch the rows needing update
242 $res = $dbw->select(
243 [ $table, $newTable ],
244 [ $primaryKey, $oldField ],
245 [ $newPrimaryKey => null ] + $next,
246 __METHOD__,
247 [
248 'ORDER BY' => $primaryKey,
249 'LIMIT' => $this->getBatchSize(),
250 ],
251 [ $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ] ]
252 );
253 if ( !$res->numRows() ) {
254 break;
255 }
256
257 // Collect the distinct comments from those rows
258 $comments = [];
259 foreach ( $res as $row ) {
260 $comments[$row->$oldField] = 0;
261 }
262 $countComments += $this->loadCommentIDs( $dbw, $comments );
263
264 // Update rows
265 $inserts = [];
266 $updates = [];
267 foreach ( $res as $row ) {
268 $inserts[] = [
269 $newPrimaryKey => $row->$primaryKey,
270 $newField => $comments[$row->$oldField]
271 ];
272 $updates[] = $row->$primaryKey;
273 }
274 $this->beginTransaction( $dbw, __METHOD__ );
275 $dbw->insert( $newTable, $inserts, __METHOD__ );
276 $dbw->update( $table, [ $oldField => '' ], [ $primaryKey => $updates ], __METHOD__ );
277 $countUpdated += $dbw->affectedRows();
278 $this->commitTransaction( $dbw, __METHOD__ );
279
280 // Calculate the "next" condition
281 $next = [ $primaryKey . ' > ' . $dbw->addQuotes( $row->$primaryKey ) ];
282 $this->output( "... {$row->$primaryKey}\n" );
283 }
284
285 $this->output(
286 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
287 );
288 }
289 }
290
291 $maintClass = MigrateComments::class;
292 require_once RUN_MAINTENANCE_IF_MAIN;