MediaWiki  master
migrateComments.php
Go to the documentation of this file.
1 <?php
26 
27 require_once __DIR__ . '/Maintenance.php';
28 
36  public function __construct() {
37  parent::__construct();
38  $this->addDescription( 'Migrates comments from pre-1.30 columns to the \'comment\' table' );
39  $this->setBatchSize( 100 );
40  }
41 
42  protected function getUpdateKey() {
43  return __CLASS__;
44  }
45 
46  protected function updateSkippedMessage() {
47  return 'comments already migrated.';
48  }
49 
50  protected function doDBUpdates() {
51  $this->migrateToTemp(
52  'revision', 'rev_id', 'rev_comment', 'revcomment_rev', 'revcomment_comment_id'
53  );
54  $this->migrate( 'archive', 'ar_id', 'ar_comment' );
55  $this->migrate( 'ipblocks', 'ipb_id', 'ipb_reason' );
56  $this->migrate( 'image', 'img_name', 'img_description' );
57  $this->migrate( 'oldimage', [ 'oi_name', 'oi_timestamp' ], 'oi_description' );
58  $this->migrate( 'filearchive', 'fa_id', 'fa_deleted_reason' );
59  $this->migrate( 'filearchive', 'fa_id', 'fa_description' );
60  $this->migrate( 'recentchanges', 'rc_id', 'rc_comment' );
61  $this->migrate( 'logging', 'log_id', 'log_comment' );
62  $this->migrate( 'protected_titles', [ 'pt_namespace', 'pt_title' ], 'pt_reason' );
63  return true;
64  }
65 
72  private function loadCommentIDs( IDatabase $dbw, array &$comments ) {
73  $count = 0;
74  $needComments = $comments;
75 
76  while ( true ) {
77  $where = [];
78  foreach ( $needComments as $need => $dummy ) {
79  $need = (string)$need; // T268887
80  $where[] = $dbw->makeList(
81  [
82  'comment_hash' => CommentStore::hash( $need, null ),
83  'comment_text' => $need,
84  ],
85  LIST_AND
86  );
87  }
88 
89  $res = $dbw->select(
90  'comment',
91  [ 'comment_id', 'comment_text' ],
92  [
93  $dbw->makeList( $where, LIST_OR ),
94  'comment_data' => null,
95  ],
96  __METHOD__
97  );
98  foreach ( $res as $row ) {
99  $comments[$row->comment_text] = $row->comment_id;
100  unset( $needComments[$row->comment_text] );
101  }
102 
103  if ( !$needComments ) {
104  break;
105  }
106 
107  $dbw->insert(
108  'comment',
109  array_map( static function ( $v ) {
110  return [
111  'comment_hash' => CommentStore::hash( $v, null ),
112  'comment_text' => $v,
113  ];
114  }, array_keys( $needComments ) ),
115  __METHOD__
116  );
117  $count += $dbw->affectedRows();
118  }
119  return $count;
120  }
121 
133  protected function migrate( $table, $primaryKey, $oldField ) {
134  $dbw = $this->getDB( DB_PRIMARY );
135  if ( !$dbw->fieldExists( $table, $oldField, __METHOD__ ) ) {
136  $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
137  return;
138  }
139 
140  $newField = $oldField . '_id';
141  $primaryKey = (array)$primaryKey;
142  $pkFilter = array_fill_keys( $primaryKey, true );
143  $this->output( "Beginning migration of $table.$oldField to $table.$newField\n" );
144  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
145  $lbFactory->waitForReplication();
146 
147  $next = '1=1';
148  $countUpdated = 0;
149  $countComments = 0;
150  while ( true ) {
151  // Fetch the rows needing update
152  $res = $dbw->select(
153  $table,
154  array_merge( $primaryKey, [ $oldField ] ),
155  [
156  $newField => 0,
157  $next,
158  ],
159  __METHOD__,
160  [
161  'ORDER BY' => $primaryKey,
162  'LIMIT' => $this->getBatchSize(),
163  ]
164  );
165  if ( !$res->numRows() ) {
166  break;
167  }
168 
169  // Collect the distinct comments from those rows
170  $comments = [];
171  foreach ( $res as $row ) {
172  $comments[$row->$oldField] = 0;
173  }
174  $countComments += $this->loadCommentIDs( $dbw, $comments );
175 
176  // Update the existing rows
177  foreach ( $res as $row ) {
178  $dbw->update(
179  $table,
180  [
181  $newField => $comments[$row->$oldField],
182  $oldField => '',
183  ],
184  array_intersect_key( (array)$row, $pkFilter ) + [
185  $newField => 0
186  ],
187  __METHOD__
188  );
189  $countUpdated += $dbw->affectedRows();
190  }
191 
192  // Calculate the "next" condition
193  $next = '';
194  $prompt = [];
195  for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
196  $field = $primaryKey[$i];
197  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
198  $prompt[] = $row->$field;
199  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
200  $value = $dbw->addQuotes( $row->$field );
201  if ( $next === '' ) {
202  $next = "$field > $value";
203  } else {
204  $next = "$field > $value OR $field = $value AND ($next)";
205  }
206  }
207  $prompt = implode( ' ', array_reverse( $prompt ) );
208  $this->output( "... $prompt\n" );
209  $lbFactory->waitForReplication();
210  }
211 
212  $this->output(
213  "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
214  );
215  }
216 
232  protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
233  $dbw = $this->getDB( DB_PRIMARY );
234  if ( !$dbw->fieldExists( $table, $oldField, __METHOD__ ) ) {
235  $this->output( "No need to migrate $table.$oldField, field does not exist\n" );
236  return;
237  }
238 
239  $newTable = $table . '_comment_temp';
240  $this->output( "Beginning migration of $table.$oldField to $newTable.$newField\n" );
241  MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
242 
243  $dbw = $this->getDB( DB_PRIMARY );
244  $next = [];
245  $countUpdated = 0;
246  $countComments = 0;
247  while ( true ) {
248  // Fetch the rows needing update
249  $res = $dbw->select(
250  [ $table, $newTable ],
251  [ $primaryKey, $oldField ],
252  [ $newPrimaryKey => null ] + $next,
253  __METHOD__,
254  [
255  'ORDER BY' => $primaryKey,
256  'LIMIT' => $this->getBatchSize(),
257  ],
258  [ $newTable => [ 'LEFT JOIN', "{$primaryKey}={$newPrimaryKey}" ] ]
259  );
260  if ( !$res->numRows() ) {
261  break;
262  }
263 
264  // Collect the distinct comments from those rows
265  $comments = [];
266  foreach ( $res as $row ) {
267  $comments[$row->$oldField] = 0;
268  }
269  $countComments += $this->loadCommentIDs( $dbw, $comments );
270 
271  // Update rows
272  $inserts = [];
273  $updates = [];
274  foreach ( $res as $row ) {
275  $inserts[] = [
276  $newPrimaryKey => $row->$primaryKey,
277  $newField => $comments[$row->$oldField]
278  ];
279  $updates[] = $row->$primaryKey;
280  }
281  $this->beginTransaction( $dbw, __METHOD__ );
282  $dbw->insert( $newTable, $inserts, __METHOD__ );
283  $dbw->update( $table, [ $oldField => '' ], [ $primaryKey => $updates ], __METHOD__ );
284  $countUpdated += $dbw->affectedRows();
285  $this->commitTransaction( $dbw, __METHOD__ );
286 
287  // Calculate the "next" condition
288  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
289  $next = [ $primaryKey . ' > ' . $dbw->addQuotes( $row->$primaryKey ) ];
290  // @phan-suppress-next-line PhanPossiblyUndeclaredVariable rows contains at least one item
291  $this->output( "... {$row->$primaryKey}\n" );
292  }
293 
294  $this->output(
295  "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
296  );
297  }
298 }
299 
300 $maintClass = MigrateComments::class;
301 require_once RUN_MAINTENANCE_IF_MAIN;
const LIST_OR
Definition: Defines.php:46
const LIST_AND
Definition: Defines.php:43
static hash( $text, $data)
Hashing function for comment storage.
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
beginTransaction(IDatabase $dbw, $fname)
Begin a transaction on a DB.
commitTransaction(IDatabase $dbw, $fname)
Commit the transaction on a DB handle and wait for replica DBs to catch up.
output( $out, $channel=null)
Throw some output to the user.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
setBatchSize( $s=0)
Service locator for MediaWiki core services.
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.
addQuotes( $s)
Escape and quote a raw value string for use in a SQL query.
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:39
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.
update( $table, $set, $conds, $fname=__METHOD__, $options=[])
Update all rows in a table that match a given condition.
insert( $table, $rows, $fname=__METHOD__, $options=[])
Insert row(s) into a table, in the provided order.
makeList(array $a, $mode=self::LIST_COMMA)
Makes an encoded list of strings from an array.
$maintClass
const DB_PRIMARY
Definition: defines.php:28