summaryrefslogtreecommitdiff
blob: 101ecab786d3159bff9122eacc82a0fbdc9bc9fe (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\Synchronization;

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

/**
 * Query information in the group synchronization cache.
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2021.01
 */
class QueryGroupSyncCacheMaintenanceScript extends BaseMaintenanceScript {
	public function __construct() {
		parent::__construct();
		$this->addDescription( 'Query the contents of the group synchronization cache' );

		$this->addOption(
			'group',
			'(optional) Group Id being queried',
			self::OPTIONAL,
			self::HAS_ARG
		);

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

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

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

		$groupSyncCache = Services::getInstance()->getGroupSynchronizationCache();

		$groupId = $this->getOption( 'group' );
		if ( $groupId ) {
			$groupMessages = $groupSyncCache->getGroupMessages( $groupId );
			$this->displayGroupMessages( $groupId, $groupMessages );
		} else {
			$groups = $groupSyncCache->getGroupsInSync();
			$this->displayGroups( $groups );
		}
	}

	private function displayGroups( array $groupIds ): void {
		if ( !$groupIds ) {
			$this->output( "No groups found in synchronization\n" );
			return;
		}

		$this->output( "Groups found in sync:\n" );
		foreach ( $groupIds as $groupId ) {
			$this->output( "\t- $groupId\n" );
		}
	}

	/**
	 * @param string $groupId
	 * @param MessageUpdateParameter[] $groupMessages
	 */
	private function displayGroupMessages( string $groupId, array $groupMessages ): void {
		if ( !$groupMessages ) {
			$this->output( "No messages found for group $groupId\n" );
			return;
		}

		$this->output( "Messages in group $groupId:\n" );
		foreach ( $groupMessages as $message ) {
			$this->displayMessageDetails( $message );
		}
	}

	private function displayMessageDetails( MessageUpdateParameter $messageParam ): void {
		$tags = [];
		if ( $messageParam->isRename() ) {
			$tags[] = 'rename';
		}

		if ( $messageParam->isFuzzy() ) {
			$tags[] = 'fuzzy';
		}

		$otherLangs = $messageParam->getOtherLangs() ?: [ 'N/A' ];
		$this->output( "\t- Title: " . $messageParam->getPageName() . "\n" );
		$this->output( "\t  Tags: " . ( $tags ? implode( ', ', $tags ) : 'N/A' ) . "\n" );
		if ( $messageParam->isRename() ) {
			$this->output( "\t  Target: " . $messageParam->getTargetValue() . "\n" );
			$this->output( "\t  Replacement: " . $messageParam->getReplacementValue() . "\n" );
			$this->output( "\t  Other languages: " . ( implode( ', ', $otherLangs ) ) . "\n" );
		}
	}
}