MediaWiki REL1_32
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->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
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 ],
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
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" );
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" );
209 }
210
211 $this->output(
212 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
213 );
214 }
215
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" );
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;
292require_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.
output( $out, $channel=null)
Throw some output to the user.
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
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:317
const LIST_OR
Definition Defines.php:46
const LIST_AND
Definition Defines.php:43
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
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_MASTER
Definition defines.php:26