Call Linker methods statically
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to perform full text search within wiki titles and content
34 *
35 * @ingroup API
36 */
37 class ApiQuerySearch extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'sr' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
49 }
50
51 /**
52 * @param $resultPageSet ApiPageSet
53 * @return void
54 */
55 private function run( $resultPageSet = null ) {
56 global $wgContLang;
57 $params = $this->extractRequestParams();
58
59 // Extract parameters
60 $limit = $params['limit'];
61 $query = $params['search'];
62 $what = $params['what'];
63 $searchInfo = array_flip( $params['info'] );
64 $prop = array_flip( $params['prop'] );
65
66 // Create search engine instance and set options
67 $search = SearchEngine::create();
68 $search->setLimitOffset( $limit + 1, $params['offset'] );
69 $search->setNamespaces( $params['namespace'] );
70 $search->showRedirects = $params['redirects'];
71
72 $query = $search->transformSearchTerm( $query );
73 $query = $search->replacePrefixes( $query );
74
75 // Perform the actual search
76 if ( $what == 'text' ) {
77 $matches = $search->searchText( $query );
78 } elseif ( $what == 'title' ) {
79 $matches = $search->searchTitle( $query );
80 } elseif ( $what == 'nearmatch' ) {
81 $matches = SearchEngine::getNearMatchResultSet( $query );
82 } else {
83 // We default to title searches; this is a terrible legacy
84 // of the way we initially set up the MySQL fulltext-based
85 // search engine with separate title and text fields.
86 // In the future, the default should be for a combined index.
87 $what = 'title';
88 $matches = $search->searchTitle( $query );
89
90 // Not all search engines support a separate title search,
91 // for instance the Lucene-based engine we use on Wikipedia.
92 // In this case, fall back to full-text search (which will
93 // include titles in it!)
94 if ( is_null( $matches ) ) {
95 $what = 'text';
96 $matches = $search->searchText( $query );
97 }
98 }
99 if ( is_null( $matches ) ) {
100 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
101 }
102
103 $apiResult = $this->getResult();
104 // Add search meta data to result
105 if ( isset( $searchInfo['totalhits'] ) ) {
106 $totalhits = $matches->getTotalHits();
107 if ( $totalhits !== null ) {
108 $apiResult->addValue( array( 'query', 'searchinfo' ),
109 'totalhits', $totalhits );
110 }
111 }
112 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
113 $apiResult->addValue( array( 'query', 'searchinfo' ),
114 'suggestion', $matches->getSuggestionQuery() );
115 }
116
117 // Add the search results to the result
118 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
119 $titles = array();
120 $count = 0;
121 $result = $matches->next();
122
123 while ( $result ) {
124 if ( ++ $count > $limit ) {
125 // We've reached the one extra which shows that there are additional items to be had. Stop here...
126 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
127 break;
128 }
129
130 // Silently skip broken and missing titles
131 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
132 continue;
133 }
134
135 $title = $result->getTitle();
136 if ( is_null( $resultPageSet ) ) {
137 $vals = array();
138 ApiQueryBase::addTitleInfo( $vals, $title );
139
140 if ( isset( $prop['snippet'] ) ) {
141 $vals['snippet'] = $result->getTextSnippet( $terms );
142 }
143 if ( isset( $prop['size'] ) ) {
144 $vals['size'] = $result->getByteSize();
145 }
146 if ( isset( $prop['wordcount'] ) ) {
147 $vals['wordcount'] = $result->getWordCount();
148 }
149 if ( isset( $prop['timestamp'] ) ) {
150 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
151 }
152 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
153 $vals['score'] = $result->getScore();
154 }
155 if ( isset( $prop['titlesnippet'] ) ) {
156 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
157 }
158 if ( !is_null( $result->getRedirectTitle() ) ) {
159 if ( isset( $prop['redirecttitle'] ) ) {
160 $vals['redirecttitle'] = $result->getRedirectTitle();
161 }
162 if ( isset( $prop['redirectsnippet'] ) ) {
163 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
164 }
165 }
166 if ( !is_null( $result->getSectionTitle() ) ) {
167 if ( isset( $prop['sectiontitle'] ) ) {
168 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
169 }
170 if ( isset( $prop['sectionsnippet'] ) ) {
171 $vals['sectionsnippet'] = $result->getSectionSnippet();
172 }
173 }
174 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
175 $vals['hasrelated'] = "";
176 }
177
178 // Add item to results and see whether it fits
179 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
180 null, $vals );
181 if ( !$fit ) {
182 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
183 break;
184 }
185 } else {
186 $titles[] = $title;
187 }
188
189 $result = $matches->next();
190 }
191
192 if ( is_null( $resultPageSet ) ) {
193 $apiResult->setIndexedTagName_internal( array(
194 'query', $this->getModuleName()
195 ), 'p' );
196 } else {
197 $resultPageSet->populateFromTitles( $titles );
198 }
199 }
200
201 public function getCacheMode( $params ) {
202 return 'public';
203 }
204
205 public function getAllowedParams() {
206 return array(
207 'search' => array(
208 ApiBase::PARAM_TYPE => 'string',
209 ApiBase::PARAM_REQUIRED => true
210 ),
211 'namespace' => array(
212 ApiBase::PARAM_DFLT => 0,
213 ApiBase::PARAM_TYPE => 'namespace',
214 ApiBase::PARAM_ISMULTI => true,
215 ),
216 'what' => array(
217 ApiBase::PARAM_DFLT => null,
218 ApiBase::PARAM_TYPE => array(
219 'title',
220 'text',
221 'nearmatch',
222 )
223 ),
224 'info' => array(
225 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
226 ApiBase::PARAM_TYPE => array(
227 'totalhits',
228 'suggestion',
229 ),
230 ApiBase::PARAM_ISMULTI => true,
231 ),
232 'prop' => array(
233 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
234 ApiBase::PARAM_TYPE => array(
235 'size',
236 'wordcount',
237 'timestamp',
238 'score',
239 'snippet',
240 'titlesnippet',
241 'redirecttitle',
242 'redirectsnippet',
243 'sectiontitle',
244 'sectionsnippet',
245 'hasrelated',
246 ),
247 ApiBase::PARAM_ISMULTI => true,
248 ),
249 'redirects' => false,
250 'offset' => 0,
251 'limit' => array(
252 ApiBase::PARAM_DFLT => 10,
253 ApiBase::PARAM_TYPE => 'limit',
254 ApiBase::PARAM_MIN => 1,
255 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
256 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
257 )
258 );
259 }
260
261 public function getParamDescription() {
262 return array(
263 'search' => 'Search for all page titles (or content) that has this value',
264 'namespace' => 'The namespace(s) to enumerate',
265 'what' => 'Search inside the text or titles',
266 'info' => 'What metadata to return',
267 'prop' => array(
268 'What properties to return',
269 ' size - Adds the size of the page in bytes',
270 ' wordcount - Adds the word count of the page',
271 ' timestamp - Adds the timestamp of when the page was last edited',
272 ' score - Adds the score (if any) from the search engine',
273 ' snippet - Adds a parsed snippet of the page',
274 ' titlesnippet - Adds a parsed snippet of the page title',
275 ' redirectsnippet - Adds a parsed snippet of the redirect title',
276 ' redirecttitle - Adds the title of the matching redirect',
277 ' sectionsnippet - Adds a parsed snippet of the matching section title',
278 ' sectiontitle - Adds the title of the matching section',
279 ' hasrelated - Indicates whether a related search is available',
280 ),
281 'redirects' => 'Include redirect pages in the search',
282 'offset' => 'Use this value to continue paging (return by query)',
283 'limit' => 'How many total pages to return'
284 );
285 }
286
287 public function getDescription() {
288 return 'Perform a full text search';
289 }
290
291 public function getPossibleErrors() {
292 return array_merge( parent::getPossibleErrors(), array(
293 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
294 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
295 ) );
296 }
297
298 public function getExamples() {
299 return array(
300 'api.php?action=query&list=search&srsearch=meaning',
301 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
302 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
303 );
304 }
305
306 public function getHelpUrls() {
307 return 'http://www.mediawiki.org/wiki/API:Search';
308 }
309
310 public function getVersion() {
311 return __CLASS__ . ': $Id$';
312 }
313 }