MediaWiki  1.30.2
migrateComments.php
Go to the documentation of this file.
1 <?php
25 
26 require_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  ],
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 
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->mBatchSize,
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 = join( ' ', 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 
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->mBatchSize,
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  wfWaitForSlaves();
286  }
287 
288  $this->output(
289  "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
290  );
291  }
292 }
293 
294 $maintClass = "MigrateComments";
295 require_once RUN_MAINTENANCE_IF_MAIN;
Wikimedia\Rdbms\IDatabase\affectedRows
affectedRows()
Get the number of rows affected by the last write query.
Wikimedia\Rdbms\IDatabase\makeList
makeList( $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
captcha-old.count
count
Definition: captcha-old.py:249
MigrateComments\doDBUpdates
doDBUpdates()
Do the actual work.
Definition: migrateComments.php:49
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
$wgCommentTableSchemaMigrationStage
int $wgCommentTableSchemaMigrationStage
Comment table schema migration stage.
Definition: DefaultSettings.php:8770
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$res
$res
Definition: database.txt:21
wfWaitForSlaves
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
Definition: GlobalFunctions.php:3010
Wikimedia\Rdbms\IDatabase\insert
insert( $table, $a, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
php
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:35
MigrateComments\migrate
migrate( $table, $primaryKey, $oldField)
Migrate comments in a table.
Definition: migrateComments.php:142
LIST_AND
const LIST_AND
Definition: Defines.php:44
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
MIGRATION_WRITE_NEW
const MIGRATION_WRITE_NEW
Definition: Defines.php:295
MigrateComments
Maintenance script that migrates comments from pre-1.30 columns to the 'comment' table.
Definition: migrateComments.php:34
Maintenance\beginTransaction
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.
Definition: Maintenance.php:1278
LIST_OR
const LIST_OR
Definition: Defines.php:47
$maintClass
$maintClass
Definition: migrateComments.php:294
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1562
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$value
$value
Definition: styleTest.css.php:45
MigrateComments\updateSkippedMessage
updateSkippedMessage()
Message to show that the update was done already and was just skipped.
Definition: migrateComments.php:45
MigrateComments\getUpdateKey
getUpdateKey()
Get the update key name to go in the update log table.
Definition: migrateComments.php:41
Maintenance\commitTransaction
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
Definition: Maintenance.php:1293
CommentStore\hash
static hash( $text, $data)
Hashing function for comment storage.
Definition: CommentStore.php:577
as
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
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
Wikimedia\Rdbms\IDatabase\select
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:373
MigrateComments\migrateToTemp
migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField)
Migrate comments in a table to a temporary table.
Definition: migrateComments.php:233
MigrateComments\__construct
__construct()
Default constructor.
Definition: migrateComments.php:35
MigrateComments\loadCommentIDs
loadCommentIDs(IDatabase $dbw, array &$comments)
Fetch comment IDs for a set of comments.
Definition: migrateComments.php:82
array
the array() calling protocol came about after MediaWiki 1.4rc1.
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314