Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationEntitySearchActionApi
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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
11/**
12 * Action API module for searching message groups and message keys.
13 * @author Niklas Laxström
14 * @license GPL-2.0-or-later
15 */
16class TranslationEntitySearchActionApi extends ApiBase {
17    /** @var EntitySearch */
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        $groupTypeFilter = $this->getParameter( 'grouptypes' );
32
33        $searchResults = [];
34        $remainingResults = $maxResults;
35
36        if ( in_array( self::GROUPS, $entityTypes ) ) {
37            $searchResults[ self::GROUPS ] = $this->entitySearch
38                ->searchStaticMessageGroups( $query, $maxResults, $groupTypeFilter );
39            $remainingResults = $maxResults - count( $searchResults[ self::GROUPS ] );
40        }
41
42        if ( in_array( self::MESSAGES, $entityTypes ) && $remainingResults > 0 ) {
43            $searchResults[ self::MESSAGES ] = $this->entitySearch
44                ->searchMessages( $query, $remainingResults );
45        }
46
47        $this->getResult()->addValue( null, $this->getModuleName(), $searchResults );
48    }
49
50    protected function getAllowedParams(): array {
51        return [
52            'entitytype' => [
53                ParamValidator::PARAM_TYPE => [ self::GROUPS, self::MESSAGES ],
54                ParamValidator::PARAM_ISMULTI => true,
55                ParamValidator::PARAM_DEFAULT => implode( '|', [ self::GROUPS, self::MESSAGES ] )
56            ],
57            'query' => [
58                ParamValidator::PARAM_TYPE => 'string',
59                ParamValidator::PARAM_REQUIRED => true
60            ],
61            'limit' => [
62                ParamValidator::PARAM_TYPE => 'limit',
63                ParamValidator::PARAM_DEFAULT => 10,
64                NumericDef::PARAM_MAX => ApiBase::LIMIT_SML1
65            ],
66            'grouptypes' => [
67                ParamValidator::PARAM_ISMULTI => true,
68                ParamValidator::PARAM_DEFAULT => [],
69                ParamValidator::PARAM_TYPE => array_keys( $this->entitySearch->getGroupTypes() )
70            ]
71        ];
72    }
73
74    public function isInternal(): bool {
75        // Temporarily until stable
76        return true;
77    }
78}