(bug 22339) Added srwhat=nearmatch to list=search to get a "go" result
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2
3 /**
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to perform full text search within wiki titles and content
33 *
34 * @ingroup API
35 */
36 class ApiQuerySearch extends ApiQueryGeneratorBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'sr' );
40 }
41
42 public function execute() {
43 $this->run();
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 private function run( $resultPageSet = null ) {
51 global $wgContLang;
52 $params = $this->extractRequestParams();
53
54 // Extract parameters
55 $limit = $params['limit'];
56 $query = $params['search'];
57 $what = $params['what'];
58 $searchInfo = array_flip( $params['info'] );
59 $prop = array_flip( $params['prop'] );
60
61 if ( strval( $query ) === '' ) {
62 $this->dieUsage( 'empty search string is not allowed', 'param-search' );
63 }
64
65 // Create search engine instance and set options
66 $search = SearchEngine::create();
67 $search->setLimitOffset( $limit + 1, $params['offset'] );
68 $search->setNamespaces( $params['namespace'] );
69 $search->showRedirects = $params['redirects'];
70
71 // Perform the actual search
72 if ( $what == 'text' ) {
73 $matches = $search->searchText( $query );
74 } elseif ( $what == 'title' ) {
75 $matches = $search->searchTitle( $query );
76 } elseif ( $what == 'nearmatch' ) {
77 $query = str_replace( '_', ' ', $query );
78 $matches = SearchEngine::getNearMatchResultSet( $query );
79 } else {
80 // We default to title searches; this is a terrible legacy
81 // of the way we initially set up the MySQL fulltext-based
82 // search engine with separate title and text fields.
83 // In the future, the default should be for a combined index.
84 $what = 'title';
85 $matches = $search->searchTitle( $query );
86
87 // Not all search engines support a separate title search,
88 // for instance the Lucene-based engine we use on Wikipedia.
89 // In this case, fall back to full-text search (which will
90 // include titles in it!)
91 if ( is_null( $matches ) ) {
92 $what = 'text';
93 $matches = $search->searchText( $query );
94 }
95 }
96 if ( is_null( $matches ) ) {
97 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
98 }
99
100 // Add search meta data to result
101 if ( isset( $searchInfo['totalhits'] ) ) {
102 $totalhits = $matches->getTotalHits();
103 if ( $totalhits !== null ) {
104 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
105 'totalhits', $totalhits );
106 }
107 }
108 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
109 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
110 'suggestion', $matches->getSuggestionQuery() );
111 }
112
113 // Add the search results to the result
114 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
115 $titles = array();
116 $count = 0;
117 while ( $result = $matches->next() ) {
118 if ( ++ $count > $limit ) {
119 // We've reached the one extra which shows that there are additional items to be had. Stop here...
120 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
121 break;
122 }
123
124 // Silently skip broken and missing titles
125 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
126 continue;
127 }
128
129 $title = $result->getTitle();
130 if ( is_null( $resultPageSet ) ) {
131 $vals = array();
132 ApiQueryBase::addTitleInfo( $vals, $title );
133
134 if ( isset( $prop['snippet'] ) ) {
135 $vals['snippet'] = $result->getTextSnippet( $terms );
136 }
137 if ( isset( $prop['size'] ) ) {
138 $vals['size'] = $result->getByteSize();
139 }
140 if ( isset( $prop['wordcount'] ) ) {
141 $vals['wordcount'] = $result->getWordCount();
142 }
143 if ( isset( $prop['timestamp'] ) ) {
144 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
145 }
146
147 // Add item to results and see whether it fits
148 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
149 null, $vals );
150 if ( !$fit ) {
151 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
152 break;
153 }
154 } else {
155 $titles[] = $title;
156 }
157 }
158
159 if ( is_null( $resultPageSet ) ) {
160 $this->getResult()->setIndexedTagName_internal( array(
161 'query', $this->getModuleName()
162 ), 'p' );
163 } else {
164 $resultPageSet->populateFromTitles( $titles );
165 }
166 }
167
168 public function getAllowedParams() {
169 return array(
170 'search' => null,
171 'namespace' => array(
172 ApiBase::PARAM_DFLT => 0,
173 ApiBase::PARAM_TYPE => 'namespace',
174 ApiBase::PARAM_ISMULTI => true,
175 ),
176 'what' => array(
177 ApiBase::PARAM_DFLT => null,
178 ApiBase::PARAM_TYPE => array(
179 'title',
180 'text',
181 'nearmatch',
182 )
183 ),
184 'info' => array(
185 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
186 ApiBase::PARAM_TYPE => array(
187 'totalhits',
188 'suggestion',
189 ),
190 ApiBase::PARAM_ISMULTI => true,
191 ),
192 'prop' => array(
193 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
194 ApiBase::PARAM_TYPE => array(
195 'size',
196 'wordcount',
197 'timestamp',
198 'snippet',
199 ),
200 ApiBase::PARAM_ISMULTI => true,
201 ),
202 'redirects' => false,
203 'offset' => 0,
204 'limit' => array(
205 ApiBase::PARAM_DFLT => 10,
206 ApiBase::PARAM_TYPE => 'limit',
207 ApiBase::PARAM_MIN => 1,
208 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
209 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
210 )
211 );
212 }
213
214 public function getParamDescription() {
215 return array(
216 'search' => 'Search for all page titles (or content) that has this value',
217 'namespace' => 'The namespace(s) to enumerate',
218 'what' => 'Search inside the text or titles',
219 'info' => 'What metadata to return',
220 'prop' => array(
221 'What properties to return',
222 ' size - Adds the size of the page in bytes',
223 ' wordcount - Adds the word count of the page',
224 ' timestamp - Adds the timestamp of when the page was last edited',
225 ' snippet - Adds a parsed snippet of the page',
226 ),
227 'redirects' => 'Include redirect pages in the search',
228 'offset' => 'Use this value to continue paging (return by query)',
229 'limit' => 'How many total pages to return'
230 );
231 }
232
233 public function getDescription() {
234 return 'Perform a full text search';
235 }
236
237 public function getPossibleErrors() {
238 return array_merge( parent::getPossibleErrors(), array(
239 array( 'code' => 'param-search', 'info' => 'empty search string is not allowed' ),
240 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
241 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
242 ) );
243 }
244
245 protected function getExamples() {
246 return array(
247 'api.php?action=query&list=search&srsearch=meaning',
248 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
249 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
250 );
251 }
252
253 public function getVersion() {
254 return __CLASS__ . ': $Id$';
255 }
256 }