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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
<?php
/**
* API module for querying for changes to message group
* @file
* @author Abijeet Patro
* @license GPL-2.0-or-later
*/
use MediaWiki\Extensions\Translate\MessageSync\MessageSourceChange;
use MediaWiki\Extensions\Translate\Utilities\StringComparators\SimpleStringComparator;
/**
* API module for querying message group changes.
* @since 2019.10
* @license GPL-2.0-or-later
* @ingroup API TranslateAPI
*/
class ApiQueryManageMessageGroups extends ApiQueryBase {
private const RIGHT = 'translate-manage';
public function __construct( $query, $moduleName ) {
parent::__construct( $query, $moduleName, 'mmg' );
}
public function execute() {
$params = $this->extractRequestParams();
$groupId = $params['groupId'];
$msgKey = $params['messageKey'];
$name = $params['changesetName'] ?? MessageChangeStorage::DEFAULT_NAME;
$user = $this->getUser();
$allowed = $user->isAllowed( self::RIGHT );
if ( !$allowed ) {
$this->dieWithError( 'apierror-permissiondenied-generic', 'permissiondenied' );
}
$group = MessageGroups::getGroup( $groupId );
if ( !$group ) {
$this->dieWithError( 'apierror-translate-invalidgroup', 'invalidgroup' );
}
if ( !MessageChangeStorage::isValidCdbName( $name ) ) {
$this->dieWithError(
[ 'apierror-translate-invalid-changeset-name', wfEscapeWikiText( $name ) ],
'invalidchangeset'
);
}
$cdbPath = MessageChangeStorage::getCdbPath( $name );
$sourceChanges = MessageChangeStorage::getGroupChanges( $cdbPath, $groupId );
if ( $sourceChanges->getAllModifications() === [] ) {
$this->dieWithError( [ 'apierror-translate-smg-nochanges' ] );
}
$messages = $this->getPossibleRenames(
$sourceChanges, $group->getNamespace(), $msgKey, $group->getSourceLanguage()
);
$result = $this->getResult();
$result->addValue( [ 'query', $this->getModuleName() ], null, $messages );
}
/**
* Fetches the messages that can be used as possible renames for a given message.
* @param MessageSourceChange $sourceChanges
* @param int $groupNamespace Group namespace
* @param string $msgKey
* @param string $languageCode Language code
* @return array
*/
protected function getPossibleRenames( MessageSourceChange $sourceChanges, $groupNamespace,
$msgKey, $languageCode
) {
$deletions = $sourceChanges->getDeletions( $languageCode );
$targetMsg = $sourceChanges->findMessage(
$languageCode, $msgKey, [ MessageSourceChange::ADDITION, MessageSourceChange::RENAME ]
);
$stringComparator = new SimpleStringComparator();
$renameList = [];
// compare deleted messages with the target message and get the similarity.
foreach ( $deletions as $deletion ) {
if ( $deletion['content'] === null ) {
continue;
}
$similarity = $stringComparator->getSimilarity(
$deletion['content'],
// @phan-suppress-next-line PhanTypeArraySuspiciousNullable
$targetMsg['content']
);
$title = Title::makeTitle(
$groupNamespace,
TranslateUtils::title( $deletion['key'], $languageCode, $groupNamespace )
);
$renameList[] = [
'key' => $deletion['key'],
'content' => $deletion['content'],
'similarity' => $similarity,
'link' => $title->getFullURL(),
'title' => $title->getPrefixedText()
];
}
// sort them based on similarity
usort( $renameList, function ( $a, $b ) {
return -( $a['similarity'] <=> $b['similarity'] );
} );
return $renameList;
}
public function getAllowedParams() {
$params = parent::getAllowedParams();
$params['groupId'] = [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
];
$params['messageKey'] = [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
];
$params['changesetName'] = [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_DFLT => MessageChangeStorage::DEFAULT_NAME
];
return $params;
}
protected function getExamplesMessages() {
return [
'action=query&meta=managemessagegroup&mmggroupId=hello
&mmgchangesetName=default&mmgmessageKey=world' => 'apihelp-query+managemessagegroups-example-1',
];
}
}
|