ApiQuerySearch.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. * Created on July 30, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to perform full text search within wiki titles and content
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQuerySearch extends ApiQueryGeneratorBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'sr');
  36. }
  37. public function execute() {
  38. $this->run();
  39. }
  40. public function executeGenerator($resultPageSet) {
  41. $this->run($resultPageSet);
  42. }
  43. private function run($resultPageSet = null) {
  44. $params = $this->extractRequestParams();
  45. $limit = $params['limit'];
  46. $query = $params['search'];
  47. $what = $params['what'];
  48. if (strval($query) === '')
  49. $this->dieUsage("empty search string is not allowed", 'param-search');
  50. $search = SearchEngine::create();
  51. $search->setLimitOffset( $limit+1, $params['offset'] );
  52. $search->setNamespaces( $params['namespace'] );
  53. $search->showRedirects = $params['redirects'];
  54. if ($what == 'text') {
  55. $matches = $search->searchText( $query );
  56. } elseif( $what == 'title' ) {
  57. $matches = $search->searchTitle( $query );
  58. } else {
  59. // We default to title searches; this is a terrible legacy
  60. // of the way we initially set up the MySQL fulltext-based
  61. // search engine with separate title and text fields.
  62. // In the future, the default should be for a combined index.
  63. $what = 'title';
  64. $matches = $search->searchTitle( $query );
  65. // Not all search engines support a separate title search,
  66. // for instance the Lucene-based engine we use on Wikipedia.
  67. // In this case, fall back to full-text search (which will
  68. // include titles in it!)
  69. if( is_null( $matches ) ) {
  70. $what = 'text';
  71. $matches = $search->searchText( $query );
  72. }
  73. }
  74. if (is_null($matches))
  75. $this->dieUsage("{$what} search is disabled",
  76. "search-{$what}-disabled");
  77. $titles = array ();
  78. $count = 0;
  79. while( $result = $matches->next() ) {
  80. if (++ $count > $limit) {
  81. // We've reached the one extra which shows that there are additional items to be had. Stop here...
  82. $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
  83. break;
  84. }
  85. // Silently skip broken and missing titles
  86. if ($result->isBrokenTitle() || $result->isMissingRevision())
  87. continue;
  88. $title = $result->getTitle();
  89. if (is_null($resultPageSet)) {
  90. $vals = array();
  91. ApiQueryBase::addTitleInfo($vals, $title);
  92. $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
  93. if(!$fit)
  94. {
  95. $this->setContinueEnumParameter('offset', $params['offset'] + $count - 1);
  96. break;
  97. }
  98. } else {
  99. $titles[] = $title;
  100. }
  101. }
  102. if (is_null($resultPageSet)) {
  103. $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
  104. } else {
  105. $resultPageSet->populateFromTitles($titles);
  106. }
  107. }
  108. public function getAllowedParams() {
  109. return array (
  110. 'search' => null,
  111. 'namespace' => array (
  112. ApiBase :: PARAM_DFLT => 0,
  113. ApiBase :: PARAM_TYPE => 'namespace',
  114. ApiBase :: PARAM_ISMULTI => true,
  115. ),
  116. 'what' => array (
  117. ApiBase :: PARAM_DFLT => null,
  118. ApiBase :: PARAM_TYPE => array (
  119. 'title',
  120. 'text',
  121. )
  122. ),
  123. 'redirects' => false,
  124. 'offset' => 0,
  125. 'limit' => array (
  126. ApiBase :: PARAM_DFLT => 10,
  127. ApiBase :: PARAM_TYPE => 'limit',
  128. ApiBase :: PARAM_MIN => 1,
  129. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  130. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  131. )
  132. );
  133. }
  134. public function getParamDescription() {
  135. return array (
  136. 'search' => 'Search for all page titles (or content) that has this value.',
  137. 'namespace' => 'The namespace(s) to enumerate.',
  138. 'what' => 'Search inside the text or titles.',
  139. 'redirects' => 'Include redirect pages in the search.',
  140. 'offset' => 'Use this value to continue paging (return by query)',
  141. 'limit' => 'How many total pages to return.'
  142. );
  143. }
  144. public function getDescription() {
  145. return 'Perform a full text search';
  146. }
  147. protected function getExamples() {
  148. return array (
  149. 'api.php?action=query&list=search&srsearch=meaning',
  150. 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
  151. 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
  152. );
  153. }
  154. public function getVersion() {
  155. return __CLASS__ . ': $Id: ApiQuerySearch.php 47865 2009-02-27 16:03:01Z catrope $';
  156. }
  157. }