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
27 class SearchResultSet
{
30 * Types of interwiki results
33 * Results that are displayed only together with existing main wiki results
36 const SECONDARY_RESULTS
= 0;
38 * Results that can displayed even if no existing main wiki results exist
41 const INLINE_RESULTS
= 1;
43 protected $containedSyntax = false;
45 public function __construct( $containedSyntax = false ) {
46 $this->containedSyntax
= $containedSyntax;
50 * Fetch an array of regular expression fragments for matching
51 * the search terms as parsed by this engine in a text extract.
56 function termMatches() {
65 * Some search modes return a total hit count for the query
66 * in the entire article database. This may include pages
67 * in namespaces that would not be matched on the given
70 * Return null if no total hits number is supported.
74 function getTotalHits() {
79 * Some search modes will run an alternative query that it thinks gives
80 * a better result than the provided search. Returns true if this has
85 function hasRewrittenQuery() {
90 * @return string|null The search the query was internally rewritten to,
91 * or null when the result of the original query was returned.
93 function getQueryAfterRewrite() {
98 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
99 * and with changes highlighted. Null when the query was not rewritten.
101 function getQueryAfterRewriteSnippet() {
106 * Some search modes return a suggested alternate term if there are
107 * no exact hits. Returns true if there is one on this set.
111 function hasSuggestion() {
116 * @return string|null Suggested query, null if none
118 function getSuggestionQuery() {
123 * @return string HTML highlighted suggested query, '' if none
125 function getSuggestionSnippet() {
130 * Return a result set of hits on other (multiple) wikis associated with this one
132 * @return SearchResultSet
134 function getInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
139 * Check if there are results on other wikis
143 function hasInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
148 * Fetches next search result, or false.
151 * @return SearchResult
158 * Frees the result set, if applicable.
165 * Did the search contain search syntax? If so, Special:Search won't offer
166 * the user a link to a create a page named by the search string because the
167 * name would contain the search syntax.
170 public function searchContainedSyntax() {
171 return $this->containedSyntax
;
176 * This class is used for different SQL-based search engines shipped with MediaWiki
179 class SqlSearchResultSet
extends SearchResultSet
{
180 protected $resultSet;
182 protected $totalHits;
184 function __construct( $resultSet, $terms, $total = null ) {
185 $this->resultSet
= $resultSet;
186 $this->terms
= $terms;
187 $this->totalHits
= $total;
190 function termMatches() {
195 if ( $this->resultSet
=== false ) {
199 return $this->resultSet
->numRows();
203 if ( $this->resultSet
=== false ) {
207 $row = $this->resultSet
->fetchObject();
208 if ( $row === false ) {
212 return SearchResult
::newFromTitle(
213 Title
::makeTitle( $row->page_namespace
, $row->page_title
)
218 if ( $this->resultSet
=== false ) {
222 $this->resultSet
->free();
225 function getTotalHits() {
226 if ( !is_null( $this->totalHits
) ) {
227 return $this->totalHits
;
229 // Special:Search expects a number here.
230 return $this->numRows();
236 * A SearchResultSet wrapper for SearchEngine::getNearMatch
238 class SearchNearMatchResultSet
extends SearchResultSet
{
239 private $fetched = false;
242 * @param Title|null $match Title if matched, else null
244 public function __construct( $match ) {
245 $this->result
= $match;
248 public function numRows() {
249 return $this->result ?
1 : 0;
252 public function next() {
253 if ( $this->fetched ||
!$this->result
) {
256 $this->fetched
= true;
257 return SearchResult
::newFromTitle( $this->result
);