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
|
<?php
/**
* @since 2017.10
* @license GPL-2.0-or-later
*/
class ApiTranslationCheck extends ApiBase {
public function execute() {
$params = $this->extractRequestParams();
$title = Title::newFromText( $params[ 'title' ] );
if ( !$title ) {
$this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
}
$handle = new MessageHandle( $title );
$translation = $params[ 'translation' ];
$validationResult = $this->validateTranslation( $handle, $translation );
$validationOutput = [ 'errors' => [], 'warnings' => [] ];
if ( $validationResult ) {
$validationOutput['errors'] =
$validationResult->getDescriptiveErrors( $this->getContext() );
$validationOutput['warnings'] =
$validationResult->getDescriptiveWarnings( $this->getContext() );
}
$this->getResult()->addValue( null, 'validation', $validationOutput );
}
private function validateTranslation( MessageHandle $handle, $translation ) {
if ( $handle->isDoc() || !$handle->isValid() ) {
return null;
}
$messageValidator = $handle->getGroup()->getValidator();
if ( !$messageValidator ) {
return null;
}
$definition = $this->getDefinition( $handle );
$message = new FatMessage( $handle->getKey(), $definition );
$message->setTranslation( $translation );
$validationResult = $messageValidator->validateMessage( $message, $handle->getCode() );
return $validationResult;
}
private function getDefinition( MessageHandle $handle ) {
$group = $handle->getGroup();
if ( is_callable( [ $group, 'getMessageContent' ] ) ) {
// @phan-suppress-next-line PhanUndeclaredMethod
return $group->getMessageContent( $handle );
} else {
return $group->getMessage( $handle->getKey(), $group->getSourceLanguage() );
}
}
protected function getAllowedParams() {
return [
'title' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
'translation' => [
ApiBase::PARAM_TYPE => 'string',
ApiBase::PARAM_REQUIRED => true,
],
];
}
public function isInternal() {
return true;
}
}
|