MediaWiki master
SearchApi.php
Go to the documentation of this file.
1<?php
2
23namespace MediaWiki\Api;
24
25use LogicException;
27use SearchEngine;
32
37trait SearchApi {
38
39 private ?SearchEngineConfig $searchEngineConfig = null;
40 private ?SearchEngineFactory $searchEngineFactory = null;
41
42 private function checkDependenciesSet() {
43 // Since this is a trait, we can't have a constructor where the services
44 // that we need are injected. Instead, the api modules that use this trait
45 // are responsible for setting them (since api modules *can* have services
46 // injected). Double check that the api module did indeed set them
47 if ( $this->searchEngineConfig === null || $this->searchEngineFactory === null ) {
48 throw new LogicException(
49 'SearchApi requires both a SearchEngineConfig and SearchEngineFactory to be set'
50 );
51 }
52 }
53
61 private static $BACKEND_NULL_PARAM = 'database-backed';
62
71 public function buildCommonApiParams( $isScrollable = true ) {
72 $this->checkDependenciesSet();
73
74 $params = [
75 'search' => [
76 ParamValidator::PARAM_TYPE => 'string',
77 ParamValidator::PARAM_REQUIRED => true,
78 ],
79 'namespace' => [
80 ParamValidator::PARAM_DEFAULT => NS_MAIN,
81 ParamValidator::PARAM_TYPE => 'namespace',
82 ParamValidator::PARAM_ISMULTI => true,
83 ],
84 'limit' => [
85 ParamValidator::PARAM_DEFAULT => 10,
86 ParamValidator::PARAM_TYPE => 'limit',
87 IntegerDef::PARAM_MIN => 1,
88 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
89 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
90 ],
91 ];
92 if ( $isScrollable ) {
93 $params['offset'] = [
94 ParamValidator::PARAM_DEFAULT => 0,
95 IntegerDef::PARAM_MIN => 0,
96 ParamValidator::PARAM_TYPE => 'integer',
97 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
98 ];
99 }
100
101 $alternatives = $this->searchEngineConfig->getSearchTypes();
102 if ( count( $alternatives ) > 1 ) {
103 $alternatives[0] ??= self::$BACKEND_NULL_PARAM;
104 $params['backend'] = [
105 ParamValidator::PARAM_DEFAULT => $this->searchEngineConfig->getSearchType(),
106 ParamValidator::PARAM_TYPE => $alternatives,
107 ];
108 // @todo: support profile selection when multiple
109 // backends are available. The solution could be to
110 // merge all possible profiles and let ApiBase
111 // subclasses do the check. Making ApiHelp and ApiSandbox
112 // comprehensive might be more difficult.
113 } else {
114 $params += $this->buildProfileApiParam();
115 }
116
117 return $params;
118 }
119
128 private function buildProfileApiParam() {
129 $this->checkDependenciesSet();
130
131 $configs = $this->getSearchProfileParams();
132 $searchEngine = $this->searchEngineFactory->create();
133 $params = [];
134 foreach ( $configs as $paramName => $paramConfig ) {
135 $profiles = $searchEngine->getProfiles(
136 $paramConfig['profile-type'],
137 $this->getContext()->getUser()
138 );
139 if ( !$profiles ) {
140 continue;
141 }
142
143 $types = [];
144 $helpMessages = [];
145 $defaultProfile = null;
146 foreach ( $profiles as $profile ) {
147 $types[] = $profile['name'];
148 if ( isset( $profile['desc-message'] ) ) {
149 $helpMessages[$profile['name']] = $profile['desc-message'];
150 }
151
152 if ( !empty( $profile['default'] ) ) {
153 $defaultProfile = $profile['name'];
154 }
155 }
156
157 $params[$paramName] = [
158 ParamValidator::PARAM_TYPE => $types,
159 ApiBase::PARAM_HELP_MSG => $paramConfig['help-message'],
160 ApiBase::PARAM_HELP_MSG_PER_VALUE => $helpMessages,
161 ParamValidator::PARAM_DEFAULT => $defaultProfile,
162 ];
163 }
164
165 return $params;
166 }
167
181 public function buildSearchEngine( ?array $params = null ) {
182 $this->checkDependenciesSet();
183
184 if ( $params == null ) {
185 return $this->searchEngineFactory->create();
186 }
187
188 $type = $params['backend'] ?? null;
189 if ( $type === self::$BACKEND_NULL_PARAM ) {
190 $type = null;
191 }
192 $searchEngine = $this->searchEngineFactory->create( $type );
193 $searchEngine->setNamespaces( $params['namespace'] );
194 $searchEngine->setLimitOffset( $params['limit'], $params['offset'] ?? 0 );
195
196 // Initialize requested search profiles.
197 $configs = $this->getSearchProfileParams();
198 foreach ( $configs as $paramName => $paramConfig ) {
199 if ( isset( $params[$paramName] ) ) {
200 $searchEngine->setFeatureData(
201 $paramConfig['profile-type'],
202 $params[$paramName]
203 );
204 }
205 }
206 return $searchEngine;
207 }
208
213 abstract public function getSearchProfileParams();
214
218 abstract public function getContext();
219}
220
222class_alias( SearchApi::class, 'SearchApi' );
const NS_MAIN
Definition Defines.php:65
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:221
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:181
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition ApiBase.php:248
const LIMIT_BIG1
Fast query, standard limit.
Definition ApiBase.php:246
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
Contain a class for special pages.
Service for formatting and validating API parameters.
Type definition for integer types.
trait SearchApi
Traits for API components that use a SearchEngine.
Definition SearchApi.php:37
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:71
getSearchProfileParams()
buildSearchEngine(?array $params=null)
Build the search engine to use.
array $params
The job parameters.