SearchApi.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $params['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. * @suppress PhanTypeMismatchDimFetch
  95. */
  96. private function buildProfileApiParam() {
  97. $configs = $this->getSearchProfileParams();
  98. $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
  99. $params = [];
  100. foreach ( $configs as $paramName => $paramConfig ) {
  101. $profiles = $searchEngine->getProfiles( $paramConfig['profile-type'],
  102. $this->getContext()->getUser() );
  103. if ( !$profiles ) {
  104. continue;
  105. }
  106. $types = [];
  107. $helpMessages = [];
  108. $defaultProfile = null;
  109. foreach ( $profiles as $profile ) {
  110. $types[] = $profile['name'];
  111. if ( isset( $profile['desc-message'] ) ) {
  112. $helpMessages[$profile['name']] = $profile['desc-message'];
  113. }
  114. if ( !empty( $profile['default'] ) ) {
  115. $defaultProfile = $profile['name'];
  116. }
  117. }
  118. $params[$paramName] = [
  119. ApiBase::PARAM_TYPE => $types,
  120. ApiBase::PARAM_HELP_MSG => $paramConfig['help-message'],
  121. ApiBase::PARAM_HELP_MSG_PER_VALUE => $helpMessages,
  122. ApiBase::PARAM_DFLT => $defaultProfile,
  123. ];
  124. }
  125. return $params;
  126. }
  127. /**
  128. * Build the search engine to use.
  129. * If $params is provided then the following searchEngine options
  130. * will be set:
  131. * - backend: which search backend to use
  132. * - limit: mandatory
  133. * - offset: optional
  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 = $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 = $params['offset'] ?? null;
  150. $searchEngine->setLimitOffset( $limit, $offset );
  151. // Initialize requested search profiles.
  152. $configs = $this->getSearchProfileParams();
  153. foreach ( $configs as $paramName => $paramConfig ) {
  154. if ( isset( $params[$paramName] ) ) {
  155. $searchEngine->setFeatureData(
  156. $paramConfig['profile-type'],
  157. $params[$paramName]
  158. );
  159. }
  160. }
  161. } else {
  162. $searchEngine = MediaWikiServices::getInstance()->newSearchEngine();
  163. }
  164. return $searchEngine;
  165. }
  166. /**
  167. * @return array[] array of arrays mapping from parameter name to a two value map
  168. * containing 'help-message' and 'profile-type' keys.
  169. */
  170. abstract public function getSearchProfileParams();
  171. /**
  172. * @return IContextSource
  173. */
  174. abstract public function getContext();
  175. }