27require_once __DIR__ .
'/Maintenance.php';
37 parent::__construct();
38 $this->
addDescription(
'Migrates comments from pre-1.30 columns to the \'comment\' table' );
47 return 'comments already migrated.';
52 'revision',
'rev_id',
'rev_comment',
'revcomment_rev',
'revcomment_comment_id'
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' );
72 private function loadCommentIDs(
IDatabase $dbw, array &$comments ) {
74 $needComments = $comments;
78 foreach ( $needComments as $need => $dummy ) {
79 $need = (string)$need;
82 'comment_hash' => CommentStore::hash( $need,
null ),
83 'comment_text' => $need,
91 [
'comment_id',
'comment_text' ],
94 'comment_data' =>
null,
98 foreach (
$res as $row ) {
99 $comments[$row->comment_text] = $row->comment_id;
100 unset( $needComments[$row->comment_text] );
103 if ( !$needComments ) {
109 array_map(
static function ( $v ) {
112 'comment_text' => $v,
114 }, array_keys( $needComments ) ),
133 protected function migrate( $table, $primaryKey, $oldField ) {
135 if ( !$dbw->fieldExists( $table, $oldField, __METHOD__ ) ) {
136 $this->
output(
"No need to migrate $table.$oldField, field does not exist\n" );
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();
154 array_merge( $primaryKey, [ $oldField ] ),
161 'ORDER BY' => $primaryKey,
165 if ( !
$res->numRows() ) {
171 foreach (
$res as $row ) {
172 $comments[$row->$oldField] = 0;
174 $countComments += $this->loadCommentIDs( $dbw, $comments );
177 foreach (
$res as $row ) {
181 $newField => $comments[$row->$oldField],
184 array_intersect_key( (array)$row, $pkFilter ) + [
195 for ( $i = count( $primaryKey ) - 1; $i >= 0; $i-- ) {
196 $field = $primaryKey[$i];
198 $prompt[] = $row->$field;
200 $value = $dbw->
addQuotes( $row->$field );
201 if ( $next ===
'' ) {
202 $next =
"$field > $value";
204 $next =
"$field > $value OR $field = $value AND ($next)";
207 $prompt = implode(
' ', array_reverse( $prompt ) );
208 $this->
output(
"... $prompt\n" );
209 $lbFactory->waitForReplication();
213 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
232 protected function migrateToTemp( $table, $primaryKey, $oldField, $newPrimaryKey, $newField ) {
234 if ( !$dbw->fieldExists( $table, $oldField, __METHOD__ ) ) {
235 $this->
output(
"No need to migrate $table.$oldField, field does not exist\n" );
239 $newTable = $table .
'_comment_temp';
240 $this->
output(
"Beginning migration of $table.$oldField to $newTable.$newField\n" );
241 MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
250 [ $table, $newTable ],
251 [ $primaryKey, $oldField ],
252 [ $newPrimaryKey =>
null ] + $next,
255 'ORDER BY' => $primaryKey,
258 [ $newTable => [
'LEFT JOIN',
"{$primaryKey}={$newPrimaryKey}" ] ]
260 if ( !
$res->numRows() ) {
266 foreach (
$res as $row ) {
267 $comments[$row->$oldField] = 0;
269 $countComments += $this->loadCommentIDs( $dbw, $comments );
274 foreach (
$res as $row ) {
276 $newPrimaryKey => $row->$primaryKey,
277 $newField => $comments[$row->$oldField]
279 $updates[] = $row->$primaryKey;
282 $dbw->
insert( $newTable, $inserts, __METHOD__ );
283 $dbw->
update( $table, [ $oldField =>
'' ], [ $primaryKey => $updates ], __METHOD__ );
289 $next = [ $primaryKey .
' > ' . $dbw->
addQuotes( $row->$primaryKey ) ];
291 $this->
output(
"... {$row->$primaryKey}\n" );
295 "Completed migration, updated $countUpdated row(s) with $countComments new comment(s)\n"
301require_once RUN_MAINTENANCE_IF_MAIN;
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
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.