5 * Created on July 30, 2007
7 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to perform full text search within wiki titles and content
37 class ApiQuerySearch
extends ApiQueryGeneratorBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'sr' );
43 public function execute() {
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
52 * @param $resultPageSet ApiPageSet
55 private function run( $resultPageSet = null ) {
57 $params = $this->extractRequestParams();
60 $limit = $params['limit'];
61 $query = $params['search'];
62 $what = $params['what'];
63 $searchInfo = array_flip( $params['info'] );
64 $prop = array_flip( $params['prop'] );
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'];
72 $query = $search->transformSearchTerm( $query );
73 $query = $search->replacePrefixes( $query );
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 );
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.
88 $matches = $search->searchTitle( $query );
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 ) ) {
96 $matches = $search->searchText( $query );
99 if ( is_null( $matches ) ) {
100 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
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 );
112 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
113 $apiResult->addValue( array( 'query', 'searchinfo' ),
114 'suggestion', $matches->getSuggestionQuery() );
117 // Add the search results to the result
118 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
121 $result = $matches->next();
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'] );
130 // Silently skip broken and missing titles
131 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
135 $title = $result->getTitle();
136 if ( is_null( $resultPageSet ) ) {
138 ApiQueryBase
::addTitleInfo( $vals, $title );
140 if ( isset( $prop['snippet'] ) ) {
141 $vals['snippet'] = $result->getTextSnippet( $terms );
143 if ( isset( $prop['size'] ) ) {
144 $vals['size'] = $result->getByteSize();
146 if ( isset( $prop['wordcount'] ) ) {
147 $vals['wordcount'] = $result->getWordCount();
149 if ( isset( $prop['timestamp'] ) ) {
150 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $result->getTimestamp() );
152 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
153 $vals['score'] = $result->getScore();
155 if ( isset( $prop['titlesnippet'] ) ) {
156 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
158 if ( !is_null( $result->getRedirectTitle() ) ) {
159 if ( isset( $prop['redirecttitle'] ) ) {
160 $vals['redirecttitle'] = $result->getRedirectTitle();
162 if ( isset( $prop['redirectsnippet'] ) ) {
163 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
166 if ( !is_null( $result->getSectionTitle() ) ) {
167 if ( isset( $prop['sectiontitle'] ) ) {
168 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
170 if ( isset( $prop['sectionsnippet'] ) ) {
171 $vals['sectionsnippet'] = $result->getSectionSnippet();
174 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
175 $vals['hasrelated'] = "";
178 // Add item to results and see whether it fits
179 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
182 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
189 $result = $matches->next();
192 if ( is_null( $resultPageSet ) ) {
193 $apiResult->setIndexedTagName_internal( array(
194 'query', $this->getModuleName()
197 $resultPageSet->populateFromTitles( $titles );
201 public function getCacheMode( $params ) {
205 public function getAllowedParams() {
208 ApiBase
::PARAM_TYPE
=> 'string',
209 ApiBase
::PARAM_REQUIRED
=> true
211 'namespace' => array(
212 ApiBase
::PARAM_DFLT
=> 0,
213 ApiBase
::PARAM_TYPE
=> 'namespace',
214 ApiBase
::PARAM_ISMULTI
=> true,
217 ApiBase
::PARAM_DFLT
=> null,
218 ApiBase
::PARAM_TYPE
=> array(
225 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
226 ApiBase
::PARAM_TYPE
=> array(
230 ApiBase
::PARAM_ISMULTI
=> true,
233 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
234 ApiBase
::PARAM_TYPE
=> array(
247 ApiBase
::PARAM_ISMULTI
=> true,
249 'redirects' => false,
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
261 public function getParamDescription() {
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',
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',
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'
287 public function getDescription() {
288 return 'Perform a full text search';
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' ),
298 public function getExamples() {
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',
306 public function getHelpUrls() {
307 return 'http://www.mediawiki.org/wiki/API:Search';
310 public function getVersion() {
311 return __CLASS__
. ': $Id$';