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;
47 * Lists titles of the result set, in the same order as results.
53 * Cache of results - serialization of the result iterator
60 * Set of result's extra data, indexed per result id
61 * and then per data item name.
63 * PAGE_ID => [ augmentor name => data, ... ]
66 protected $extraData = [];
68 public function __construct( $containedSyntax = false ) {
69 $this->containedSyntax
= $containedSyntax;
73 * Fetch an array of regular expression fragments for matching
74 * the search terms as parsed by this engine in a text extract.
79 function termMatches() {
88 * Some search modes return a total hit count for the query
89 * in the entire article database. This may include pages
90 * in namespaces that would not be matched on the given
93 * Return null if no total hits number is supported.
97 function getTotalHits() {
102 * Some search modes will run an alternative query that it thinks gives
103 * a better result than the provided search. Returns true if this has
108 function hasRewrittenQuery() {
113 * @return string|null The search the query was internally rewritten to,
114 * or null when the result of the original query was returned.
116 function getQueryAfterRewrite() {
121 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
122 * and with changes highlighted. Null when the query was not rewritten.
124 function getQueryAfterRewriteSnippet() {
129 * Some search modes return a suggested alternate term if there are
130 * no exact hits. Returns true if there is one on this set.
134 function hasSuggestion() {
139 * @return string|null Suggested query, null if none
141 function getSuggestionQuery() {
146 * @return string HTML highlighted suggested query, '' if none
148 function getSuggestionSnippet() {
153 * Return a result set of hits on other (multiple) wikis associated with this one
155 * @return SearchResultSet[]
157 function getInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
162 * Check if there are results on other wikis
166 function hasInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
171 * Fetches next search result, or false.
173 * FIXME: refactor as iterator, so we could use nicer interfaces.
174 * @return SearchResult|false
181 * Rewind result set back to beginning
187 * Frees the result set, if applicable.
194 * Did the search contain search syntax? If so, Special:Search won't offer
195 * the user a link to a create a page named by the search string because the
196 * name would contain the search syntax.
199 public function searchContainedSyntax() {
200 return $this->containedSyntax
;
204 * Extract all the results in the result set as array.
205 * @return SearchResult[]
207 public function extractResults() {
208 if ( is_null( $this->results
) ) {
210 if ( $this->numRows() == 0 ) {
211 // Don't bother if we've got empty result
212 return $this->results
;
215 while ( ( $result = $this->next() ) != false ) {
216 $this->results
[] = $result;
220 return $this->results
;
224 * Extract all the titles in the result set.
227 public function extractTitles() {
228 if ( is_null( $this->titles
) ) {
229 if ( $this->numRows() == 0 ) {
230 // Don't bother if we've got empty result
233 $this->titles
= array_map(
234 function ( SearchResult
$result ) {
235 return $result->getTitle();
237 $this->extractResults() );
240 return $this->titles
;
244 * Sets augmented data for result set.
245 * @param string $name Extra data item name
246 * @param array[] $data Extra data as PAGEID => data
248 public function setAugmentedData( $name, $data ) {
249 foreach ( $data as $id => $resultData ) {
250 $this->extraData
[$id][$name] = $resultData;
255 * Returns extra data for specific result and store it in SearchResult object.
256 * @param SearchResult $result
257 * @return array|null List of data as name => value or null if none present.
259 public function augmentResult( SearchResult
$result ) {
260 $id = $result->getTitle()->getArticleID();
261 if ( !$id ||
!isset( $this->extraData
[$id] ) ) {
264 $result->setExtensionData( $this->extraData
[$id] );
265 return $this->extraData
[$id];