Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
ClearGroupSyncCacheMaintenanceScript
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
182
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 validateParamsAndArgs
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 clearGroupFromSync
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Synchronization;
5
6use MediaWiki\Extension\Translate\Services;
7use MediaWiki\Extension\Translate\Utilities\BaseMaintenanceScript;
8use MediaWiki\MediaWikiServices;
9
10/**
11 * Clear the contents of the group synchronization cache
12 * @author Abijeet Patro
13 * @license GPL-2.0-or-later
14 * @since 2021.01
15 */
16class ClearGroupSyncCacheMaintenanceScript extends BaseMaintenanceScript {
17    public function __construct() {
18        parent::__construct();
19        $this->addDescription( 'Clear the contents of the group synchronization cache for a single or all groups' );
20
21        $this->addOption(
22            'group',
23            '(optional) Group Id being cleared',
24            self::OPTIONAL,
25            self::HAS_ARG
26        );
27        $this->addOption(
28            'all',
29            '(optional) Clear all groups'
30        );
31
32        $this->requireExtension( 'Translate' );
33    }
34
35    public function execute() {
36        $config = MediaWikiServices::getInstance()->getMainConfig();
37
38        if ( !$config->get( 'TranslateGroupSynchronizationCache' ) ) {
39            $this->fatalError( 'GroupSynchronizationCache is not enabled' );
40        }
41
42        $this->validateParamsAndArgs();
43        $groupId = $this->getOption( 'group' );
44        $all = $this->hasOption( 'all' );
45        $groupSyncCache = Services::getInstance()->getGroupSynchronizationCache();
46
47        if ( $groupId ) {
48            $this->clearGroupFromSync( $groupSyncCache, $groupId );
49            $this->output( "Ended synchronization for group: $groupId\n" );
50        } elseif ( $all ) {
51            // Remove all groups
52            $groupsInSync = $groupSyncCache->getGroupsInSync();
53            $this->output( 'Found ' . count( $groupsInSync ) . " groups in sync.\n" );
54            foreach ( $groupsInSync as $groupId ) {
55                $this->clearGroupFromSync( $groupSyncCache, $groupId );
56                $this->output( "Ended synchronization for group: $groupId\n" );
57            }
58        }
59    }
60
61    public function validateParamsAndArgs() {
62        parent::validateParamsAndArgs();
63
64        $group = $this->getOption( 'group' );
65        $all = $this->hasOption( 'all' );
66
67        if ( $all && $group !== null ) {
68            $this->fatalError( 'The "all" and "group" options cannot be used together.' );
69        }
70
71        if ( !$all && $group === null ) {
72            $this->fatalError( 'One of "all" OR "group" options must be specified.' );
73        }
74    }
75
76    private function clearGroupFromSync( GroupSynchronizationCache $groupSyncCache, string $groupId ): void {
77        if ( !$groupSyncCache->isGroupBeingProcessed( $groupId ) ) {
78            $this->fatalError( "$groupId is currently not being processed" );
79        }
80
81        $groupSyncCache->forceEndSync( $groupId );
82    }
83}