Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
CheckTranslationActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation;
5
6use ApiBase;
9use MediaWiki\Title\Title;
10use Wikimedia\ParamValidator\ParamValidator;
11
13class CheckTranslationActionApi extends ApiBase {
14 public function execute(): void {
15 $params = $this->extractRequestParams();
16
17 $title = Title::newFromText( $params[ 'title' ] );
18 if ( !$title ) {
19 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['title'] ) ] );
20 }
21 $handle = new MessageHandle( $title );
22 $translation = $params[ 'translation' ];
23
24 $validationResult = $this->validateTranslation( $handle, $translation );
25
26 $validationOutput = [ 'errors' => [], 'warnings' => [] ];
27 if ( $validationResult ) {
28 $validationOutput['errors'] =
29 $validationResult->getDescriptiveErrors( $this->getContext() );
30 $validationOutput['warnings'] =
31 $validationResult->getDescriptiveWarnings( $this->getContext() );
32 }
33
34 $this->getResult()->addValue( null, 'validation', $validationOutput );
35 }
36
37 private function validateTranslation( MessageHandle $handle, string $translation ): ?ValidationResult {
38 if ( $handle->isDoc() || !$handle->isValid() ) {
39 return null;
40 }
41
42 $messageValidator = $handle->getGroup()->getValidator();
43 if ( !$messageValidator ) {
44 return null;
45 }
46
47 $definition = $this->getDefinition( $handle );
48 if ( $definition === null ) {
49 // Very unlikely to happen since the handle is already found to be valid
50 return null;
51 }
52
53 $message = new FatMessage( $handle->getKey(), $definition );
54 $message->setTranslation( $translation );
55
56 $validationResult = $messageValidator->validateMessage( $message, $handle->getCode() );
57
58 return $validationResult;
59 }
60
61 private function getDefinition( MessageHandle $handle ): ?string {
62 $group = $handle->getGroup();
63 if ( method_exists( $group, 'getMessageContent' ) ) {
64 // @phan-suppress-next-line PhanUndeclaredMethod
65 return $group->getMessageContent( $handle );
66 } else {
67 return $group->getMessage( $handle->getKey(), $group->getSourceLanguage() );
68 }
69 }
70
71 protected function getAllowedParams(): array {
72 return [
73 'title' => [
74 ParamValidator::PARAM_TYPE => 'string',
75 ParamValidator::PARAM_REQUIRED => true,
76 ],
77 'translation' => [
78 ParamValidator::PARAM_TYPE => 'string',
79 ParamValidator::PARAM_REQUIRED => true,
80 ],
81 ];
82 }
83
84 public function isInternal(): bool {
85 return true;
86 }
87}
Message object where you can directly set the translation.
Class for pointing to messages, like Title class is for titles.
getKey()
Returns the identified or guessed message key.
getGroup()
Get the primary MessageGroup this message belongs to.
isValid()
Checks if the handle corresponds to a known message.
isDoc()
Determine whether the current handle is for message documentation.
Container for validation issues returned by MessageValidator.