* Added fields to list=search output: size, wordcount, timestamp, snippet
[lhc/web/wiklou.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 $limit = $params['limit'];
55 $query = $params['search'];
56 $what = $params['what'];
57 if (strval($query) === '')
58 $this->dieUsage("empty search string is not allowed", 'param-search');
59
60 $search = SearchEngine::create();
61 $search->setLimitOffset( $limit+1, $params['offset'] );
62 $search->setNamespaces( $params['namespace'] );
63 $search->showRedirects = $params['redirects'];
64
65 if ($what == 'text') {
66 $matches = $search->searchText( $query );
67 } elseif( $what == 'title' ) {
68 $matches = $search->searchTitle( $query );
69 } else {
70 // We default to title searches; this is a terrible legacy
71 // of the way we initially set up the MySQL fulltext-based
72 // search engine with separate title and text fields.
73 // In the future, the default should be for a combined index.
74 $what = 'title';
75 $matches = $search->searchTitle( $query );
76
77 // Not all search engines support a separate title search,
78 // for instance the Lucene-based engine we use on Wikipedia.
79 // In this case, fall back to full-text search (which will
80 // include titles in it!)
81 if( is_null( $matches ) ) {
82 $what = 'text';
83 $matches = $search->searchText( $query );
84 }
85 }
86 if (is_null($matches))
87 $this->dieUsage("{$what} search is disabled",
88 "search-{$what}-disabled");
89
90 $totalhits = $matches->getTotalHits();
91 if( $totalhits !== null ) {
92 $this->getResult()->addValue( array( 'query', 'searchinfo' ), 'totalhits', $totalhits );
93 }
94 if( $matches->hasSuggestion() ) {
95 $this->getResult()->addValue( array( 'query', 'searchinfo' ), 'suggestion',
96 $matches->getSuggestionQuery() );
97 }
98
99 $terms = $wgContLang->convertForSearchResult($matches->termMatches());
100 $titles = array ();
101 $count = 0;
102 while( $result = $matches->next() ) {
103 if (++ $count > $limit) {
104 // We've reached the one extra which shows that there are additional items to be had. Stop here...
105 $this->setContinueEnumParameter('offset', $params['offset'] + $params['limit']);
106 break;
107 }
108
109 // Silently skip broken and missing titles
110 if ($result->isBrokenTitle() || $result->isMissingRevision())
111 continue;
112
113 $title = $result->getTitle();
114 if (is_null($resultPageSet)) {
115 $vals = array();
116 ApiQueryBase::addTitleInfo($vals, $title);
117 $vals['snippet'] = $result->getTextSnippet($terms);
118 $vals['size'] = $result->getByteSize();
119 $vals['wordcount'] = $result->getWordCount();
120 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() );
121 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
122 if(!$fit)
123 {
124 $this->setContinueEnumParameter('offset', $params['offset'] + $count - 1);
125 break;
126 }
127 } else {
128 $titles[] = $title;
129 }
130 }
131
132 if (is_null($resultPageSet)) {
133 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'p');
134 } else {
135 $resultPageSet->populateFromTitles($titles);
136 }
137 }
138
139 public function getAllowedParams() {
140 return array (
141 'search' => null,
142 'namespace' => array (
143 ApiBase :: PARAM_DFLT => 0,
144 ApiBase :: PARAM_TYPE => 'namespace',
145 ApiBase :: PARAM_ISMULTI => true,
146 ),
147 'what' => array (
148 ApiBase :: PARAM_DFLT => null,
149 ApiBase :: PARAM_TYPE => array (
150 'title',
151 'text',
152 )
153 ),
154 'redirects' => false,
155 'offset' => 0,
156 'limit' => array (
157 ApiBase :: PARAM_DFLT => 10,
158 ApiBase :: PARAM_TYPE => 'limit',
159 ApiBase :: PARAM_MIN => 1,
160 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
161 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
162 )
163 );
164 }
165
166 public function getParamDescription() {
167 return array (
168 'search' => 'Search for all page titles (or content) that has this value.',
169 'namespace' => 'The namespace(s) to enumerate.',
170 'what' => 'Search inside the text or titles.',
171 'redirects' => 'Include redirect pages in the search.',
172 'offset' => 'Use this value to continue paging (return by query)',
173 'limit' => 'How many total pages to return.'
174 );
175 }
176
177 public function getDescription() {
178 return 'Perform a full text search';
179 }
180
181 protected function getExamples() {
182 return array (
183 'api.php?action=query&list=search&srsearch=meaning',
184 'api.php?action=query&list=search&srwhat=text&srsearch=meaning',
185 'api.php?action=query&generator=search&gsrsearch=meaning&prop=info',
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }