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