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