Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchResultSet.php
blob978db2707f9f73fb242a641d7b7bd055bc92e75a
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 {
29 /**
30 * Types of interwiki results
32 /**
33 * Results that are displayed only together with existing main wiki results
34 * @var int
36 const SECONDARY_RESULTS = 0;
37 /**
38 * Results that can displayed even if no existing main wiki results exist
39 * @var int
41 const INLINE_RESULTS = 1;
43 protected $containedSyntax = false;
45 /**
46 * Cache of titles.
47 * Lists titles of the result set, in the same order as results.
48 * @var Title[]
50 private $titles;
52 /**
53 * Cache of results - serialization of the result iterator
54 * as an array.
55 * @var SearchResult[]
57 private $results;
59 /**
60 * Set of result's extra data, indexed per result id
61 * and then per data item name.
62 * The structure is:
63 * PAGE_ID => [ augmentor name => data, ... ]
64 * @var array[]
66 protected $extraData = [];
68 public function __construct( $containedSyntax = false ) {
69 $this->containedSyntax = $containedSyntax;
72 /**
73 * Fetch an array of regular expression fragments for matching
74 * the search terms as parsed by this engine in a text extract.
75 * STUB
77 * @return array
79 function termMatches() {
80 return [];
83 function numRows() {
84 return 0;
87 /**
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
91 * settings.
93 * Return null if no total hits number is supported.
95 * @return int
97 function getTotalHits() {
98 return null;
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
104 * occured.
106 * @return bool
108 function hasRewrittenQuery() {
109 return false;
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() {
117 return null;
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() {
125 return null;
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.
132 * @return bool
134 function hasSuggestion() {
135 return false;
139 * @return string|null Suggested query, null if none
141 function getSuggestionQuery() {
142 return null;
146 * @return string HTML highlighted suggested query, '' if none
148 function getSuggestionSnippet() {
149 return '';
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 ) {
158 return null;
162 * Check if there are results on other wikis
164 * @return bool
166 function hasInterwikiResults( $type = self::SECONDARY_RESULTS ) {
167 return false;
171 * Fetches next search result, or false.
172 * STUB
173 * FIXME: refactor as iterator, so we could use nicer interfaces.
174 * @return SearchResult|false
176 function next() {
177 return false;
181 * Rewind result set back to beginning
183 function rewind() {
187 * Frees the result set, if applicable.
189 function free() {
190 // ...
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.
197 * @return bool
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 ) ) {
209 $this->results = [];
210 if ( $this->numRows() == 0 ) {
211 // Don't bother if we've got empty result
212 return $this->results;
214 $this->rewind();
215 while ( ( $result = $this->next() ) != false ) {
216 $this->results[] = $result;
218 $this->rewind();
220 return $this->results;
224 * Extract all the titles in the result set.
225 * @return Title[]
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
231 $this->titles = [];
232 } else {
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] ) ) {
262 return null;
264 $result->setExtensionData( $this->extraData[$id] );
265 return $this->extraData[$id];