Completed
Push — master ( a2db4a...affb95 )
by
unknown
07:39 queued 10s
created

newFromGlobalState()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace Wikibase\Client\ChangeModification;
6
7
use MediaWiki\MediaWikiServices;
8
use Title;
9
use Wikimedia\Assert\Assert;
10
use Wikimedia\Rdbms\LBFactory;
11
12
/**
13
 * Job for notifying a client wiki of a batch of revision visibility changes on the repository.
14
 *
15
 * @license GPL-2.0-or-later
16
 * @author Marius Hoch
17
 */
18
class ChangeVisibilityNotificationJob extends ChangeModificationNotificationJob {
19
20
	private $batchSize;
21
	private $lbFactory;
22
23
	/**
24
	 * Constructs a ChangeVisibilityNotificationJob for the repo revisions given.
25
	 *
26
	 * @param LBFactory $lbFactory
27
	 * @param int $batchSize
28
	 * @param array $params Contains the name of the repo, revisionIdentifiersJson to redact
29
	 *   and the visibilityBitFlag to set.
30
	 */
31
	public function __construct( LBFactory $lbFactory, int $batchSize, array $params = [] ) {
32
		parent::__construct( 'ChangeVisibilityNotification', $lbFactory->getMainLB(), $params );
33
34
		Assert::parameter(
35
			isset( $params['visibilityBitFlag'] ),
36
			'$params',
37
			'$params[\'visibilityBitFlag\'] not set.'
38
		);
39
40
		$this->lbFactory = $lbFactory;
41
		$this->batchSize = $batchSize;
42
	}
43
44
	public static function newFromGlobalState( Title $unused, array $params ) {
45
		$mwServices = MediaWikiServices::getInstance();
46
47
		return new self(
48
			$mwServices->getDBLoadBalancerFactory(),
49
			$mwServices->getMainConfig()->get( 'UpdateRowsPerQuery' ),
50
			$params
51
		);
52
	}
53
54
	/**
55
	 * @param int[] $relevantChanges
56
	 */
57
	protected function modifyChanges( array $relevantChanges ): void {
58
		$visibilityBitFlag = $this->params['visibilityBitFlag'];
59
60
		$dbw = $this->loadBalancer->getConnection( DB_MASTER );
61
62
		foreach ( array_chunk( $relevantChanges, $this->batchSize ) as $rcIdBatch ) {
63
			$dbw->update(
64
				'recentchanges',
65
				[ 'rc_deleted' => $visibilityBitFlag ],
66
				[ 'rc_id' => $rcIdBatch ],
67
				__METHOD__
68
			);
69
70
			$this->lbFactory->waitForReplication();
71
		}
72
	}
73
74
}
75