MediaWiki  master
migrateRevisionActorTemp.php
Go to the documentation of this file.
1 <?php
2 
4 
5 require_once __DIR__ . '/Maintenance.php';
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[] = 'rev_id >= ' . $dbw->addQuotes( $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->update(
71  'revision',
72  [ 'rev_actor' => $row->revactor_actor ],
73  [ 'rev_id' => $row->rev_id ],
74  __METHOD__
75  );
76  $updated += $dbw->affectedRows();
77  } elseif ( $row->rev_actor !== $row->revactor_actor ) {
78  $this->error(
79  "Revision ID $row->rev_id has rev_actor = $row->rev_actor and "
80  . "revactor_actor = $row->revactor_actor. Ignoring the latter."
81  );
82  }
83  }
84 
85  if ( $numRows < $batchSize ) {
86  // We must have reached the end
87  break;
88  }
89 
90  // @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
91  $this->output( "... rev_id=$last, updated $updated\n" );
92  $conds = [ 'rev_id > ' . $dbw->addQuotes( $last ) ];
93 
94  // Sleep between batches for replication to catch up
95  MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->waitForReplication();
96  $sleep = (int)$this->getOption( 'sleep', 0 );
97  if ( $sleep > 0 ) {
98  sleep( $sleep );
99  }
100  }
101 
102  $this->output(
103  "Completed merge of revision_actor into the revision table, "
104  . "$updated rows updated.\n"
105  );
106 
107  return true;
108  }
109 
110 }
111 
112 $maintClass = MigrateRevisionActorTemp::class;
113 require_once RUN_MAINTENANCE_IF_MAIN;
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.
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.
Service locator for MediaWiki core services.
Maintenance script that merges the revision_actor_temp table into the revision table.
__construct()
Default constructor.
getUpdateKey()
Get the update key name to go in the update log table.
const DB_PRIMARY
Definition: defines.php:28