reimplement r73652 having actually read coding conventions this time
[lhc/web/wiklou.git] / includes / api / ApiQuerySearch.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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 private function run( $resultPageSet = null ) {
52 global $wgContLang;
53 $params = $this->extractRequestParams();
54
55 // Extract parameters
56 $limit = $params['limit'];
57 $query = $params['search'];
58 $what = $params['what'];
59 $searchInfo = array_flip( $params['info'] );
60 $prop = array_flip( $params['prop'] );
61
62 // Create search engine instance and set options
63 $search = SearchEngine::create();
64 $search->setLimitOffset( $limit + 1, $params['offset'] );
65 $search->setNamespaces( $params['namespace'] );
66 $search->showRedirects = $params['redirects'];
67
68 // Perform the actual search
69 if ( $what == 'text' ) {
70 $matches = $search->searchText( $query );
71 } elseif ( $what == 'title' ) {
72 $matches = $search->searchTitle( $query );
73 } elseif ( $what == 'nearmatch' ) {
74 $matches = SearchEngine::getNearMatchResultSet( $query );
75 } else {
76 // We default to title searches; this is a terrible legacy
77 // of the way we initially set up the MySQL fulltext-based
78 // search engine with separate title and text fields.
79 // In the future, the default should be for a combined index.
80 $what = 'title';
81 $matches = $search->searchTitle( $query );
82
83 // Not all search engines support a separate title search,
84 // for instance the Lucene-based engine we use on Wikipedia.
85 // In this case, fall back to full-text search (which will
86 // include titles in it!)
87 if ( is_null( $matches ) ) {
88 $what = 'text';
89 $matches = $search->searchText( $query );
90 }
91 }
92 if ( is_null( $matches ) ) {
93 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
94 }
95
96 // Add search meta data to result
97 if ( isset( $searchInfo['totalhits'] ) ) {
98 $totalhits = $matches->getTotalHits();
99 if ( $totalhits !== null ) {
100 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
101 'totalhits', $totalhits );
102 }
103 }
104 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
105 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
106 'suggestion', $matches->getSuggestionQuery() );
107 }
108
109 // Add the search results to the result
110 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
111 $titles = array();
112 $count = 0;
113 while ( $result = $matches->next() ) {
114 if ( ++ $count > $limit ) {
115 // We've reached the one extra which shows that there are additional items to be had. Stop here...
116 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
117 break;
118 }
119
120 // Silently skip broken and missing titles
121 if ( $result->isBrokenTitle() || $result->isMissingRevision() ) {
122 continue;
123 }
124
125 $title = $result->getTitle();
126 if ( is_null( $resultPageSet ) ) {
127 $vals = array();
128 ApiQueryBase::addTitleInfo( $vals, $title );
129
130 if ( isset( $prop['snippet'] ) ) {
131 $vals['snippet'] = $result->getTextSnippet( $terms );
132 }
133 if ( isset( $prop['size'] ) ) {
134 $vals['size'] = $result->getByteSize();
135 }
136 if ( isset( $prop['wordcount'] ) ) {
137 $vals['wordcount'] = $result->getWordCount();
138 }
139 if ( isset( $prop['timestamp'] ) ) {
140 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
141 }
142
143 // Add item to results and see whether it fits
144 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
145 null, $vals );
146 if ( !$fit ) {
147 $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
148 break;
149 }
150 } else {
151 $titles[] = $title;
152 }
153 }
154
155 if ( is_null( $resultPageSet ) ) {
156 $this->getResult()->setIndexedTagName_internal( array(
157 'query', $this->getModuleName()
158 ), 'p' );
159 } else {
160 $resultPageSet->populateFromTitles( $titles );
161 }
162 }
163
164 public function getCacheMode( $params ) {
165 return 'public';
166 }
167
168 public function getAllowedParams() {
169 return array(
170 'search' => array(
171 ApiBase::PARAM_TYPE => 'string',
172 ApiBase::PARAM_REQUIRED => true
173 ),
174 'namespace' => array(
175 ApiBase::PARAM_DFLT => 0,
176 ApiBase::PARAM_TYPE => 'namespace',
177 ApiBase::PARAM_ISMULTI => true,
178 ),
179 'what' => array(
180 ApiBase::PARAM_DFLT => null,
181 ApiBase::PARAM_TYPE => array(
182 'title',
183 'text',
184 'nearmatch',
185 )
186 ),
187 'info' => array(
188 ApiBase::PARAM_DFLT => 'totalhits|suggestion',
189 ApiBase::PARAM_TYPE => array(
190 'totalhits',
191 'suggestion',
192 ),
193 ApiBase::PARAM_ISMULTI => true,
194 ),
195 'prop' => array(
196 ApiBase::PARAM_DFLT => 'size|wordcount|timestamp|snippet',
197 ApiBase::PARAM_TYPE => array(
198 'size',
199 'wordcount',
200 'timestamp',
201 'snippet',
202 ),
203 ApiBase::PARAM_ISMULTI => true,
204 ),
205 'redirects' => false,
206 'offset' => 0,
207 'limit' => array(
208 ApiBase::PARAM_DFLT => 10,
209 ApiBase::PARAM_TYPE => 'limit',
210 ApiBase::PARAM_MIN => 1,
211 ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
212 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2
213 )
214 );
215 }
216
217 public function getParamDescription() {
218 return array(
219 'search' => 'Search for all page titles (or content) that has this value',
220 'namespace' => 'The namespace(s) to enumerate',
221 'what' => 'Search inside the text or titles',
222 'info' => 'What metadata to return',
223 'prop' => array(
224 'What properties to return',
225 ' size - Adds the size of the page in bytes',
226 ' wordcount - Adds the word count of the page',
227 ' timestamp - Adds the timestamp of when the page was last edited',
228 ' snippet - Adds a parsed snippet of the page',
229 ),
230 'redirects' => 'Include redirect pages in the search',
231 'offset' => 'Use this value to continue paging (return by query)',
232 'limit' => 'How many total pages to return'
233 );
234 }
235
236 public function getDescription() {
237 return 'Perform a full text search';
238 }
239
240 public function getPossibleErrors() {
241 return array_merge( parent::getPossibleErrors(), array(
242 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
243 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
244 ) );
245 }
246
247 protected function getExamples() {
248 return array(
249 'api.php?action=query&list=search&srsearch=meaning',
250 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
251 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
252 );
253 }
254
255 public function getVersion() {
256 return __CLASS__ . ': $Id$';
257 }
258 }