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