MediaWiki REL1_31
migrateComments.php
Go to the documentation of this file.
1<?php
25
26require_once __DIR__ . '/Maintenance.php';
27
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() {
51
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
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 ],
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
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" );
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" );
211 }
212
213 $this->output(
214 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
215 );
216 }
217
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" );
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;
294require_once RUN_MAINTENANCE_IF_MAIN;
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
int $wgCommentTableSchemaMigrationStage
Comment table schema migration stage.
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Set the batch size.
Maintenance script that migrates comments from pre-1.30 columns to the 'comment' table.
__construct()
Default constructor.
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField)
Migrate comments in a table to a temporary table.
doDBUpdates()
Do the actual work.
migrate( $table, $primaryKey, $oldField)
Migrate comments in a table.
getUpdateKey()
Get the update key name to go in the update log table.
loadCommentIDs(IDatabase $dbw, array &$comments)
Fetch comment IDs for a set of comments.
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const MIGRATION_WRITE_NEW
Definition Defines.php:304
const LIST_OR
Definition Defines.php:56
const LIST_AND
Definition Defines.php:53
the array() calling protocol came about after MediaWiki 1.4rc1.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
affectedRows()
Get the number of rows affected by the last write query.
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:29