Merge "SearchResult: clean up construction"
[lhc/web/wiklou.git] / includes / search / SearchResultSet.php
1 <?php
2 /**
3 * Search result sets
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Search
22 */
23
24 /**
25 * @ingroup Search
26 */
27 class SearchResultSet {
28 /**
29 * Fetch an array of regular expression fragments for matching
30 * the search terms as parsed by this engine in a text extract.
31 * STUB
32 *
33 * @return array
34 */
35 function termMatches() {
36 return array();
37 }
38
39 function numRows() {
40 return 0;
41 }
42
43 /**
44 * Some search modes return a total hit count for the query
45 * in the entire article database. This may include pages
46 * in namespaces that would not be matched on the given
47 * settings.
48 *
49 * Return null if no total hits number is supported.
50 *
51 * @return int
52 */
53 function getTotalHits() {
54 return null;
55 }
56
57 /**
58 * Some search modes return a suggested alternate term if there are
59 * no exact hits. Returns true if there is one on this set.
60 *
61 * @return bool
62 */
63 function hasSuggestion() {
64 return false;
65 }
66
67 /**
68 * @return string Suggested query, null if none
69 */
70 function getSuggestionQuery() {
71 return null;
72 }
73
74 /**
75 * @return string HTML highlighted suggested query, '' if none
76 */
77 function getSuggestionSnippet() {
78 return '';
79 }
80
81 /**
82 * Return a result set of hits on other (multiple) wikis associated with this one
83 *
84 * @return SearchResultSet
85 */
86 function getInterwikiResults() {
87 return null;
88 }
89
90 /**
91 * Check if there are results on other wikis
92 *
93 * @return bool
94 */
95 function hasInterwikiResults() {
96 return $this->getInterwikiResults() != null;
97 }
98
99 /**
100 * Fetches next search result, or false.
101 * STUB
102 *
103 * @return SearchResult
104 */
105 function next() {
106 return false;
107 }
108
109 /**
110 * Frees the result set, if applicable.
111 */
112 function free() {
113 // ...
114 }
115
116 /**
117 * Did the search contain search syntax? If so, Special:Search won't offer
118 * the user a link to a create a page named by the search string because the
119 * name would contain the search syntax.
120 * @return bool
121 */
122 public function searchContainedSyntax() {
123 return false;
124 }
125 }
126
127 /**
128 * This class is used for different SQL-based search engines shipped with MediaWiki
129 * @ingroup Search
130 */
131 class SqlSearchResultSet extends SearchResultSet {
132 protected $resultSet;
133 protected $terms;
134 protected $totalHits;
135
136 function __construct( $resultSet, $terms, $total = null ) {
137 $this->resultSet = $resultSet;
138 $this->terms = $terms;
139 $this->totalHits = $total;
140 }
141
142 function termMatches() {
143 return $this->terms;
144 }
145
146 function numRows() {
147 if ( $this->resultSet === false ) {
148 return false;
149 }
150
151 return $this->resultSet->numRows();
152 }
153
154 function next() {
155 if ( $this->resultSet === false ) {
156 return false;
157 }
158
159 $row = $this->resultSet->fetchObject();
160 if ( $row === false ) {
161 return false;
162 }
163
164 return SearchResult::newFromTitle(
165 Title::makeTitle( $row->page_namespace, $row->page_title )
166 );
167 }
168
169 function free() {
170 if ( $this->resultSet === false ) {
171 return false;
172 }
173
174 $this->resultSet->free();
175 }
176
177 function getTotalHits() {
178 if ( !is_null( $this->totalHits ) ) {
179 return $this->totalHits;
180 } else {
181 // Special:Search expects a number here.
182 return $this->numRows();
183 }
184 }
185 }
186
187 /**
188 * A SearchResultSet wrapper for SearchEngine::getNearMatch
189 */
190 class SearchNearMatchResultSet extends SearchResultSet {
191 private $fetched = false;
192
193 /**
194 * @param Title|null $match Title if matched, else null
195 */
196 public function __construct( $match ) {
197 $this->result = $match;
198 }
199
200 public function numRows() {
201 return $this->result ? 1 : 0;
202 }
203
204 public function next() {
205 if ( $this->fetched || !$this->result ) {
206 return false;
207 }
208 $this->fetched = true;
209 return SearchResult::newFromTitle( $this->result );
210 }
211 }