Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
ClearGroupSyncCacheMaintenanceScript.php
1<?php
2declare( strict_types = 1 );
3
5
8use MediaWiki\MediaWikiServices;
9
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}
Minimal service container.
Definition Services.php:38
isGroupBeingProcessed(string $groupId)
Check if the group is in synchronization.
Constants for making code for maintenance scripts more readable.
Finds external changes for file based message groups.