Merge "Improve the wording of apihelp-parse-param-section"
[mediawiki.git] / includes / search / SearchResultSet.php
blob8d18b0e6ed598841fd843838691bafb53ef25dba
1 <?php
2 /**
3 * Search result sets
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Search
24 /**
25 * @ingroup Search
27 class SearchResultSet {
28 protected $containedSyntax = false;
30 public function __construct( $containedSyntax = false ) {
31 $this->containedSyntax = $containedSyntax;
34 /**
35 * Fetch an array of regular expression fragments for matching
36 * the search terms as parsed by this engine in a text extract.
37 * STUB
39 * @return array
41 function termMatches() {
42 return array();
45 function numRows() {
46 return 0;
49 /**
50 * Some search modes return a total hit count for the query
51 * in the entire article database. This may include pages
52 * in namespaces that would not be matched on the given
53 * settings.
55 * Return null if no total hits number is supported.
57 * @return int
59 function getTotalHits() {
60 return null;
63 /**
64 * Some search modes will run an alternative query that it thinks gives
65 * a better result than the provided search. Returns true if this has
66 * occured.
68 * @return bool
70 function hasRewrittenQuery() {
71 return false;
74 /**
75 * @return string|null The search the query was internally rewritten to,
76 * or null when the result of the original query was returned.
78 function getQueryAfterRewrite() {
79 return null;
82 /**
83 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
84 * and with changes highlighted. Null when the query was not rewritten.
86 function getQueryAfterRewriteSnippet() {
87 return null;
90 /**
91 * Some search modes return a suggested alternate term if there are
92 * no exact hits. Returns true if there is one on this set.
94 * @return bool
96 function hasSuggestion() {
97 return false;
101 * @return string Suggested query, null if none
103 function getSuggestionQuery() {
104 return null;
108 * @return string HTML highlighted suggested query, '' if none
110 function getSuggestionSnippet() {
111 return '';
115 * Return a result set of hits on other (multiple) wikis associated with this one
117 * @return SearchResultSet
119 function getInterwikiResults() {
120 return null;
124 * Check if there are results on other wikis
126 * @return bool
128 function hasInterwikiResults() {
129 return $this->getInterwikiResults() != null;
133 * Fetches next search result, or false.
134 * STUB
136 * @return SearchResult
138 function next() {
139 return false;
143 * Frees the result set, if applicable.
145 function free() {
146 // ...
150 * Did the search contain search syntax? If so, Special:Search won't offer
151 * the user a link to a create a page named by the search string because the
152 * name would contain the search syntax.
153 * @return bool
155 public function searchContainedSyntax() {
156 return $this->containedSyntax;
161 * This class is used for different SQL-based search engines shipped with MediaWiki
162 * @ingroup Search
164 class SqlSearchResultSet extends SearchResultSet {
165 protected $resultSet;
166 protected $terms;
167 protected $totalHits;
169 function __construct( $resultSet, $terms, $total = null ) {
170 $this->resultSet = $resultSet;
171 $this->terms = $terms;
172 $this->totalHits = $total;
175 function termMatches() {
176 return $this->terms;
179 function numRows() {
180 if ( $this->resultSet === false ) {
181 return false;
184 return $this->resultSet->numRows();
187 function next() {
188 if ( $this->resultSet === false ) {
189 return false;
192 $row = $this->resultSet->fetchObject();
193 if ( $row === false ) {
194 return false;
197 return SearchResult::newFromTitle(
198 Title::makeTitle( $row->page_namespace, $row->page_title )
202 function free() {
203 if ( $this->resultSet === false ) {
204 return false;
207 $this->resultSet->free();
210 function getTotalHits() {
211 if ( !is_null( $this->totalHits ) ) {
212 return $this->totalHits;
213 } else {
214 // Special:Search expects a number here.
215 return $this->numRows();
221 * A SearchResultSet wrapper for SearchEngine::getNearMatch
223 class SearchNearMatchResultSet extends SearchResultSet {
224 private $fetched = false;
227 * @param Title|null $match Title if matched, else null
229 public function __construct( $match ) {
230 $this->result = $match;
233 public function numRows() {
234 return $this->result ? 1 : 0;
237 public function next() {
238 if ( $this->fetched || !$this->result ) {
239 return false;
241 $this->fetched = true;
242 return SearchResult::newFromTitle( $this->result );