Localisation updates for core and extension messages from translatewiki.net (2010...
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 } else {
77 // We default to title searches; this is a terrible legacy
78 // of the way we initially set up the MySQL fulltext-based
79 // search engine with separate title and text fields.
80 // In the future, the default should be for a combined index.
81 $what = 'title';
82 $matches = $search->searchTitle( $query );
83
84 // Not all search engines support a separate title search,
85 // for instance the Lucene-based engine we use on Wikipedia.
86 // In this case, fall back to full-text search (which will
87 // include titles in it!)
88 if ( is_null( $matches ) ) {
89 $what = 'text';
90 $matches = $search->searchText( $query );
91 }
92 }
93 if ( is_null( $matches ) ) {
94 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
95 }
96
97 // Add search meta data to result
98 if ( isset( $searchInfo['totalhits'] ) ) {
99 $totalhits = $matches->getTotalHits();
100 if ( $totalhits !== null ) {
101 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
102 'totalhits', $totalhits );
103 }
104 }
105 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
106 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
107 'suggestion', $matches->getSuggestionQuery() );
108 }
109
110 // Add the search results to the result
111 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
112 $titles = array();
113 $count = 0;
114 while ( $result = $matches->next() ) {
115 if ( ++ $count > $limit ) {
116 // We've reached the one extra which shows that there are additional items to be had. Stop here...
117 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
118 break;
119 }
120
121 // Silently skip broken and missing titles
122 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
123 continue;
124 }
125
126 $title = $result->getTitle();
127 if ( is_null( $resultPageSet ) ) {
128 $vals = array();
129 ApiQueryBase::addTitleInfo( $vals, $title );
130
131 if ( isset( $prop['snippet'] ) ) {
132 $vals['snippet'] = $result->getTextSnippet( $terms );
133 }
134 if ( isset( $prop['size'] ) ) {
135 $vals['size'] = $result->getByteSize();
136 }
137 if ( isset( $prop['wordcount'] ) ) {
138 $vals['wordcount'] = $result->getWordCount();
139 }
140 if ( isset( $prop['timestamp'] ) ) {
141 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
142 }
143
144 // Add item to results and see whether it fits
145 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
146 null, $vals );
147 if ( !$fit ) {
148 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
149 break;
150 }
151 } else {
152 $titles[] = $title;
153 }
154 }
155
156 if ( is_null( $resultPageSet ) ) {
157 $this->getResult()->setIndexedTagName_internal( array(
158 'query', $this->getModuleName()
159 ), 'p' );
160 } else {
161 $resultPageSet->populateFromTitles( $titles );
162 }
163 }
164
165 public function getAllowedParams() {
166 return array(
167 'search' => null,
168 'namespace' => array(
169 ApiBase::PARAM_DFLT => 0,
170 ApiBase::PARAM_TYPE => 'namespace',
171 ApiBase::PARAM_ISMULTI => true,
172 ),
173 'what' => array(
174 ApiBase::PARAM_DFLT => null,
175 ApiBase::PARAM_TYPE => array(
176 'title',
177 'text',
178 )
179 ),
180 'info' => array(
181 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
182 ApiBase::PARAM_TYPE => array(
183 'totalhits',
184 'suggestion',
185 ),
186 ApiBase::PARAM_ISMULTI => true,
187 ),
188 'prop' => array(
189 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
190 ApiBase::PARAM_TYPE => array(
191 'size',
192 'wordcount',
193 'timestamp',
194 'snippet',
195 ),
196 ApiBase::PARAM_ISMULTI => true,
197 ),
198 'redirects' => false,
199 'offset' => 0,
200 'limit' => array(
201 ApiBase::PARAM_DFLT => 10,
202 ApiBase::PARAM_TYPE => 'limit',
203 ApiBase::PARAM_MIN => 1,
204 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
205 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
206 )
207 );
208 }
209
210 public function getParamDescription() {
211 return array(
212 'search' => 'Search for all page titles (or content) that has this value.',
213 'namespace' => 'The namespace(s) to enumerate.',
214 'what' => 'Search inside the text or titles.',
215 'info' => 'What metadata to return.',
216 'prop' => 'What properties to return.',
217 'redirects' => 'Include redirect pages in the search.',
218 'offset' => 'Use this value to continue paging (return by query)',
219 'limit' => 'How many total pages to return.'
220 );
221 }
222
223 public function getDescription() {
224 return 'Perform a full text search';
225 }
226
227 public function getPossibleErrors() {
228 return array_merge( parent::getPossibleErrors(), array(
229 array( 'code' => 'param-search', 'info' => 'empty search string is not allowed' ),
230 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
231 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
232 ) );
233 }
234
235 protected function getExamples() {
236 return array(
237 'api.php?action=query&list=search&srsearch=meaning',
238 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
239 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
240 );
241 }
242
243 public function getVersion() {
244 return __CLASS__ . ': $Id$';
245 }
246 }