Merge "Don't let LinkCache grow indefinitely"
[mediawiki.git] / includes / search / SearchResultSet.php
blob0a05eef1594eb0e343b82613bfe5edb0b7be1095
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 return a suggested alternate term if there are
65 * no exact hits. Returns true if there is one on this set.
67 * @return bool
69 function hasSuggestion() {
70 return false;
73 /**
74 * @return string Suggested query, null if none
76 function getSuggestionQuery() {
77 return null;
80 /**
81 * @return string HTML highlighted suggested query, '' if none
83 function getSuggestionSnippet() {
84 return '';
87 /**
88 * Return a result set of hits on other (multiple) wikis associated with this one
90 * @return SearchResultSet
92 function getInterwikiResults() {
93 return null;
96 /**
97 * Check if there are results on other wikis
99 * @return bool
101 function hasInterwikiResults() {
102 return $this->getInterwikiResults() != null;
106 * Fetches next search result, or false.
107 * STUB
109 * @return SearchResult
111 function next() {
112 return false;
116 * Frees the result set, if applicable.
118 function free() {
119 // ...
123 * Did the search contain search syntax? If so, Special:Search won't offer
124 * the user a link to a create a page named by the search string because the
125 * name would contain the search syntax.
126 * @return bool
128 public function searchContainedSyntax() {
129 return $this->containedSyntax;
134 * This class is used for different SQL-based search engines shipped with MediaWiki
135 * @ingroup Search
137 class SqlSearchResultSet extends SearchResultSet {
138 protected $resultSet;
139 protected $terms;
140 protected $totalHits;
142 function __construct( $resultSet, $terms, $total = null ) {
143 $this->resultSet = $resultSet;
144 $this->terms = $terms;
145 $this->totalHits = $total;
148 function termMatches() {
149 return $this->terms;
152 function numRows() {
153 if ( $this->resultSet === false ) {
154 return false;
157 return $this->resultSet->numRows();
160 function next() {
161 if ( $this->resultSet === false ) {
162 return false;
165 $row = $this->resultSet->fetchObject();
166 if ( $row === false ) {
167 return false;
170 return SearchResult::newFromTitle(
171 Title::makeTitle( $row->page_namespace, $row->page_title )
175 function free() {
176 if ( $this->resultSet === false ) {
177 return false;
180 $this->resultSet->free();
183 function getTotalHits() {
184 if ( !is_null( $this->totalHits ) ) {
185 return $this->totalHits;
186 } else {
187 // Special:Search expects a number here.
188 return $this->numRows();
194 * A SearchResultSet wrapper for SearchEngine::getNearMatch
196 class SearchNearMatchResultSet extends SearchResultSet {
197 private $fetched = false;
200 * @param Title|null $match Title if matched, else null
202 public function __construct( $match ) {
203 $this->result = $match;
206 public function numRows() {
207 return $this->result ? 1 : 0;
210 public function next() {
211 if ( $this->fetched || !$this->result ) {
212 return false;
214 $this->fetched = true;
215 return SearchResult::newFromTitle( $this->result );