SearchApi.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. use MediaWiki\MediaWikiServices;
  3. /**
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. * http://www.gnu.org/copyleft/gpl.html
  18. *
  19. * @file
  20. * @since 1.28
  21. */
  22. /**
  23. * Traits for API components that use a SearchEngine.
  24. * @ingroup API
  25. */
  26. trait SearchApi {
  27. /**
  28. * When $wgSearchType is null, $wgSearchAlternatives[0] is null. Null isn't
  29. * a valid option for an array for PARAM_TYPE, so we'll use a fake name
  30. * that can't possibly be a class name and describes what the null behavior
  31. * does
  32. */
  33. private static $BACKEND_NULL_PARAM = 'database-backed';
  34. /**
  35. * The set of api parameters that are shared between api calls that
  36. * call the SearchEngine. Primarily this defines parameters that
  37. * are utilized by self::buildSearchEngine().
  38. *
  39. * @param bool $isScrollable True if the api offers scrolling
  40. * @return array
  41. */
  42. public function buildCommonApiParams( $isScrollable = true ) {
  43. $params = [
  44. 'search' => [
  45. ApiBase::PARAM_TYPE => 'string',
  46. ApiBase::PARAM_REQUIRED => true,
  47. ],
  48. 'namespace' => [
  49. ApiBase::PARAM_DFLT => NS_MAIN,
  50. ApiBase::PARAM_TYPE => 'namespace',
  51. ApiBase::PARAM_ISMULTI => true,
  52. ],
  53. 'limit' => [
  54. ApiBase::PARAM_DFLT => 10,
  55. ApiBase::PARAM_TYPE => 'limit',
  56. ApiBase::PARAM_MIN => 1,
  57. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  58. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
  59. ],
  60. ];
  61. if ( $isScrollable ) {
  62. $params['offset'] = [
  63. ApiBase::PARAM_DFLT => 0,
  64. ApiBase::PARAM_TYPE => 'integer',
  65. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  66. ];
  67. }
  68. $searchConfig = MediaWikiServices::getInstance()->getSearchEngineConfig();
  69. $alternatives = $searchConfig->getSearchTypes();
  70. if ( count( $alternatives ) > 1 ) {
  71. if ( $alternatives[0] === null ) {
  72. $alternatives[0] = self::$BACKEND_NULL_PARAM;
  73. }
  74. $this->allowedParams['backend'] = [
  75. ApiBase::PARAM_DFLT => $searchConfig->getSearchType(),
  76. ApiBase::PARAM_TYPE => $alternatives,
  77. ];
  78. // @todo: support profile selection when multiple
  79. // backends are available. The solution could be to
  80. // merge all possible profiles and let ApiBase
  81. // subclasses do the check. Making ApiHelp and ApiSandbox
  82. // comprehensive might be more difficult.
  83. } else {
  84. $params += $this->buildProfileApiParam();
  85. }
  86. return $params;
  87. }
  88. /**
  89. * Build the profile api param definitions. Makes bold assumption only one search
  90. * engine is available, ensure that is true before calling.
  91. *
  92. * @return array array containing available additional api param definitions.
  93. * Empty if profiles are not supported by the searchEngine implementation.
  94. */
  95. private function buildProfileApiParam() {
  96. $configs = $this->getSearchProfileParams();
  97. $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
  98. $params = [];
  99. foreach ( $configs as $paramName => $paramConfig ) {
  100. $profiles = $searchEngine->getProfiles( $paramConfig['profile-type'],
  101. $this->getContext()->getUser() );
  102. if ( !$profiles ) {
  103. continue;
  104. }
  105. $types = [];
  106. $helpMessages = [];
  107. $defaultProfile = null;
  108. foreach ( $profiles as $profile ) {
  109. $types[] = $profile['name'];
  110. if ( isset( $profile['desc-message'] ) ) {
  111. $helpMessages[$profile['name']] = $profile['desc-message'];
  112. }
  113. if ( !empty( $profile['default'] ) ) {
  114. $defaultProfile = $profile['name'];
  115. }
  116. }
  117. $params[$paramName] = [
  118. ApiBase::PARAM_TYPE => $types,
  119. ApiBase::PARAM_HELP_MSG => $paramConfig['help-message'],
  120. ApiBase::PARAM_HELP_MSG_PER_VALUE => $helpMessages,
  121. ApiBase::PARAM_DFLT => $defaultProfile,
  122. ];
  123. }
  124. return $params;
  125. }
  126. /**
  127. * Build the search engine to use.
  128. * If $params is provided then the following searchEngine options
  129. * will be set:
  130. * - backend: which search backend to use
  131. * - limit: mandatory
  132. * - offset: optional, if set limit will be incremented by
  133. * one ( to support the continue parameter )
  134. * - namespace: mandatory
  135. * - search engine profiles defined by SearchApi::getSearchProfileParams()
  136. * @param string[]|null $params API request params (must be sanitized by
  137. * ApiBase::extractRequestParams() before)
  138. * @return SearchEngine the search engine
  139. */
  140. public function buildSearchEngine( array $params = null ) {
  141. if ( $params != null ) {
  142. $type = isset( $params['backend'] ) ? $params['backend'] : null;
  143. if ( $type === self::$BACKEND_NULL_PARAM ) {
  144. $type = null;
  145. }
  146. $searchEngine = MediaWikiServices::getInstance()->getSearchEngineFactory()->create( $type );
  147. $limit = $params['limit'];
  148. $searchEngine->setNamespaces( $params['namespace'] );
  149. $offset = null;
  150. if ( isset( $params['offset'] ) ) {
  151. // If the API supports offset then it probably
  152. // wants to fetch limit+1 so it can check if
  153. // more results are available to properly set
  154. // the continue param
  155. $offset = $params['offset'];
  156. $limit += 1;
  157. }
  158. $searchEngine->setLimitOffset( $limit, $offset );
  159. // Initialize requested search profiles.
  160. $configs = $this->getSearchProfileParams();
  161. foreach ( $configs as $paramName => $paramConfig ) {
  162. if ( isset( $params[$paramName] ) ) {
  163. $searchEngine->setFeatureData(
  164. $paramConfig['profile-type'],
  165. $params[$paramName]
  166. );
  167. }
  168. }
  169. } else {
  170. $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
  171. }
  172. return $searchEngine;
  173. }
  174. /**
  175. * @return array[] array of arrays mapping from parameter name to a two value map
  176. * containing 'help-message' and 'profile-type' keys.
  177. */
  178. abstract public function getSearchProfileParams();
  179. /**
  180. * @return IContextSource
  181. */
  182. abstract public function getContext();
  183. }