Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
QueryGroupSyncCacheMaintenanceScript | |
0.00% |
0 / 43 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
displayGroups | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
displayGroupMessages | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
displayMessageDetails | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Synchronization; |
5 | |
6 | use MediaWiki\Extension\Translate\Services; |
7 | use MediaWiki\Extension\Translate\Utilities\BaseMaintenanceScript; |
8 | use MediaWiki\MediaWikiServices; |
9 | |
10 | /** |
11 | * Query information in the group synchronization cache. |
12 | * @author Abijeet Patro |
13 | * @license GPL-2.0-or-later |
14 | * @since 2021.01 |
15 | */ |
16 | class QueryGroupSyncCacheMaintenanceScript extends BaseMaintenanceScript { |
17 | public function __construct() { |
18 | parent::__construct(); |
19 | $this->addDescription( 'Query the contents of the group synchronization cache' ); |
20 | |
21 | $this->addOption( |
22 | 'group', |
23 | '(optional) Group Id being queried', |
24 | self::OPTIONAL, |
25 | self::HAS_ARG |
26 | ); |
27 | |
28 | $this->requireExtension( 'Translate' ); |
29 | } |
30 | |
31 | public function execute() { |
32 | $config = MediaWikiServices::getInstance()->getMainConfig(); |
33 | |
34 | if ( !$config->get( 'TranslateGroupSynchronizationCache' ) ) { |
35 | $this->fatalError( 'GroupSynchronizationCache is not enabled' ); |
36 | } |
37 | |
38 | $groupSyncCache = Services::getInstance()->getGroupSynchronizationCache(); |
39 | |
40 | $groupId = $this->getOption( 'group' ); |
41 | if ( $groupId ) { |
42 | $groupMessages = $groupSyncCache->getGroupMessages( $groupId ); |
43 | $this->displayGroupMessages( $groupId, $groupMessages ); |
44 | } else { |
45 | $groups = $groupSyncCache->getGroupsInSync(); |
46 | $this->displayGroups( $groups ); |
47 | } |
48 | } |
49 | |
50 | private function displayGroups( array $groupIds ): void { |
51 | if ( !$groupIds ) { |
52 | $this->output( "No groups found in synchronization\n" ); |
53 | return; |
54 | } |
55 | |
56 | $this->output( "Groups found in sync:\n" ); |
57 | foreach ( $groupIds as $groupId ) { |
58 | $this->output( "\t- $groupId\n" ); |
59 | } |
60 | } |
61 | |
62 | /** |
63 | * @param string $groupId |
64 | * @param MessageUpdateParameter[] $groupMessages |
65 | */ |
66 | private function displayGroupMessages( string $groupId, array $groupMessages ): void { |
67 | if ( !$groupMessages ) { |
68 | $this->output( "No messages found for group $groupId\n" ); |
69 | return; |
70 | } |
71 | |
72 | $this->output( "Messages in group $groupId:\n" ); |
73 | foreach ( $groupMessages as $message ) { |
74 | $this->displayMessageDetails( $message ); |
75 | } |
76 | } |
77 | |
78 | private function displayMessageDetails( MessageUpdateParameter $messageParam ): void { |
79 | $tags = []; |
80 | if ( $messageParam->isRename() ) { |
81 | $tags[] = 'rename'; |
82 | } |
83 | |
84 | if ( $messageParam->isFuzzy() ) { |
85 | $tags[] = 'fuzzy'; |
86 | } |
87 | |
88 | $otherLangs = $messageParam->getOtherLangs() ?: [ 'N/A' ]; |
89 | $this->output( "\t- Title: " . $messageParam->getPageName() . "\n" ); |
90 | $this->output( "\t Tags: " . ( $tags ? implode( ', ', $tags ) : 'N/A' ) . "\n" ); |
91 | if ( $messageParam->isRename() ) { |
92 | $this->output( "\t Target: " . $messageParam->getTargetValue() . "\n" ); |
93 | $this->output( "\t Replacement: " . $messageParam->getReplacementValue() . "\n" ); |
94 | $this->output( "\t Other languages: " . ( implode( ', ', $otherLangs ) ) . "\n" ); |
95 | } |
96 | } |
97 | } |