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 // Perform the actual search
73 if ( $what == 'text' ) {
74 $matches = $search->searchText( $query );
75 } elseif ( $what == 'title' ) {
76 $matches = $search->searchTitle( $query );
77 } elseif ( $what == 'nearmatch' ) {
78 $matches = SearchEngine
::getNearMatchResultSet( $query );
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.
85 $matches = $search->searchTitle( $query );
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 ) ) {
93 $matches = $search->searchText( $query );
96 if ( is_null( $matches ) ) {
97 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
100 $apiResult = $this->getResult();
101 // Add search meta data to result
102 if ( isset( $searchInfo['totalhits'] ) ) {
103 $totalhits = $matches->getTotalHits();
104 if ( $totalhits !== null ) {
105 $apiResult->addValue( array( 'query', 'searchinfo' ),
106 'totalhits', $totalhits );
109 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
110 $apiResult->addValue( array( 'query', 'searchinfo' ),
111 'suggestion', $matches->getSuggestionQuery() );
114 // Add the search results to the result
115 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
118 $result = $matches->next();
121 if ( ++
$count > $limit ) {
122 // We've reached the one extra which shows that there are additional items to be had. Stop here...
123 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
127 // Silently skip broken and missing titles
128 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
132 $title = $result->getTitle();
133 if ( is_null( $resultPageSet ) ) {
135 ApiQueryBase
::addTitleInfo( $vals, $title );
137 if ( isset( $prop['snippet'] ) ) {
138 $vals['snippet'] = $result->getTextSnippet( $terms );
140 if ( isset( $prop['size'] ) ) {
141 $vals['size'] = $result->getByteSize();
143 if ( isset( $prop['wordcount'] ) ) {
144 $vals['wordcount'] = $result->getWordCount();
146 if ( isset( $prop['timestamp'] ) ) {
147 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $result->getTimestamp() );
149 if ( !is_null( $result->getScore() ) && isset( $prop['score'] ) ) {
150 $vals['score'] = $result->getScore();
152 if ( isset( $prop['titlesnippet'] ) ) {
153 $vals['titlesnippet'] = $result->getTitleSnippet( $terms );
155 if ( !is_null( $result->getRedirectTitle() ) ) {
156 if ( isset( $prop['redirecttitle'] ) ) {
157 $vals['redirecttitle'] = $result->getRedirectTitle();
159 if ( isset( $prop['redirectsnippet'] ) ) {
160 $vals['redirectsnippet'] = $result->getRedirectSnippet( $terms );
163 if ( !is_null( $result->getSectionTitle() ) ) {
164 if ( isset( $prop['sectiontitle'] ) ) {
165 $vals['sectiontitle'] = $result->getSectionTitle()->getFragment();
167 if ( isset( $prop['sectionsnippet'] ) ) {
168 $vals['sectionsnippet'] = $result->getSectionSnippet();
171 if ( isset( $prop['hasrelated'] ) && $result->hasRelated() ) {
172 $vals['hasrelated'] = "";
175 // Add item to results and see whether it fits
176 $fit = $apiResult->addValue( array( 'query', $this->getModuleName() ),
179 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
186 $result = $matches->next();
189 if ( is_null( $resultPageSet ) ) {
190 $apiResult->setIndexedTagName_internal( array(
191 'query', $this->getModuleName()
194 $resultPageSet->populateFromTitles( $titles );
198 public function getCacheMode( $params ) {
202 public function getAllowedParams() {
205 ApiBase
::PARAM_TYPE
=> 'string',
206 ApiBase
::PARAM_REQUIRED
=> true
208 'namespace' => array(
209 ApiBase
::PARAM_DFLT
=> 0,
210 ApiBase
::PARAM_TYPE
=> 'namespace',
211 ApiBase
::PARAM_ISMULTI
=> true,
214 ApiBase
::PARAM_DFLT
=> null,
215 ApiBase
::PARAM_TYPE
=> array(
222 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
223 ApiBase
::PARAM_TYPE
=> array(
227 ApiBase
::PARAM_ISMULTI
=> true,
230 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
231 ApiBase
::PARAM_TYPE
=> array(
244 ApiBase
::PARAM_ISMULTI
=> true,
246 'redirects' => false,
249 ApiBase
::PARAM_DFLT
=> 10,
250 ApiBase
::PARAM_TYPE
=> 'limit',
251 ApiBase
::PARAM_MIN
=> 1,
252 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_SML1
,
253 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_SML2
258 public function getParamDescription() {
260 'search' => 'Search for all page titles (or content) that has this value',
261 'namespace' => 'The namespace(s) to enumerate',
262 'what' => 'Search inside the text or titles',
263 'info' => 'What metadata to return',
265 'What properties to return',
266 ' size - Adds the size of the page in bytes',
267 ' wordcount - Adds the word count of the page',
268 ' timestamp - Adds the timestamp of when the page was last edited',
269 ' score - Adds the score (if any) from the search engine',
270 ' snippet - Adds a parsed snippet of the page',
271 ' titlesnippet - Adds a parsed snippet of the page title',
272 ' redirectsnippet - Adds a parsed snippet of the redirect',
273 ' redirecttitle - Adds a parsed snippet of the redirect title',
274 ' sectionsnippet - Adds a parsed snippet of the matching section',
275 ' sectiontitle - Adds a parsed snippet of the matching section title',
276 ' hasrelated - Indicates whether a related search is available',
278 'redirects' => 'Include redirect pages in the search',
279 'offset' => 'Use this value to continue paging (return by query)',
280 'limit' => 'How many total pages to return'
284 public function getDescription() {
285 return 'Perform a full text search';
288 public function getPossibleErrors() {
289 return array_merge( parent
::getPossibleErrors(), array(
290 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
291 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
295 protected function getExamples() {
297 'api.php?action=query&list=search&srsearch=meaning',
298 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
299 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
303 public function getVersion() {
304 return __CLASS__
. ': $Id$';