MediaWiki  master
SearchApi.php
Go to the documentation of this file.
1 <?php
2 
27 trait SearchApi {
28 
30  private $searchEngineConfig = null;
31 
33  private $searchEngineFactory = null;
34 
35  private function checkDependenciesSet() {
36  // Since this is a trait, we can't have a constructor where the services
37  // that we need are injected. Instead, the api modules that use this trait
38  // are responsible for setting them (since api modules *can* have services
39  // injected). Double check that the api module did indeed set them
40  if ( !$this->searchEngineConfig || !$this->searchEngineFactory ) {
41  throw new MWException(
42  'SearchApi requires both a SearchEngineConfig and SearchEngineFactory to be set'
43  );
44  }
45  }
46 
53  private static $BACKEND_NULL_PARAM = 'database-backed';
54 
63  public function buildCommonApiParams( $isScrollable = true ) {
64  $this->checkDependenciesSet();
65 
66  $params = [
67  'search' => [
68  ApiBase::PARAM_TYPE => 'string',
70  ],
71  'namespace' => [
73  ApiBase::PARAM_TYPE => 'namespace',
74  ApiBase::PARAM_ISMULTI => true,
75  ],
76  'limit' => [
77  ApiBase::PARAM_DFLT => 10,
78  ApiBase::PARAM_TYPE => 'limit',
79  ApiBase::PARAM_MIN => 1,
82  ],
83  ];
84  if ( $isScrollable ) {
85  $params['offset'] = [
87  ApiBase::PARAM_TYPE => 'integer',
88  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
89  ];
90  }
91 
92  $alternatives = $this->searchEngineConfig->getSearchTypes();
93  if ( count( $alternatives ) > 1 ) {
94  if ( $alternatives[0] === null ) {
95  $alternatives[0] = self::$BACKEND_NULL_PARAM;
96  }
97  $params['backend'] = [
98  ApiBase::PARAM_DFLT => $this->searchEngineConfig->getSearchType(),
99  ApiBase::PARAM_TYPE => $alternatives,
100  ];
101  // @todo: support profile selection when multiple
102  // backends are available. The solution could be to
103  // merge all possible profiles and let ApiBase
104  // subclasses do the check. Making ApiHelp and ApiSandbox
105  // comprehensive might be more difficult.
106  } else {
107  $params += $this->buildProfileApiParam();
108  }
109 
110  return $params;
111  }
112 
121  private function buildProfileApiParam() {
122  $this->checkDependenciesSet();
123 
124  $configs = $this->getSearchProfileParams();
125  $searchEngine = $this->searchEngineFactory->create();
126  $params = [];
127  foreach ( $configs as $paramName => $paramConfig ) {
128  $profiles = $searchEngine->getProfiles(
129  $paramConfig['profile-type'],
130  $this->getContext()->getUser()
131  );
132  if ( !$profiles ) {
133  continue;
134  }
135 
136  $types = [];
137  $helpMessages = [];
138  $defaultProfile = null;
139  foreach ( $profiles as $profile ) {
140  $types[] = $profile['name'];
141  if ( isset( $profile['desc-message'] ) ) {
142  $helpMessages[$profile['name']] = $profile['desc-message'];
143  }
144 
145  if ( !empty( $profile['default'] ) ) {
146  $defaultProfile = $profile['name'];
147  }
148  }
149 
150  $params[$paramName] = [
151  ApiBase::PARAM_TYPE => $types,
152  ApiBase::PARAM_HELP_MSG => $paramConfig['help-message'],
153  ApiBase::PARAM_HELP_MSG_PER_VALUE => $helpMessages,
154  ApiBase::PARAM_DFLT => $defaultProfile,
155  ];
156  }
157 
158  return $params;
159  }
160 
174  public function buildSearchEngine( array $params = null ) {
175  $this->checkDependenciesSet();
176 
177  if ( $params == null ) {
178  return $this->searchEngineFactory->create();
179  }
180 
181  $type = $params['backend'] ?? null;
182  if ( $type === self::$BACKEND_NULL_PARAM ) {
183  $type = null;
184  }
185  $searchEngine = $this->searchEngineFactory->create( $type );
186  $searchEngine->setNamespaces( $params['namespace'] );
187  $searchEngine->setLimitOffset( $params['limit'], $params['offset'] ?? null );
188 
189  // Initialize requested search profiles.
190  $configs = $this->getSearchProfileParams();
191  foreach ( $configs as $paramName => $paramConfig ) {
192  if ( isset( $params[$paramName] ) ) {
193  $searchEngine->setFeatureData(
194  $paramConfig['profile-type'],
195  $params[$paramName]
196  );
197  }
198  }
199  return $searchEngine;
200  }
201 
206  abstract public function getSearchProfileParams();
207 
211  abstract public function getContext();
212 }
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:78
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:105
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
$searchEngineFactory
SearchEngineFactory null $searchEngineFactory
Definition: SearchApi.php:33
SearchEngineFactory
Factory class for SearchEngine.
Definition: SearchEngineFactory.php:12
getContext
getContext()
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:75
MWException
MediaWiki exception.
Definition: MWException.php:29
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:163
buildSearchEngine
buildSearchEngine(array $params=null)
Build the search engine to use.
Definition: SearchApi.php:174
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:73
buildProfileApiParam
buildProfileApiParam()
Build the profile api param definitions.
Definition: SearchApi.php:121
checkDependenciesSet
checkDependenciesSet()
Definition: SearchApi.php:35
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:165
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:74
SearchEngineConfig
Configuration handling class for SearchEngine.
Definition: SearchEngineConfig.php:12
$BACKEND_NULL_PARAM
static $BACKEND_NULL_PARAM
When $wgSearchType is null, $wgSearchAlternatives[0] is null.
Definition: SearchApi.php:53
ApiBase\PARAM_HELP_MSG_PER_VALUE
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:138
SearchApi
trait SearchApi
Traits for API components that use a SearchEngine.
Definition: SearchApi.php:27
getSearchProfileParams
getSearchProfileParams()
buildCommonApiParams
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition: SearchApi.php:63
$type
$type
Definition: testCompression.php:52