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