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 MediaWiki\Api\ApiBase;
7use MediaWiki\Api\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    private EntitySearch $entitySearch;
18    private const GROUPS = 'groups';
19    private const MESSAGES = 'messages';
20
21    public function __construct(
22        ApiMain $mainModule,
23        string $moduleName,
24        EntitySearch $entitySearch
25    ) {
26        parent::__construct( $mainModule, $moduleName );
27        $this->entitySearch = $entitySearch;
28    }
29
30    public function execute() {
31        $query = $this->getParameter( 'query' );
32        $maxResults = $this->getParameter( 'limit' );
33        $entityTypes = $this->getParameter( 'entitytype' );
34        $groupTypeFilter = $this->getParameter( 'grouptypes' );
35
36        $searchResults = [];
37        $remainingResults = $maxResults;
38
39        if ( in_array( self::GROUPS, $entityTypes ) ) {
40            $searchResults[ self::GROUPS ] = $this->entitySearch
41                ->searchStaticMessageGroups( $query, $maxResults, $groupTypeFilter );
42            $remainingResults = $maxResults - count( $searchResults[ self::GROUPS ] );
43        }
44
45        if ( in_array( self::MESSAGES, $entityTypes ) && $remainingResults > 0 ) {
46            $searchResults[ self::MESSAGES ] = $this->entitySearch
47                ->searchMessages( $query, $remainingResults );
48        }
49
50        $this->getResult()->addValue( null, $this->getModuleName(), $searchResults );
51    }
52
53    protected function getAllowedParams(): array {
54        return [
55            'entitytype' => [
56                ParamValidator::PARAM_TYPE => [ self::GROUPS, self::MESSAGES ],
57                ParamValidator::PARAM_ISMULTI => true,
58                ParamValidator::PARAM_DEFAULT => implode( '|', [ self::GROUPS, self::MESSAGES ] )
59            ],
60            'query' => [
61                ParamValidator::PARAM_TYPE => 'string',
62                ParamValidator::PARAM_DEFAULT => ''
63            ],
64            'limit' => [
65                ParamValidator::PARAM_TYPE => 'limit',
66                ParamValidator::PARAM_DEFAULT => 10,
67                NumericDef::PARAM_MAX => ApiBase::LIMIT_SML1
68            ],
69            'grouptypes' => [
70                ParamValidator::PARAM_ISMULTI => true,
71                ParamValidator::PARAM_DEFAULT => [],
72                ParamValidator::PARAM_TYPE => array_keys( $this->entitySearch->getGroupTypes() )
73            ]
74        ];
75    }
76
77    public function isInternal(): bool {
78        // Temporarily until stable
79        return true;
80    }
81}