summaryrefslogtreecommitdiff
blob: 151fda0316f21bd708df60a8c5e82c0f51d45029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface;

use ApiBase;
use ApiMain;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\NumericDef;

/**
 * Action API module for searching message groups and message keys.
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */
class TranslationEntitySearchActionApi extends ApiBase {
	/** @var EntitySearch */
	private $entitySearch;

	public function __construct( ApiMain $mainModule, $moduleName, EntitySearch $entitySearch ) {
		parent::__construct( $mainModule, $moduleName );
		$this->entitySearch = $entitySearch;
	}

	public function execute() {
		$query = $this->getParameter( 'query' );
		$maxResults = $this->getParameter( 'limit' );

		$searchResults = $this->entitySearch->searchStaticMessageGroups( $query, $maxResults );
		$this->getResult()->addValue( null, $this->getModuleName(), array_values( $searchResults ) );
	}

	protected function getAllowedParams(): array {
		return [
			'query' => [
				ParamValidator::PARAM_TYPE => 'string',
				ParamValidator::PARAM_REQUIRED => true
			],
			'limit' => [
				ParamValidator::PARAM_TYPE => 'limit',
				ParamValidator::PARAM_DEFAULT => 10,
				NumericDef::PARAM_MAX => ApiBase::LIMIT_SML1,
				ParamValidator::PARAM_REQUIRED => false,
			],
		];
	}

	public function isInternal(): bool {
		// Temporarily until stable
		return true;
	}
}