Merge "Remove unused 'XMPGetInfo' and 'XMPGetResults' hooks"
[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 protected $containedSyntax = false;
29
30 public function __construct( $containedSyntax = false ) {
31 $this->containedSyntax = $containedSyntax;
32 }
33
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
38 *
39 * @return array
40 */
41 function termMatches() {
42 return array();
43 }
44
45 function numRows() {
46 return 0;
47 }
48
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.
54 *
55 * Return null if no total hits number is supported.
56 *
57 * @return int
58 */
59 function getTotalHits() {
60 return null;
61 }
62
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.
66 *
67 * @return bool
68 */
69 function hasSuggestion() {
70 return false;
71 }
72
73 /**
74 * @return string Suggested query, null if none
75 */
76 function getSuggestionQuery() {
77 return null;
78 }
79
80 /**
81 * @return string HTML highlighted suggested query, '' if none
82 */
83 function getSuggestionSnippet() {
84 return '';
85 }
86
87 /**
88 * Return a result set of hits on other (multiple) wikis associated with this one
89 *
90 * @return SearchResultSet
91 */
92 function getInterwikiResults() {
93 return null;
94 }
95
96 /**
97 * Check if there are results on other wikis
98 *
99 * @return bool
100 */
101 function hasInterwikiResults() {
102 return $this->getInterwikiResults() != null;
103 }
104
105 /**
106 * Fetches next search result, or false.
107 * STUB
108 *
109 * @return SearchResult
110 */
111 function next() {
112 return false;
113 }
114
115 /**
116 * Frees the result set, if applicable.
117 */
118 function free() {
119 // ...
120 }
121
122 /**
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
127 */
128 public function searchContainedSyntax() {
129 return $this->containedSyntax;
130 }
131 }
132
133 /**
134 * This class is used for different SQL-based search engines shipped with MediaWiki
135 * @ingroup Search
136 */
137 class SqlSearchResultSet extends SearchResultSet {
138 protected $resultSet;
139 protected $terms;
140 protected $totalHits;
141
142 function __construct( $resultSet, $terms, $total = null ) {
143 $this->resultSet = $resultSet;
144 $this->terms = $terms;
145 $this->totalHits = $total;
146 }
147
148 function termMatches() {
149 return $this->terms;
150 }
151
152 function numRows() {
153 if ( $this->resultSet === false ) {
154 return false;
155 }
156
157 return $this->resultSet->numRows();
158 }
159
160 function next() {
161 if ( $this->resultSet === false ) {
162 return false;
163 }
164
165 $row = $this->resultSet->fetchObject();
166 if ( $row === false ) {
167 return false;
168 }
169
170 return SearchResult::newFromTitle(
171 Title::makeTitle( $row->page_namespace, $row->page_title )
172 );
173 }
174
175 function free() {
176 if ( $this->resultSet === false ) {
177 return false;
178 }
179
180 $this->resultSet->free();
181 }
182
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();
189 }
190 }
191 }
192
193 /**
194 * A SearchResultSet wrapper for SearchEngine::getNearMatch
195 */
196 class SearchNearMatchResultSet extends SearchResultSet {
197 private $fetched = false;
198
199 /**
200 * @param Title|null $match Title if matched, else null
201 */
202 public function __construct( $match ) {
203 $this->result = $match;
204 }
205
206 public function numRows() {
207 return $this->result ? 1 : 0;
208 }
209
210 public function next() {
211 if ( $this->fetched || !$this->result ) {
212 return false;
213 }
214 $this->fetched = true;
215 return SearchResult::newFromTitle( $this->result );
216 }
217 }