MediaWiki master
migrateRevisionCommentTemp.php
Go to the documentation of this file.
1<?php
21require_once __DIR__ . '/Maintenance.php';
22
31 public function __construct() {
32 parent::__construct();
33 $this->addDescription(
34 'Copy the data from the revision_comment_temp into the revision table'
35 );
36 $this->addOption(
37 'sleep',
38 'Sleep time (in seconds) between every batch. Default: 0',
39 false,
40 true
41 );
42 }
43
44 protected function getUpdateKey() {
45 return __CLASS__;
46 }
47
48 protected function doDBUpdates() {
49 $batchSize = $this->getBatchSize();
50
51 $dbw = $this->getDB( DB_PRIMARY );
52 if ( !$dbw->fieldExists( 'revision', 'rev_comment_id', __METHOD__ ) ) {
53 $this->output( "Run update.php to create rev_comment_id.\n" );
54 return false;
55 }
56 if ( !$dbw->tableExists( 'revision_comment_temp', __METHOD__ ) ) {
57 $this->output( "revision_comment_temp does not exist, so nothing to do.\n" );
58 return true;
59 }
60
61 $this->output( "Merging the revision_comment_temp table into the revision table...\n" );
62 $conds = [];
63 $updated = 0;
64 $sleep = (int)$this->getOption( 'sleep', 0 );
65 while ( true ) {
66 $res = $dbw->newSelectQueryBuilder()
67 ->select( [ 'rev_id', 'revcomment_comment_id' ] )
68 ->from( 'revision' )
69 ->join( 'revision_comment_temp', null, 'rev_id=revcomment_rev' )
70 ->where( [ 'rev_comment_id' => 0 ] )
71 ->andWhere( $conds )
72 ->limit( $batchSize )
73 ->orderBy( 'rev_id' )
74 ->caller( __METHOD__ )
75 ->fetchResultSet();
76
77 $numRows = $res->numRows();
78
79 $last = null;
80 foreach ( $res as $row ) {
81 $last = $row->rev_id;
82 $dbw->newUpdateQueryBuilder()
83 ->update( 'revision' )
84 ->set( [ 'rev_comment_id' => $row->revcomment_comment_id ] )
85 ->where( [ 'rev_id' => $row->rev_id ] )
86 ->caller( __METHOD__ )->execute();
87 $updated += $dbw->affectedRows();
88 }
89
90 if ( $numRows < $batchSize ) {
91 // We must have reached the end
92 break;
93 }
94
95 // @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
96 $this->output( "... rev_id=$last, updated $updated\n" );
97 $conds = [ $dbw->expr( 'rev_id', '>', $last ) ];
98
99 // Sleep between batches for replication to catch up
100 $this->waitForReplication();
101 if ( $sleep > 0 ) {
102 sleep( $sleep );
103 }
104 }
105 $this->output(
106 "Completed merge of revision_comment_temp into the revision table, "
107 . "$updated rows updated.\n"
108 );
109 return true;
110 }
111}
112
113$maintClass = MigrateRevisionCommentTemp::class;
114require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
Class for scripts that perform database maintenance and want to log the update in updatelog so we can...
output( $out, $channel=null)
Throw some output to the user.
waitForReplication()
Wait for replica DBs to catch up.
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Maintenance script that merges the revision_comment_temp table into the revision table.
getUpdateKey()
Get the update key name to go in the update log table.
const DB_PRIMARY
Definition defines.php:28