Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslationEntitySearchActionApi.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\TranslatorInterface;
5
6use ApiBase;
7use ApiMain;
8use Wikimedia\ParamValidator\ParamValidator;
9use Wikimedia\ParamValidator\TypeDef\NumericDef;
10
16class TranslationEntitySearchActionApi extends ApiBase {
18 private $entitySearch;
19 private const GROUPS = 'groups';
20 private const MESSAGES = 'messages';
21
22 public function __construct( ApiMain $mainModule, $moduleName, EntitySearch $entitySearch ) {
23 parent::__construct( $mainModule, $moduleName );
24 $this->entitySearch = $entitySearch;
25 }
26
27 public function execute() {
28 $query = $this->getParameter( 'query' );
29 $maxResults = $this->getParameter( 'limit' );
30 $entityTypes = $this->getParameter( 'entitytype' );
31
32 $searchResults = [];
33 $remainingResults = $maxResults;
34
35 if ( in_array( self::GROUPS, $entityTypes ) ) {
36 $searchResults[ self::GROUPS ] = $this->entitySearch
37 ->searchStaticMessageGroups( $query, $maxResults );
38 $remainingResults = $maxResults - count( $searchResults[ self::GROUPS ] );
39 }
40
41 if ( in_array( self::MESSAGES, $entityTypes ) && $remainingResults > 0 ) {
42 $searchResults[ self::MESSAGES ] = $this->entitySearch
43 ->searchMessages( $query, $remainingResults );
44 }
45
46 $this->getResult()->addValue( null, $this->getModuleName(), $searchResults );
47 }
48
49 protected function getAllowedParams(): array {
50 return [
51 'entitytype' => [
52 ParamValidator::PARAM_TYPE => [ self::GROUPS, self::MESSAGES ],
53 ParamValidator::PARAM_ISMULTI => true,
54 ParamValidator::PARAM_DEFAULT => implode( '|', [ self::GROUPS, self::MESSAGES ] )
55 ],
56 'query' => [
57 ParamValidator::PARAM_TYPE => 'string',
58 ParamValidator::PARAM_REQUIRED => true
59 ],
60 'limit' => [
61 ParamValidator::PARAM_TYPE => 'limit',
62 ParamValidator::PARAM_DEFAULT => 10,
63 NumericDef::PARAM_MAX => ApiBase::LIMIT_SML1
64 ],
65 ];
66 }
67
68 public function isInternal(): bool {
69 // Temporarily until stable
70 return true;
71 }
72}
Service for searching message groups and message keys.