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