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 {
17 private EntitySearch $entitySearch;
18 private const GROUPS = 'groups';
19 private const MESSAGES = 'messages';
20
21 public function __construct( ApiMain $mainModule, $moduleName, EntitySearch $entitySearch ) {
22 parent::__construct( $mainModule, $moduleName );
23 $this->entitySearch = $entitySearch;
24 }
25
26 public function execute() {
27 $query = $this->getParameter( 'query' );
28 $maxResults = $this->getParameter( 'limit' );
29 $entityTypes = $this->getParameter( 'entitytype' );
30 $groupTypeFilter = $this->getParameter( 'grouptypes' );
31
32 $searchResults = [];
33 $remainingResults = $maxResults;
34
35 if ( in_array( self::GROUPS, $entityTypes ) ) {
36 $searchResults[ self::GROUPS ] = $this->entitySearch
37 ->searchStaticMessageGroups( $query, $maxResults, $groupTypeFilter );
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_DEFAULT => ''
59 ],
60 'limit' => [
61 ParamValidator::PARAM_TYPE => 'limit',
62 ParamValidator::PARAM_DEFAULT => 10,
63 NumericDef::PARAM_MAX => ApiBase::LIMIT_SML1
64 ],
65 'grouptypes' => [
66 ParamValidator::PARAM_ISMULTI => true,
67 ParamValidator::PARAM_DEFAULT => [],
68 ParamValidator::PARAM_TYPE => array_keys( $this->entitySearch->getGroupTypes() )
69 ]
70 ];
71 }
72
73 public function isInternal(): bool {
74 // Temporarily until stable
75 return true;
76 }
77}
Service for searching message groups and message keys.