aboutsummaryrefslogtreecommitdiffstats
path: root/maintenance/migrateRevisionActorTemp.php
blob: e6f660e7be1c67adbb3a48b1c6672218688a996f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

use MediaWiki\Maintenance\LoggedUpdateMaintenance;

// @codeCoverageIgnoreStart
require_once __DIR__ . '/Maintenance.php';
// @codeCoverageIgnoreEnd

/**
 * Maintenance script that merges the revision_actor_temp table into the
 * revision table.
 *
 * @ingroup Maintenance
 * @since 1.37
 */
class MigrateRevisionActorTemp extends LoggedUpdateMaintenance {
	public function __construct() {
		parent::__construct();
		$this->addDescription(
			'Copy the data from the revision_actor_temp into the revision table'
		);
		$this->addOption(
			'sleep',
			'Sleep time (in seconds) between every batch. Default: 0',
			false,
			true
		);
		$this->addOption( 'start', 'Start after this rev_id', false, true );
	}

	protected function getUpdateKey() {
		return __CLASS__;
	}

	protected function doDBUpdates() {
		$batchSize = $this->getBatchSize();

		$dbw = $this->getDB( DB_PRIMARY );
		if ( !$dbw->fieldExists( 'revision', 'rev_actor', __METHOD__ ) ) {
			$this->output( "Run update.php to create rev_actor.\n" );
			return false;
		}
		if ( !$dbw->tableExists( 'revision_actor_temp', __METHOD__ ) ) {
			$this->output( "revision_actor_temp does not exist, so nothing to do.\n" );
			return true;
		}

		$this->output( "Merging the revision_actor_temp table into the revision table...\n" );
		$conds = [];
		$updated = 0;
		$start = (int)$this->getOption( 'start', 0 );
		if ( $start > 0 ) {
			$conds[] = $dbw->expr( 'rev_id', '>=', $start );
		}
		while ( true ) {
			$res = $dbw->newSelectQueryBuilder()
				->select( [ 'rev_id', 'rev_actor', 'revactor_actor' ] )
				->from( 'revision' )
				->join( 'revision_actor_temp', null, 'rev_id=revactor_rev' )
				->where( $conds )
				->limit( $batchSize )
				->orderBy( 'rev_id' )
				->caller( __METHOD__ )
				->fetchResultSet();

			$numRows = $res->numRows();

			$last = null;
			foreach ( $res as $row ) {
				$last = $row->rev_id;
				if ( !$row->rev_actor ) {
					$dbw->newUpdateQueryBuilder()
						->update( 'revision' )
						->set( [ 'rev_actor' => $row->revactor_actor ] )
						->where( [ 'rev_id' => $row->rev_id ] )
						->caller( __METHOD__ )->execute();
					$updated += $dbw->affectedRows();
				} elseif ( $row->rev_actor !== $row->revactor_actor ) {
					$this->error(
						"Revision ID $row->rev_id has rev_actor = $row->rev_actor and "
						. "revactor_actor = $row->revactor_actor. Ignoring the latter."
					);
				}
			}

			if ( $numRows < $batchSize ) {
				// We must have reached the end
				break;
			}

			// @phan-suppress-next-line PhanTypeSuspiciousStringExpression last is not-null when used
			$this->output( "... rev_id=$last, updated $updated\n" );
			$conds = [ $dbw->expr( 'rev_id', '>', $last ) ];

			// Sleep between batches for replication to catch up
			$this->waitForReplication();
			$sleep = (int)$this->getOption( 'sleep', 0 );
			if ( $sleep > 0 ) {
				sleep( $sleep );
			}
		}

		$this->output(
			"Completed merge of revision_actor into the revision table, "
			. "$updated rows updated.\n"
		);

		return true;
	}

}

// @codeCoverageIgnoreStart
$maintClass = MigrateRevisionActorTemp::class;
require_once RUN_MAINTENANCE_IF_MAIN;
// @codeCoverageIgnoreEnd