4 * Created on July 30, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
32 * Query module to perform full text search within wiki titles and content
36 class ApiQuerySearch
extends ApiQueryGeneratorBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'sr' );
42 public function execute() {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
50 private function run( $resultPageSet = null ) {
52 $params = $this->extractRequestParams();
55 $limit = $params['limit'];
56 $query = $params['search'];
57 $what = $params['what'];
58 $searchInfo = array_flip( $params['info'] );
59 $prop = array_flip( $params['prop'] );
61 if ( strval( $query ) === '' ) {
62 $this->dieUsage( 'empty search string is not allowed', 'param-search' );
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'];
71 // Perform the actual search
72 if ( $what == 'text' ) {
73 $matches = $search->searchText( $query );
74 } elseif ( $what == 'title' ) {
75 $matches = $search->searchTitle( $query );
76 } elseif ( $what == 'nearmatch' ) {
77 $matches = SearchEngine
::getNearMatchResultSet( $query );
79 // We default to title searches; this is a terrible legacy
80 // of the way we initially set up the MySQL fulltext-based
81 // search engine with separate title and text fields.
82 // In the future, the default should be for a combined index.
84 $matches = $search->searchTitle( $query );
86 // Not all search engines support a separate title search,
87 // for instance the Lucene-based engine we use on Wikipedia.
88 // In this case, fall back to full-text search (which will
89 // include titles in it!)
90 if ( is_null( $matches ) ) {
92 $matches = $search->searchText( $query );
95 if ( is_null( $matches ) ) {
96 $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" );
99 // Add search meta data to result
100 if ( isset( $searchInfo['totalhits'] ) ) {
101 $totalhits = $matches->getTotalHits();
102 if ( $totalhits !== null ) {
103 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
104 'totalhits', $totalhits );
107 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
108 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
109 'suggestion', $matches->getSuggestionQuery() );
112 // Add the search results to the result
113 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
116 while ( $result = $matches->next() ) {
117 if ( ++
$count > $limit ) {
118 // We've reached the one extra which shows that there are additional items to be had. Stop here...
119 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$params['limit'] );
123 // Silently skip broken and missing titles
124 if ( $result->isBrokenTitle() ||
$result->isMissingRevision() ) {
128 $title = $result->getTitle();
129 if ( is_null( $resultPageSet ) ) {
131 ApiQueryBase
::addTitleInfo( $vals, $title );
133 if ( isset( $prop['snippet'] ) ) {
134 $vals['snippet'] = $result->getTextSnippet( $terms );
136 if ( isset( $prop['size'] ) ) {
137 $vals['size'] = $result->getByteSize();
139 if ( isset( $prop['wordcount'] ) ) {
140 $vals['wordcount'] = $result->getWordCount();
142 if ( isset( $prop['timestamp'] ) ) {
143 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $result->getTimestamp() );
146 // Add item to results and see whether it fits
147 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
150 $this->setContinueEnumParameter( 'offset', $params['offset'] +
$count - 1 );
158 if ( is_null( $resultPageSet ) ) {
159 $this->getResult()->setIndexedTagName_internal( array(
160 'query', $this->getModuleName()
163 $resultPageSet->populateFromTitles( $titles );
167 public function getAllowedParams() {
170 'namespace' => array(
171 ApiBase
::PARAM_DFLT
=> 0,
172 ApiBase
::PARAM_TYPE
=> 'namespace',
173 ApiBase
::PARAM_ISMULTI
=> true,
176 ApiBase
::PARAM_DFLT
=> null,
177 ApiBase
::PARAM_TYPE
=> array(
184 ApiBase
::PARAM_DFLT
=> 'totalhits|suggestion',
185 ApiBase
::PARAM_TYPE
=> array(
189 ApiBase
::PARAM_ISMULTI
=> true,
192 ApiBase
::PARAM_DFLT
=> 'size|wordcount|timestamp|snippet',
193 ApiBase
::PARAM_TYPE
=> array(
199 ApiBase
::PARAM_ISMULTI
=> true,
201 'redirects' => false,
204 ApiBase
::PARAM_DFLT
=> 10,
205 ApiBase
::PARAM_TYPE
=> 'limit',
206 ApiBase
::PARAM_MIN
=> 1,
207 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_SML1
,
208 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_SML2
213 public function getParamDescription() {
215 'search' => 'Search for all page titles (or content) that has this value',
216 'namespace' => 'The namespace(s) to enumerate',
217 'what' => 'Search inside the text or titles',
218 'info' => 'What metadata to return',
220 'What properties to return',
221 ' size - Adds the size of the page in bytes',
222 ' wordcount - Adds the word count of the page',
223 ' timestamp - Adds the timestamp of when the page was last edited',
224 ' snippet - Adds a parsed snippet of the page',
226 'redirects' => 'Include redirect pages in the search',
227 'offset' => 'Use this value to continue paging (return by query)',
228 'limit' => 'How many total pages to return'
232 public function getDescription() {
233 return 'Perform a full text search';
236 public function getPossibleErrors() {
237 return array_merge( parent
::getPossibleErrors(), array(
238 array( 'code' => 'param-search', 'info' => 'empty search string is not allowed' ),
239 array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
240 array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
244 protected function getExamples() {
246 'api.php?action=query&list=search&srsearch=meaning',
247 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
248 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
252 public function getVersion() {
253 return __CLASS__
. ': $Id$';