33 private $searchEngineConfig =
null;
36 private $searchEngineFactory =
null;
38 private function checkDependenciesSet() {
43 if ( !$this->searchEngineConfig || !$this->searchEngineFactory ) {
44 throw new LogicException(
45 'SearchApi requires both a SearchEngineConfig and SearchEngineFactory to be set'
56 private static $BACKEND_NULL_PARAM =
'database-backed';
67 $this->checkDependenciesSet();
71 ParamValidator::PARAM_TYPE =>
'string',
72 ParamValidator::PARAM_REQUIRED =>
true,
75 ParamValidator::PARAM_DEFAULT =>
NS_MAIN,
76 ParamValidator::PARAM_TYPE =>
'namespace',
77 ParamValidator::PARAM_ISMULTI =>
true,
80 ParamValidator::PARAM_DEFAULT => 10,
81 ParamValidator::PARAM_TYPE =>
'limit',
82 IntegerDef::PARAM_MIN => 1,
87 if ( $isScrollable ) {
89 ParamValidator::PARAM_DEFAULT => 0,
90 ParamValidator::PARAM_TYPE =>
'integer',
95 $alternatives = $this->searchEngineConfig->getSearchTypes();
96 if ( count( $alternatives ) > 1 ) {
97 $alternatives[0] ??= self::$BACKEND_NULL_PARAM;
98 $params[
'backend'] = [
99 ParamValidator::PARAM_DEFAULT => $this->searchEngineConfig->getSearchType(),
100 ParamValidator::PARAM_TYPE => $alternatives,
108 $params += $this->buildProfileApiParam();
122 private function buildProfileApiParam() {
123 $this->checkDependenciesSet();
126 $searchEngine = $this->searchEngineFactory->create();
128 foreach ( $configs as $paramName => $paramConfig ) {
129 $profiles = $searchEngine->getProfiles(
130 $paramConfig[
'profile-type'],
139 $defaultProfile =
null;
140 foreach ( $profiles as $profile ) {
141 $types[] = $profile[
'name'];
142 if ( isset( $profile[
'desc-message'] ) ) {
143 $helpMessages[$profile[
'name']] = $profile[
'desc-message'];
146 if ( !empty( $profile[
'default'] ) ) {
147 $defaultProfile = $profile[
'name'];
151 $params[$paramName] = [
152 ParamValidator::PARAM_TYPE => $types,
155 ParamValidator::PARAM_DEFAULT => $defaultProfile,
176 $this->checkDependenciesSet();
178 if ( $params ==
null ) {
179 return $this->searchEngineFactory->create();
182 $type = $params[
'backend'] ??
null;
183 if (
$type === self::$BACKEND_NULL_PARAM ) {
186 $searchEngine = $this->searchEngineFactory->create(
$type );
187 $searchEngine->setNamespaces( $params[
'namespace'] );
188 $searchEngine->setLimitOffset( $params[
'limit'], $params[
'offset'] ?? 0 );
192 foreach ( $configs as $paramName => $paramConfig ) {
193 if ( isset( $params[$paramName] ) ) {
194 $searchEngine->setFeatureData(
195 $paramConfig[
'profile-type'],
200 return $searchEngine;
buildSearchEngine(array $params=null)
Build the search engine to use.
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
const LIMIT_BIG1
Fast query, standard limit.
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
const LIMIT_BIG2
Fast query, apihighlimits limit.
trait SearchApi
Traits for API components that use a SearchEngine.