From: Erik Bernhardson Date: Sat, 19 May 2018 13:42:54 +0000 (-0700) Subject: Expose sort orders from search engine in ApiQuerySearch X-Git-Tag: 1.34.0-rc.0~5083^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/banques/?a=commitdiff_plain;h=7bf3c405063370e101cf91c65f5356d057820622;p=lhc%2Fweb%2Fwiklou.git Expose sort orders from search engine in ApiQuerySearch Selectable sort orders were added to search some time ago, but never appear to have been used. Expose them in ApiQuerySearch. There is one large problem here, but it's not easily solvable. Specifically supported sort orders vary per search engine, but the available api parameters are calculated with the default search engine. This is already a problem with search profiles so it's nothing new, just continuing the existing problem. Much like search profiles we hide the functionality when multiple engines are available. Bug: T195071 Change-Id: I187a530bc973c08386d2c2126e02a85b952d9022 --- diff --git a/includes/api/ApiQuerySearch.php b/includes/api/ApiQuerySearch.php index 7d46a5fb08..e5e3f363e6 100644 --- a/includes/api/ApiQuerySearch.php +++ b/includes/api/ApiQuerySearch.php @@ -60,6 +60,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { // Create search engine instance and set options $search = $this->buildSearchEngine( $params ); + if ( isset( $params['sort'] ) ) { + $search->setSort( $params['sort'] ); + } $search->setFeatureData( 'rewrite', (bool)$params['enablerewrites'] ); $search->setFeatureData( 'interwiki', (bool)$interwiki ); @@ -391,6 +394,20 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { 'enablerewrites' => false, ]; + // If we have more than one engine the list of available sorts is + // difficult to represent. For now don't expose it. + $alternatives = MediaWiki\MediaWikiServices::getInstance() + ->getSearchEngineConfig() + ->getSearchTypes(); + if ( count( $alternatives ) == 1 ) { + $this->allowedParams['sort'] = [ + ApiBase::PARAM_DFLT => 'relevance', + ApiBase::PARAM_TYPE => MediaWiki\MediaWikiServices::getInstance() + ->newSearchEngine() + ->getValidSorts(), + ]; + } + return $this->allowedParams; } diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 6838e545d6..fa388d1af2 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -1164,6 +1164,7 @@ "apihelp-query+search-param-interwiki": "Include interwiki results in the search, if available.", "apihelp-query+search-param-backend": "Which search backend to use, if not the default.", "apihelp-query+search-param-enablerewrites": "Enable internal query rewriting. Some search backends can rewrite the query into another which is thought to provide better results, for instance by correcting spelling errors.", + "apihelp-query+search-param-sort": "Set the sort order of returned results.", "apihelp-query+search-example-simple": "Search for meaning.", "apihelp-query+search-example-text": "Search texts for meaning.", "apihelp-query+search-example-generator": "Get page info about the pages returned for a search for meaning.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 594bf8e685..5f9cc702f5 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -1087,6 +1087,7 @@ "apihelp-query+search-param-interwiki": "{{doc-apihelp-param|query+search|interwiki}}", "apihelp-query+search-param-backend": "{{doc-apihelp-param|query+search|backend}}", "apihelp-query+search-param-enablerewrites": "{{doc-apihelp-param|query+search|enablerewrites}}", + "apihelp-query+search-param-sort": "{{doc-apihelp-param|query+search|sort}}", "apihelp-query+search-example-simple": "{{doc-apihelp-example|query+search}}", "apihelp-query+search-example-text": "{{doc-apihelp-example|query+search}}", "apihelp-query+search-example-generator": "{{doc-apihelp-example|query+search}}",