MediaWiki  1.32.0
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->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  ],
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 
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 
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 
292 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:317
$wgCommentTableSchemaMigrationStage
int $wgCommentTableSchemaMigrationStage
Comment table schema migration stage.
Definition: DefaultSettings.php:8967
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:2847
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:140
LIST_AND
const LIST_AND
Definition: Defines.php:43
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
MIGRATION_WRITE_NEW
const MIGRATION_WRITE_NEW
Definition: Defines.php:317
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:1378
LIST_OR
const LIST_OR
Definition: Defines.php:46
$maintClass
$maintClass
Definition: migrateComments.php:291
LoggedUpdateMaintenance
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Definition: Maintenance.php:1679
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
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
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))
$value
$value
Definition: styleTest.css.php:49
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:1393
CommentStore\hash
static hash( $text, $data)
Hashing function for comment storage.
Definition: CommentStore.php:692
Maintenance\getBatchSize
getBatchSize()
Returns batch size.
Definition: Maintenance.php:347
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:1351
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:414
MigrateComments\migrateToTemp
migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField)
Migrate comments in a table to a temporary table.
Definition: migrateComments.php:231
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
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:80
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:355