MediaWiki master
ApiOpenSearch.php
Go to the documentation of this file.
1<?php
31
35class ApiOpenSearch extends ApiBase {
36 use SearchApi;
37
39 private $format = null;
41 private $fm = null;
42
43 private LinkBatchFactory $linkBatchFactory;
44 private UrlUtils $urlUtils;
45
54 public function __construct(
55 ApiMain $mainModule,
56 $moduleName,
57 LinkBatchFactory $linkBatchFactory,
58 SearchEngineConfig $searchEngineConfig,
59 SearchEngineFactory $searchEngineFactory,
60 UrlUtils $urlUtils
61 ) {
62 parent::__construct( $mainModule, $moduleName );
63 $this->linkBatchFactory = $linkBatchFactory;
64 // Services needed in SearchApi trait
65 $this->searchEngineConfig = $searchEngineConfig;
66 $this->searchEngineFactory = $searchEngineFactory;
67 $this->urlUtils = $urlUtils;
68 }
69
75 protected function getFormat() {
76 if ( $this->format === null ) {
77 $format = $this->getParameter( 'format' );
78
79 if ( str_ends_with( $format, 'fm' ) ) {
80 $this->format = substr( $format, 0, -2 );
81 $this->fm = 'fm';
82 } else {
83 $this->format = $format;
84 $this->fm = '';
85 }
86 }
87 return $this->format;
88 }
89
90 public function getCustomPrinter() {
91 switch ( $this->getFormat() ) {
92 case 'json':
93 return new ApiOpenSearchFormatJson(
94 $this->getMain(), $this->fm, $this->getParameter( 'warningsaserror' )
95 );
96
97 case 'xml':
98 $printer = $this->getMain()->createPrinterByName( 'xml' . $this->fm );
99 '@phan-var ApiFormatXml $printer';
101 $printer->setRootElement( 'SearchSuggestion' );
102 return $printer;
103
104 default:
105 ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
106 }
107 }
108
109 public function execute() {
110 $params = $this->extractRequestParams();
111 $search = $params['search'];
112
113 // Open search results may be stored for a very long time
114 $this->getMain()->setCacheMaxAge(
115 $this->getConfig()->get( MainConfigNames::SearchSuggestCacheExpiry ) );
116 $this->getMain()->setCacheMode( 'public' );
117 $results = $this->search( $search, $params );
118
119 // Allow hooks to populate extracts and images
120 $this->getHookRunner()->onApiOpenSearchSuggest( $results );
121
122 // Trim extracts, if necessary
123 $length = $this->getConfig()->get( MainConfigNames::OpenSearchDescriptionLength );
124 foreach ( $results as &$r ) {
125 if ( is_string( $r['extract'] ) && !$r['extract trimmed'] ) {
126 $r['extract'] = self::trimExtract( $r['extract'], $length );
127 }
128 }
129
130 // Populate result object
131 $this->populateResult( $search, $results );
132 }
133
142 private function search( $search, array $params ) {
143 $searchEngine = $this->buildSearchEngine( $params );
144 $titles = $searchEngine->extractTitles( $searchEngine->completionSearchWithVariants( $search ) );
145 $results = [];
146
147 if ( !$titles ) {
148 return $results;
149 }
150
151 // Special pages need unique integer ids in the return list, so we just
152 // assign them negative numbers because those won't clash with the
153 // always positive articleIds that non-special pages get.
154 $nextSpecialPageId = -1;
155
156 if ( $params['redirects'] === null ) {
157 // Backwards compatibility, don't resolve for JSON.
158 $resolveRedir = $this->getFormat() !== 'json';
159 } else {
160 $resolveRedir = $params['redirects'] === 'resolve';
161 }
162
163 if ( $resolveRedir ) {
164 // Query for redirects
165 $redirects = [];
166 $lb = $this->linkBatchFactory->newLinkBatch( $titles );
167 if ( !$lb->isEmpty() ) {
168 $db = $this->getDB();
169 $res = $db->newSelectQueryBuilder()
170 ->select( [ 'page_namespace', 'page_title', 'rd_namespace', 'rd_title' ] )
171 ->from( 'page' )
172 ->join( 'redirect', null, [ 'rd_from = page_id' ] )
173 ->where( [
174 'rd_interwiki' => '',
175 $lb->constructSet( 'page', $db )
176 ] )
177 ->caller( __METHOD__ )
178 ->fetchResultSet();
179 foreach ( $res as $row ) {
180 $redirects[$row->page_namespace][$row->page_title] =
181 [ $row->rd_namespace, $row->rd_title ];
182 }
183 }
184
185 // Bypass any redirects
186 $seen = [];
187 foreach ( $titles as $title ) {
188 $ns = $title->getNamespace();
189 $dbkey = $title->getDBkey();
190 $from = null;
191 if ( isset( $redirects[$ns][$dbkey] ) ) {
192 [ $ns, $dbkey ] = $redirects[$ns][$dbkey];
193 $from = $title;
194 $title = Title::makeTitle( $ns, $dbkey );
195 }
196 if ( !isset( $seen[$ns][$dbkey] ) ) {
197 $seen[$ns][$dbkey] = true;
198 $resultId = $title->getArticleID();
199 if ( $resultId === 0 ) {
200 $resultId = $nextSpecialPageId;
201 $nextSpecialPageId--;
202 }
203 $results[$resultId] = [
204 'title' => $title,
205 'redirect from' => $from,
206 'extract' => false,
207 'extract trimmed' => false,
208 'image' => false,
209 'url' => (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ),
210 ];
211 }
212 }
213 } else {
214 foreach ( $titles as $title ) {
215 $resultId = $title->getArticleID();
216 if ( $resultId === 0 ) {
217 $resultId = $nextSpecialPageId;
218 $nextSpecialPageId--;
219 }
220 $results[$resultId] = [
221 'title' => $title,
222 'redirect from' => null,
223 'extract' => false,
224 'extract trimmed' => false,
225 'image' => false,
226 'url' => (string)$this->urlUtils->expand( $title->getFullURL(), PROTO_CURRENT ),
227 ];
228 }
229 }
230
231 return $results;
232 }
233
238 protected function populateResult( $search, &$results ) {
239 $result = $this->getResult();
240
241 switch ( $this->getFormat() ) {
242 case 'json':
243 // http://www.opensearch.org/Specifications/OpenSearch/Extensions/Suggestions/1.1
244 $result->addArrayType( null, 'array' );
245 $result->addValue( null, 0, strval( $search ) );
246 $terms = [];
247 $descriptions = [];
248 $urls = [];
249 foreach ( $results as $r ) {
250 $terms[] = $r['title']->getPrefixedText();
251 $descriptions[] = strval( $r['extract'] );
252 $urls[] = $r['url'];
253 }
254 $result->addValue( null, 1, $terms );
255 $result->addValue( null, 2, $descriptions );
256 $result->addValue( null, 3, $urls );
257 break;
258
259 case 'xml':
260 // https://msdn.microsoft.com/en-us/library/cc891508(v=vs.85).aspx
261 $imageKeys = [
262 'source' => true,
263 'alt' => true,
264 'width' => true,
265 'height' => true,
266 'align' => true,
267 ];
268 $items = [];
269 foreach ( $results as $r ) {
270 $item = [
271 'Text' => $r['title']->getPrefixedText(),
272 'Url' => $r['url'],
273 ];
274 if ( is_string( $r['extract'] ) && $r['extract'] !== '' ) {
275 $item['Description'] = $r['extract'];
276 }
277 if ( is_array( $r['image'] ) && isset( $r['image']['source'] ) ) {
278 $item['Image'] = array_intersect_key( $r['image'], $imageKeys );
279 }
280 ApiResult::setSubelementsList( $item, array_keys( $item ) );
281 $items[] = $item;
282 }
283 ApiResult::setIndexedTagName( $items, 'Item' );
284 $result->addValue( null, 'version', '2.0' );
285 $result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' );
286 $result->addValue( null, 'Query', strval( $search ) );
287 $result->addSubelementsList( null, 'Query' );
288 $result->addValue( null, 'Section', $items );
289 break;
290
291 default:
292 ApiBase::dieDebug( __METHOD__, "Unsupported format '{$this->getFormat()}'" );
293 }
294 }
295
296 public function getAllowedParams() {
297 $allowedParams = $this->buildCommonApiParams( false ) + [
298 'suggest' => [
299 ParamValidator::PARAM_DEFAULT => false,
300 // Deprecated since 1.35
301 ParamValidator::PARAM_DEPRECATED => true,
302 ],
303 'redirects' => [
304 ParamValidator::PARAM_TYPE => [ 'return', 'resolve' ],
306 ApiBase::PARAM_HELP_MSG_APPEND => [ 'apihelp-opensearch-param-redirects-append' ],
307 ],
308 'format' => [
309 ParamValidator::PARAM_DEFAULT => 'json',
310 ParamValidator::PARAM_TYPE => [ 'json', 'jsonfm', 'xml', 'xmlfm' ],
311 ],
312 'warningsaserror' => false,
313 ];
314
315 // Use open search specific default limit
316 $allowedParams['limit'][ParamValidator::PARAM_DEFAULT] = $this->getConfig()->get(
317 MainConfigNames::OpenSearchDefaultLimit
318 );
319
320 return $allowedParams;
321 }
322
323 public function getSearchProfileParams() {
324 return [
325 'profile' => [
326 'profile-type' => SearchEngine::COMPLETION_PROFILE_TYPE,
327 'help-message' => 'apihelp-query+prefixsearch-param-profile'
328 ],
329 ];
330 }
331
332 protected function getExamplesMessages() {
333 return [
334 'action=opensearch&search=Te'
335 => 'apihelp-opensearch-example-te',
336 ];
337 }
338
339 public function getHelpUrls() {
340 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Opensearch';
341 }
342
353 public static function trimExtract( $text, $length ) {
354 static $regex = null;
355
356 if ( $regex === null ) {
357 $endchars = [
358 '([^\d])\.\s', '\!\s', '\?\s', // regular ASCII
359 '。', // full-width ideographic full-stop
360 '.', '!', '?', // double-width roman forms
361 '。', // half-width ideographic full stop
362 ];
363 $endgroup = implode( '|', $endchars );
364 $end = "(?:$endgroup)";
365 $sentence = ".{{$length},}?$end+";
366 $regex = "/^($sentence)/u";
367 }
368
369 $matches = [];
370 if ( preg_match( $regex, $text, $matches ) ) {
371 return trim( $matches[1] );
372 } else {
373 // Just return the first line
374 return trim( explode( "\n", $text )[0] );
375 }
376 }
377
384 public static function getOpenSearchTemplate( $type ) {
385 $services = MediaWikiServices::getInstance();
386 $canonicalServer = $services->getMainConfig()->get( MainConfigNames::CanonicalServer );
387 $searchEngineConfig = $services->getSearchEngineConfig();
388 $ns = implode( '|', $searchEngineConfig->defaultNamespaces() );
389 if ( !$ns ) {
390 $ns = '0';
391 }
392
393 switch ( $type ) {
394 case 'application/x-suggestions+json':
395 return $canonicalServer .
396 wfScript( 'api' ) . '?action=opensearch&search={searchTerms}&namespace=' . $ns;
397
398 case 'application/x-suggestions+xml':
399 return $canonicalServer .
400 wfScript( 'api' ) .
401 '?action=opensearch&format=xml&search={searchTerms}&namespace=' . $ns;
402
403 default:
404 throw new InvalidArgumentException( __METHOD__ . ": Unknown type '$type'" );
405 }
406 }
407}
getDB()
const PROTO_CURRENT
Definition Defines.php:209
wfScript( $script='index')
Get the URL path to a MediaWiki entry point.
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.
Definition SearchApi.php:66
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:67
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:964
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition ApiBase.php:1811
getMain()
Get the main module.
Definition ApiBase.php:580
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:183
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:215
getResult()
Get the result object.
Definition ApiBase.php:701
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:842
getHookRunner()
Get an ApiHookRunner for running core API hooks.
Definition ApiBase.php:786
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:68
static trimExtract( $text, $length)
Trim an extract to a sensible length.
getHelpUrls()
Return links to more detailed help pages about the module.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiMain $mainModule, $moduleName, LinkBatchFactory $linkBatchFactory, SearchEngineConfig $searchEngineConfig, SearchEngineFactory $searchEngineFactory, UrlUtils $urlUtils)
getCustomPrinter()
If the module may only be used with a certain format module, it should override this method to return...
getExamplesMessages()
Returns usage examples for this module.
populateResult( $search, &$results)
getFormat()
Get the output format.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
static getOpenSearchTemplate( $type)
Fetch the template for a type.
A class containing constants representing the names of configuration variables.
Service locator for MediaWiki core services.
Represents a title within MediaWiki.
Definition Title.php:78
A service to expand, parse, and otherwise manipulate URLs.
Definition UrlUtils.php:16
Configuration handling class for SearchEngine.
Factory class for SearchEngine.
Service for formatting and validating API parameters.
trait SearchApi
Traits for API components that use a SearchEngine.
Definition SearchApi.php:31