MediaWiki master
SearchApi.php
Go to the documentation of this file.
1<?php
2
8
9namespace MediaWiki\Api;
10
11use LogicException;
16use Wikimedia\ParamValidator\ParamValidator;
17use Wikimedia\ParamValidator\TypeDef\IntegerDef;
18
23trait SearchApi {
24
25 private ?SearchEngineConfig $searchEngineConfig = null;
26 private ?SearchEngineFactory $searchEngineFactory = null;
27
28 private function checkDependenciesSet() {
29 // Since this is a trait, we can't have a constructor where the services
30 // that we need are injected. Instead, the api modules that use this trait
31 // are responsible for setting them (since api modules *can* have services
32 // injected). Double check that the api module did indeed set them
33 if ( $this->searchEngineConfig === null || $this->searchEngineFactory === null ) {
34 throw new LogicException(
35 'SearchApi requires both a SearchEngineConfig and SearchEngineFactory to be set'
36 );
37 }
38 }
39
46 private const BACKEND_NULL_PARAM = 'database-backed';
47
56 public function buildCommonApiParams( $isScrollable = true ) {
57 $this->checkDependenciesSet();
58
59 $params = [
60 'search' => [
61 ParamValidator::PARAM_TYPE => 'string',
62 ParamValidator::PARAM_REQUIRED => true,
63 ],
64 'namespace' => [
65 ParamValidator::PARAM_DEFAULT => NS_MAIN,
66 ParamValidator::PARAM_TYPE => 'namespace',
67 ParamValidator::PARAM_ISMULTI => true,
68 ],
69 'limit' => [
70 ParamValidator::PARAM_DEFAULT => 10,
71 ParamValidator::PARAM_TYPE => 'limit',
72 IntegerDef::PARAM_MIN => 1,
73 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
74 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
75 ],
76 ];
77 if ( $isScrollable ) {
78 $params['offset'] = [
79 ParamValidator::PARAM_DEFAULT => 0,
80 IntegerDef::PARAM_MIN => 0,
81 ParamValidator::PARAM_TYPE => 'integer',
82 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
83 ];
84 }
85
86 $alternatives = $this->searchEngineConfig->getSearchTypes();
87 if ( count( $alternatives ) > 1 ) {
88 $alternatives[0] ??= self::BACKEND_NULL_PARAM;
89 $params['backend'] = [
90 ParamValidator::PARAM_DEFAULT => $this->searchEngineConfig->getSearchType(),
91 ParamValidator::PARAM_TYPE => $alternatives,
92 ];
93 // @todo: support profile selection when multiple
94 // backends are available. The solution could be to
95 // merge all possible profiles and let ApiBase
96 // subclasses do the check. Making ApiHelp and ApiSandbox
97 // comprehensive might be more difficult.
98 } else {
99 $params += $this->buildProfileApiParam();
100 }
101
102 return $params;
103 }
104
113 private function buildProfileApiParam() {
114 $this->checkDependenciesSet();
115
116 $configs = $this->getSearchProfileParams();
117 $searchEngine = $this->searchEngineFactory->create();
118 $params = [];
119 foreach ( $configs as $paramName => $paramConfig ) {
120 $profiles = $searchEngine->getProfiles(
121 $paramConfig['profile-type'],
122 $this->getContext()->getUser()
123 );
124 if ( !$profiles ) {
125 continue;
126 }
127
128 $types = [];
129 $helpMessages = [];
130 $defaultProfile = null;
131 foreach ( $profiles as $profile ) {
132 $types[] = $profile['name'];
133 if ( isset( $profile['desc-message'] ) ) {
134 $helpMessages[$profile['name']] = $profile['desc-message'];
135 }
136
137 if ( !empty( $profile['default'] ) ) {
138 $defaultProfile = $profile['name'];
139 }
140 }
141
142 $params[$paramName] = [
143 ParamValidator::PARAM_TYPE => $types,
144 ApiBase::PARAM_HELP_MSG => $paramConfig['help-message'],
145 ApiBase::PARAM_HELP_MSG_PER_VALUE => $helpMessages,
146 ParamValidator::PARAM_DEFAULT => $defaultProfile,
147 ];
148 }
149
150 return $params;
151 }
152
166 public function buildSearchEngine( ?array $params = null ) {
167 $this->checkDependenciesSet();
168
169 if ( $params == null ) {
170 return $this->searchEngineFactory->create();
171 }
172
173 $type = $params['backend'] ?? null;
174 if ( $type === self::BACKEND_NULL_PARAM ) {
175 $type = null;
176 }
177 $searchEngine = $this->searchEngineFactory->create( $type );
178 $searchEngine->setNamespaces( $params['namespace'] );
179 $searchEngine->setLimitOffset( $params['limit'], $params['offset'] ?? 0 );
180
181 // Initialize requested search profiles.
182 $configs = $this->getSearchProfileParams();
183 foreach ( $configs as $paramName => $paramConfig ) {
184 if ( isset( $params[$paramName] ) ) {
185 $searchEngine->setFeatureData(
186 $paramConfig['profile-type'],
187 $params[$paramName]
188 );
189 }
190 }
191 return $searchEngine;
192 }
193
198 abstract public function getSearchProfileParams();
199
203 abstract public function getContext();
204}
205
207class_alias( SearchApi::class, 'SearchApi' );
const NS_MAIN
Definition Defines.php:51
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:205
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:165
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:232
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:230
array $params
Array of job parameters.
Definition Job.php:33
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
trait SearchApi
Traits for API components that use a SearchEngine.
Definition SearchApi.php:23
Interface for objects which can provide a MediaWiki context on request.
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition SearchApi.php:56
getSearchProfileParams()
buildSearchEngine(?array $params=null)
Build the search engine to use.