]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQuerySearch.php
MediaWiki 1.16.0-scripts
[autoinstalls/mediawiki.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 (C) 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                 // Create search engine instance and set options
65                 $search = SearchEngine::create();
66                 $search->setLimitOffset( $limit + 1, $params['offset'] );
67                 $search->setNamespaces( $params['namespace'] );
68                 $search->showRedirects = $params['redirects'];
69
70                 // Perform the actual search
71                 if ( $what == 'text' ) {
72                         $matches = $search->searchText( $query );
73                 } elseif ( $what == 'title' ) {
74                         $matches = $search->searchTitle( $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                 // Add search meta data to result
96                 if ( isset( $searchInfo['totalhits'] ) ) {
97                         $totalhits = $matches->getTotalHits();
98                         if ( $totalhits !== null ) {
99                                 $this->getResult()->addValue( array( 'query', 'searchinfo' ),
100                                                 'totalhits', $totalhits );
101                         }
102                 }
103                 if ( isset( $searchInfo['suggestion'] ) && $matches->hasSuggestion() ) {
104                         $this->getResult()->addValue( array( 'query', 'searchinfo' ),
105                                                 'suggestion', $matches->getSuggestionQuery() );
106                 }
107
108                 // Add the search results to the result
109                 $terms = $wgContLang->convertForSearchResult( $matches->termMatches() );
110                 $titles = array ();
111                 $count = 0;
112                 while ( $result = $matches->next() ) {
113                         if ( ++ $count > $limit ) {
114                                 // We've reached the one extra which shows that there are additional items to be had. Stop here...
115                                 $this->setContinueEnumParameter( 'offset', $params['offset'] + $params['limit'] );
116                                 break;
117                         }
118
119                         // Silently skip broken and missing titles
120                         if ( $result->isBrokenTitle() || $result->isMissingRevision() )
121                                 continue;
122                         
123                         $title = $result->getTitle();
124                         if ( is_null( $resultPageSet ) ) {
125                                 $vals = array();
126                                 ApiQueryBase::addTitleInfo( $vals, $title );
127                                 
128                                 if ( isset( $prop['snippet'] ) )
129                                         $vals['snippet'] = $result->getTextSnippet( $terms );
130                                 if ( isset( $prop['size'] ) )
131                                         $vals['size'] = $result->getByteSize();
132                                 if ( isset( $prop['wordcount'] ) )
133                                         $vals['wordcount'] = $result->getWordCount();
134                                 if ( isset( $prop['timestamp'] ) )
135                                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
136                                 
137                                 // Add item to results and see whether it fits
138                                 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
139                                                 null, $vals );
140                                 if ( !$fit ) {
141                                         $this->setContinueEnumParameter( 'offset', $params['offset'] + $count - 1 );
142                                         break;
143                                 }
144                         } else {
145                                 $titles[] = $title;
146                         }
147                 }
148
149                 if ( is_null( $resultPageSet ) ) {
150                         $this->getResult()->setIndexedTagName_internal( array(
151                                                 'query', $this->getModuleName()
152                                         ), 'p' );
153                 } else {
154                         $resultPageSet->populateFromTitles( $titles );
155                 }
156         }
157
158         public function getCacheMode( $params ) {
159                 return 'public';
160         }
161
162         public function getAllowedParams() {
163                 return array (
164                         'search' => null,
165                         'namespace' => array (
166                                 ApiBase :: PARAM_DFLT => 0,
167                                 ApiBase :: PARAM_TYPE => 'namespace',
168                                 ApiBase :: PARAM_ISMULTI => true,
169                         ),
170                         'what' => array (
171                                 ApiBase :: PARAM_DFLT => null,
172                                 ApiBase :: PARAM_TYPE => array (
173                                         'title',
174                                         'text',
175                                 )
176                         ),
177                         'info' => array(
178                                 ApiBase :: PARAM_DFLT => 'totalhits|suggestion',
179                                 ApiBase :: PARAM_TYPE => array (
180                                         'totalhits',
181                                         'suggestion',
182                                 ),
183                                 ApiBase :: PARAM_ISMULTI => true,
184                         ),
185                         'prop' => array(
186                                 ApiBase :: PARAM_DFLT => 'size|wordcount|timestamp|snippet',
187                                 ApiBase :: PARAM_TYPE => array (
188                                         'size',
189                                         'wordcount',
190                                         'timestamp',
191                                         'snippet',
192                                 ),
193                                 ApiBase :: PARAM_ISMULTI => true,
194                         ),
195                         'redirects' => false,
196                         'offset' => 0,
197                         'limit' => array (
198                                 ApiBase :: PARAM_DFLT => 10,
199                                 ApiBase :: PARAM_TYPE => 'limit',
200                                 ApiBase :: PARAM_MIN => 1,
201                                 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_SML1,
202                                 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
203                         )
204                 );
205         }
206
207         public function getParamDescription() {
208                 return array (
209                         'search' => 'Search for all page titles (or content) that has this value.',
210                         'namespace' => 'The namespace(s) to enumerate.',
211                         'what' => 'Search inside the text or titles.',
212                         'info' => 'What metadata to return.',
213                         'prop' => 'What properties to return.',
214                         'redirects' => 'Include redirect pages in the search.',
215                         'offset' => 'Use this value to continue paging (return by query)',
216                         'limit' => 'How many total pages to return.'
217                 );
218         }
219
220         public function getDescription() {
221                 return 'Perform a full text search';
222         }
223         
224         public function getPossibleErrors() {
225                 return array_merge( parent::getPossibleErrors(), array(
226                         array( 'code' => 'param-search', 'info' => 'empty search string is not allowed' ),
227                         array( 'code' => 'search-text-disabled', 'info' => 'text search is disabled' ),
228                         array( 'code' => 'search-title-disabled', 'info' => 'title search is disabled' ),
229                 ) );
230         }
231
232         protected function getExamples() {
233                 return array (
234                         'api.php?action=query&list=search&srsearch=meaning',
235                         'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
236                         'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
237                 );
238         }
239
240         public function getVersion() {
241                 return __CLASS__ . ': $Id: ApiQuerySearch.php 69932 2010-07-26 08:03:21Z tstarling $';
242         }
243 }