MediaWiki  master
migrateRevisionCommentTemp.php
Go to the documentation of this file.
1 <?php
21 require_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->update(
83  'revision',
84  [ 'rev_comment_id' => $row->revcomment_comment_id ],
85  [ 'rev_id' => $row->rev_id ],
86  __METHOD__
87  );
88  $updated += $dbw->affectedRows();
89  }
90 
91  if ( $numRows < $batchSize ) {
92  // We must have reached the end
93  break;
94  }
95 
96  // @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
97  $this->output( "... rev_id=$last, updated $updated\n" );
98  $conds = [ $dbw->buildComparison( '>', [ 'rev_id' => $last ] ) ];
99 
100  // Sleep between batches for replication to catch up
101  $this->waitForReplication();
102  if ( $sleep > 0 ) {
103  sleep( $sleep );
104  }
105  }
106  $this->output(
107  "Completed merge of revision_comment_temp into the revision table, "
108  . "$updated rows updated.\n"
109  );
110  return true;
111  }
112 }
113 
114 $maintClass = MigrateRevisionCommentTemp::class;
115 require_once RUN_MAINTENANCE_IF_MAIN;
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.
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