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