MediaWiki  master
SearchApi.php
Go to the documentation of this file.
1 <?php
2 
4 
29 trait SearchApi {
30 
37  private static $BACKEND_NULL_PARAM = 'database-backed';
38 
47  public function buildCommonApiParams( $isScrollable = true ) {
48  $params = [
49  'search' => [
50  ApiBase::PARAM_TYPE => 'string',
52  ],
53  'namespace' => [
55  ApiBase::PARAM_TYPE => 'namespace',
56  ApiBase::PARAM_ISMULTI => true,
57  ],
58  'limit' => [
59  ApiBase::PARAM_DFLT => 10,
60  ApiBase::PARAM_TYPE => 'limit',
61  ApiBase::PARAM_MIN => 1,
64  ],
65  ];
66  if ( $isScrollable ) {
67  $params['offset'] = [
69  ApiBase::PARAM_TYPE => 'integer',
70  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
71  ];
72  }
73 
74  $searchConfig = MediaWikiServices::getInstance()->getSearchEngineConfig();
75  $alternatives = $searchConfig->getSearchTypes();
76  if ( count( $alternatives ) > 1 ) {
77  if ( $alternatives[0] === null ) {
78  $alternatives[0] = self::$BACKEND_NULL_PARAM;
79  }
80  $params['backend'] = [
81  ApiBase::PARAM_DFLT => $searchConfig->getSearchType(),
82  ApiBase::PARAM_TYPE => $alternatives,
83  ];
84  // @todo: support profile selection when multiple
85  // backends are available. The solution could be to
86  // merge all possible profiles and let ApiBase
87  // subclasses do the check. Making ApiHelp and ApiSandbox
88  // comprehensive might be more difficult.
89  } else {
90  $params += $this->buildProfileApiParam();
91  }
92 
93  return $params;
94  }
95 
103  private function buildProfileApiParam() {
104  $configs = $this->getSearchProfileParams();
105  $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
106  $params = [];
107  foreach ( $configs as $paramName => $paramConfig ) {
108  $profiles = $searchEngine->getProfiles( $paramConfig['profile-type'],
109  $this->getContext()->getUser() );
110  if ( !$profiles ) {
111  continue;
112  }
113 
114  $types = [];
115  $helpMessages = [];
116  $defaultProfile = null;
117  foreach ( $profiles as $profile ) {
118  $types[] = $profile['name'];
119  if ( isset( $profile['desc-message'] ) ) {
120  $helpMessages[$profile['name']] = $profile['desc-message'];
121  }
122  if ( !empty( $profile['default'] ) ) {
123  $defaultProfile = $profile['name'];
124  }
125  }
126 
127  $params[$paramName] = [
128  ApiBase::PARAM_TYPE => $types,
129  ApiBase::PARAM_HELP_MSG => $paramConfig['help-message'],
130  ApiBase::PARAM_HELP_MSG_PER_VALUE => $helpMessages,
131  ApiBase::PARAM_DFLT => $defaultProfile,
132  ];
133  }
134 
135  return $params;
136  }
137 
151  public function buildSearchEngine( array $params = null ) {
152  if ( $params != null ) {
153  $type = $params['backend'] ?? null;
154  if ( $type === self::$BACKEND_NULL_PARAM ) {
155  $type = null;
156  }
157  $searchEngine = MediaWikiServices::getInstance()->getSearchEngineFactory()->create( $type );
158  $limit = $params['limit'];
159  $searchEngine->setNamespaces( $params['namespace'] );
160  $offset = $params['offset'] ?? null;
161  $searchEngine->setLimitOffset( $limit, $offset );
162 
163  // Initialize requested search profiles.
164  $configs = $this->getSearchProfileParams();
165  foreach ( $configs as $paramName => $paramConfig ) {
166  if ( isset( $params[$paramName] ) ) {
167  $searchEngine->setFeatureData(
168  $paramConfig['profile-type'],
169  $params[$paramName]
170  );
171  }
172  }
173  } else {
174  $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
175  }
176  return $searchEngine;
177  }
178 
183  abstract public function getSearchProfileParams();
184 
188  abstract public function getContext();
189 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:94
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:261
const NS_MAIN
Definition: Defines.php:60
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getSearchProfileParams()
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:259
buildCommonApiParams( $isScrollable=true)
The set of api parameters that are shared between api calls that call the SearchEngine.
Definition: SearchApi.php:47
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE &#39;integer&#39; and &#39;limit&#39;.
Definition: ApiBase.php:97
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
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:164
getContext()
$params
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right, for PARAM_TYPE &#39;limit&#39;.
Definition: ApiBase.php:103
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:131
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
buildSearchEngine(array $params=null)
Build the search engine to use.
Definition: SearchApi.php:151
buildProfileApiParam()
Build the profile api param definitions.
Definition: SearchApi.php:103
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:58
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE &#39;integer&#39; and &#39;limit&#39;.
Definition: ApiBase.php:106