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
49 protected function getUpdateKey() {
50 return __CLASS__;
51 }
52
54 protected function doDBUpdates() {
55 $batchSize = $this->getBatchSize();
56
57 $dbw = $this->getDB( DB_PRIMARY );
58 if ( !$dbw->fieldExists( 'revision', 'rev_comment_id', __METHOD__ ) ) {
59 $this->output( "Run update.php to create rev_comment_id.\n" );
60 return false;
61 }
62 if ( !$dbw->tableExists( 'revision_comment_temp', __METHOD__ ) ) {
63 $this->output( "revision_comment_temp does not exist, so nothing to do.\n" );
64 return true;
65 }
66
67 $this->output( "Merging the revision_comment_temp table into the revision table...\n" );
68 $conds = [];
69 $updated = 0;
70 $sleep = (int)$this->getOption( 'sleep', 0 );
71 while ( true ) {
72 $res = $dbw->newSelectQueryBuilder()
73 ->select( [ 'rev_id', 'revcomment_comment_id' ] )
74 ->from( 'revision' )
75 ->join( 'revision_comment_temp', null, 'rev_id=revcomment_rev' )
76 ->where( [ 'rev_comment_id' => 0 ] )
77 ->andWhere( $conds )
78 ->limit( $batchSize )
79 ->orderBy( 'rev_id' )
80 ->caller( __METHOD__ )
81 ->fetchResultSet();
82
83 $numRows = $res->numRows();
84
85 $last = null;
86 foreach ( $res as $row ) {
87 $last = $row->rev_id;
88 $dbw->newUpdateQueryBuilder()
89 ->update( 'revision' )
90 ->set( [ 'rev_comment_id' => $row->revcomment_comment_id ] )
91 ->where( [ 'rev_id' => $row->rev_id ] )
92 ->caller( __METHOD__ )->execute();
93 $updated += $dbw->affectedRows();
94 }
95
96 if ( $numRows < $batchSize ) {
97 // We must have reached the end
98 break;
99 }
100
101 // @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
102 $this->output( "... rev_id=$last, updated $updated\n" );
103 $conds = [ $dbw->expr( 'rev_id', '>', $last ) ];
104
105 // Sleep between batches for replication to catch up
106 $this->waitForReplication();
107 if ( $sleep > 0 ) {
108 sleep( $sleep );
109 }
110 }
111 $this->output(
112 "Completed merge of revision_comment_temp into the revision table, "
113 . "$updated rows updated.\n"
114 );
115 return true;
116 }
117}
118
119// @codeCoverageIgnoreStart
120$maintClass = MigrateRevisionCommentTemp::class;
121require_once RUN_MAINTENANCE_IF_MAIN;
122// @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.
doDBUpdates()
Do the actual work.All child classes will need to implement this. Return true to log the update as do...
getUpdateKey()
Get the update key name to go in the update log table.string
const DB_PRIMARY
Definition defines.php:28