summaryrefslogtreecommitdiff
blob: d5cf156833ce0088d43159ad838a7875867235f1 (plain)
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
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\Synchronization;

use MediaWiki\Extension\Translate\Services;
use MediaWiki\Extension\Translate\Utilities\BaseMaintenanceScript;
use MediaWiki\MediaWikiServices;

/**
 * Clear the contents of the group synchronization cache
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2021.01
 */
class ClearGroupSyncCacheMaintenanceScript extends BaseMaintenanceScript {
	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Clear the contents of the group synchronization cache for a single or all groups' );

		$this->addOption(
			'group',
			'(optional) Group Id being cleared',
			self::OPTIONAL,
			self::HAS_ARG
		);
		$this->addOption(
			'all',
			'(optional) Clear all groups'
		);

		$this->requireExtension( 'Translate' );
	}

	public function execute() {
		$config = MediaWikiServices::getInstance()->getMainConfig();

		if ( !$config->get( 'TranslateGroupSynchronizationCache' ) ) {
			$this->fatalError( 'GroupSynchronizationCache is not enabled' );
		}

		$this->validateParamsAndArgs();
		$groupId = $this->getOption( 'group' );
		$all = $this->hasOption( 'all' );
		$groupSyncCache = Services::getInstance()->getGroupSynchronizationCache();

		if ( $groupId ) {
			$this->clearGroupFromSync( $groupSyncCache, $groupId );
			$this->output( "Ended synchronization for group: $groupId\n" );
		} elseif ( $all ) {
			// Remove all groups
			$groupsInSync = $groupSyncCache->getGroupsInSync();
			$this->output( 'Found ' . count( $groupsInSync ) . " groups in sync.\n" );
			foreach ( $groupsInSync as $groupId ) {
				$this->clearGroupFromSync( $groupSyncCache, $groupId );
				$this->output( "Ended synchronization for group: $groupId\n" );
			}
		}
	}

	public function validateParamsAndArgs() {
		parent::validateParamsAndArgs();

		$group = $this->getOption( 'group' );
		$all = $this->hasOption( 'all' );

		if ( $all && $group !== null ) {
			$this->fatalError( 'The "all" and "group" options cannot be used together.' );
		}

		if ( !$all && $group === null ) {
			$this->fatalError( 'One of "all" OR "group" options must be specified.' );
		}
	}

	private function clearGroupFromSync( GroupSynchronizationCache $groupSyncCache, string $groupId ): void {
		if ( !$groupSyncCache->isGroupBeingProcessed( $groupId ) ) {
			$this->fatalError( "$groupId is currently not being processed" );
		}

		$groupSyncCache->forceEndSync( $groupId );
	}
}