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