Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / migrateRevisionActorTemp.php
blob23c6e8e4818bf2927226eae2197d38ec78c2145f
1 <?php
3 use MediaWiki\MediaWikiServices;
5 require_once __DIR__ . '/Maintenance.php';
7 /**
8 * Maintenance script that merges the revision_actor_temp table into the
9 * revision table.
11 * @ingroup Maintenance
12 * @since 1.37
14 class MigrateRevisionActorTemp extends LoggedUpdateMaintenance {
15 public function __construct() {
16 parent::__construct();
17 $this->addDescription(
18 'Copy the data from the revision_actor_temp into the revision table'
20 $this->addOption(
21 'sleep',
22 'Sleep time (in seconds) between every batch. Default: 0',
23 false,
24 true
26 $this->addOption( 'start', 'Start after this rev_id', false, true );
29 protected function getUpdateKey() {
30 return __CLASS__;
33 protected function doDBUpdates() {
34 $batchSize = $this->getBatchSize();
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;
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;
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 );
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();
64 $numRows = $res->numRows();
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__
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."
85 if ( $numRows < $batchSize ) {
86 // We must have reached the end
87 break;
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 ) ];
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 );
102 $this->output(
103 "Completed merge of revision_actor into the revision table, "
104 . "$updated rows updated.\n"
107 return true;
112 $maintClass = MigrateRevisionActorTemp::class;
113 require_once RUN_MAINTENANCE_IF_MAIN;