MediaWiki  master
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() {
50  $this->migrateToTemp(
51  'revision', 'rev_id', 'rev_comment', 'revcomment_rev', 'revcomment_comment_id'
52  );
53  $this->migrate( 'archive', 'ar_id', 'ar_comment' );
54  $this->migrate( 'ipblocks', 'ipb_id', 'ipb_reason' );
55  $this->migrate( 'image', 'img_name', 'img_description' );
56  $this->migrate( 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_description' );
57  $this->migrate( 'filearchive', 'fa_id', 'fa_deleted_reason' );
58  $this->migrate( 'filearchive', 'fa_id', 'fa_description' );
59  $this->migrate( 'recentchanges', 'rc_id', 'rc_comment' );
60  $this->migrate( 'logging', 'log_id', 'log_comment' );
61  $this->migrate( 'protected_titles', [ 'pt_namespace', 'pt_title' ], 'pt_reason' );
62  return true;
63  }
64 
71  private function loadCommentIDs( IDatabase $dbw, array &$comments ) {
72  $count = 0;
73  $needComments = $comments;
74 
75  while ( true ) {
76  $where = [];
77  foreach ( $needComments as $need => $dummy ) {
78  $where[] = $dbw->makeList(
79  [
80  'comment_hash' => CommentStore::hash( $need, null ),
81  'comment_text' => $need,
82  ],
83  LIST_AND
84  );
85  }
86 
87  $res = $dbw->select(
88  'comment',
89  [ 'comment_id', 'comment_text' ],
90  [
91  $dbw->makeList( $where, LIST_OR ),
92  'comment_data' => null,
93  ],
94  __METHOD__
95  );
96  foreach ( $res as $row ) {
97  $comments[$row->comment_text] = $row->comment_id;
98  unset( $needComments[$row->comment_text] );
99  }
100 
101  if ( !$needComments ) {
102  break;
103  }
104 
105  $dbw->insert(
106  'comment',
107  array_map( function ( $v ) {
108  return [
109  'comment_hash' => CommentStore::hash( $v, null ),
110  'comment_text' => $v,
111  ];
112  }, array_keys( $needComments ) ),
113  __METHOD__
114  );
115  $count += $dbw->affectedRows();
116  }
117  return $count;
118  }
119 
131  protected function migrate( $table, $primaryKey, $oldField ) {
132  $dbw = $this->getDB( DB_MASTER );
133  if ( !$dbw->fieldExists( $table, $oldField, __METHOD__ ) ) {
134  $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
135  return;
136  }
137 
138  $newField = $oldField . '_id';
139  $primaryKey = (array)$primaryKey;
140  $pkFilter = array_flip( $primaryKey );
141  $this->output( "Beginning migration of $table.$oldField to $table.$newField\n" );
142  wfWaitForSlaves();
143 
144  $next = '1=1';
145  $countUpdated = 0;
146  $countComments = 0;
147  while ( true ) {
148  // Fetch the rows needing update
149  $res = $dbw->select(
150  $table,
151  array_merge( $primaryKey, [ $oldField ] ),
152  [
153  $newField => 0,
154  $next,
155  ],
156  __METHOD__,
157  [
158  'ORDER BY' => $primaryKey,
159  'LIMIT' => $this->getBatchSize(),
160  ]
161  );
162  if ( !$res->numRows() ) {
163  break;
164  }
165 
166  // Collect the distinct comments from those rows
167  $comments = [];
168  foreach ( $res as $row ) {
169  $comments[$row->$oldField] = 0;
170  }
171  $countComments += $this->loadCommentIDs( $dbw, $comments );
172 
173  // Update the existing rows
174  foreach ( $res as $row ) {
175  $dbw->update(
176  $table,
177  [
178  $newField => $comments[$row->$oldField],
179  $oldField => '',
180  ],
181  array_intersect_key( (array)$row, $pkFilter ) + [
182  $newField => 0
183  ],
184  __METHOD__
185  );
186  $countUpdated += $dbw->affectedRows();
187  }
188 
189  // Calculate the "next" condition
190  $next = '';
191  $prompt = [];
192  for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
193  $field = $primaryKey[$i];
194  $prompt[] = $row->$field;
195  $value = $dbw->addQuotes( $row->$field );
196  if ( $next === '' ) {
197  $next = "$field > $value";
198  } else {
199  $next = "$field > $value OR $field = $value AND ($next)";
200  }
201  }
202  $prompt = implode( ' ', array_reverse( $prompt ) );
203  $this->output( "... $prompt\n" );
204  wfWaitForSlaves();
205  }
206 
207  $this->output(
208  "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
209  );
210  }
211 
227  protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
228  $dbw = $this->getDB( DB_MASTER );
229  if ( !$dbw->fieldExists( $table, $oldField, __METHOD__ ) ) {
230  $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
231  return;
232  }
233 
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;
commitTransaction(IDatabase $dbw, $fname)
Commit the transcation on a DB handle and wait for replica DBs to catch up.
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
affectedRows()
Get the number of rows affected by the last write query.
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
migrate( $table, $primaryKey, $oldField)
Migrate comments in a table.
static hash( $text, $data)
Hashing function for comment storage.
setBatchSize( $s=0)
Set the batch size.
$maintClass
const DB_MASTER
Definition: defines.php:26
const LIST_AND
Definition: Defines.php:39
wfWaitForSlaves( $ifWritesSince=null, $wiki=false, $cluster=false, $timeout=null)
Waits for the replica DBs to catch up to the master position.
addDescription( $text)
Set the description text.
output( $out, $channel=null)
Throw some output to the user.
const LIST_OR
Definition: Defines.php:42
migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField)
Migrate comments in a table to a temporary table.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:38
insert( $table, $rows, $fname=__METHOD__, $options=[])
INSERT wrapper, inserts an array into a table.
getBatchSize()
Returns batch size.
select( $table, $vars, $conds='', $fname=__METHOD__, $options=[], $join_conds=[])
Execute a SELECT query constructed using the various parameters provided.
loadCommentIDs(IDatabase $dbw, array &$comments)
Fetch comment IDs for a set of comments.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Maintenance script that migrates comments from pre-1.30 columns to the &#39;comment&#39; table.
beginTransaction(IDatabase $dbw, $fname)
Begin a transcation on a DB.