Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / search / SearchResultSet.php
blob7f28e3aee3b23c6416b56bf74ae81aaacb8350b1
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 use MediaWiki\Title\Title;
26 /**
27 * @ingroup Search
29 class SearchResultSet extends BaseSearchResultSet {
31 use SearchResultSetTrait;
33 /** @var bool */
34 protected $containedSyntax = false;
36 /**
37 * Cache of titles.
38 * Lists titles of the result set, in the same order as results.
39 * @var Title[]|null
41 private $titles;
43 /**
44 * Cache of results - serialization of the result iterator
45 * as an array.
46 * @var SearchResult[]
48 protected $results;
50 /**
51 * @var bool True when there are more pages of search results available.
53 private $hasMoreResults;
55 /**
56 * @param bool $containedSyntax True when query is not requesting a simple
57 * term match
58 * @param bool $hasMoreResults True when there are more pages of search
59 * results available.
61 public function __construct( $containedSyntax = false, $hasMoreResults = false ) {
62 if ( static::class === self::class ) {
63 // This class will eventually be abstract. SearchEngine implementations
64 // already have to extend this class anyways to provide the actual
65 // search results.
66 wfDeprecated( __METHOD__, '1.32' );
68 $this->containedSyntax = $containedSyntax;
69 $this->hasMoreResults = $hasMoreResults;
72 public function numRows() {
73 return $this->count();
76 final public function count(): int {
77 return count( $this->extractResults() );
80 /**
81 * Some search modes return a total hit count for the query
82 * in the entire article database. This may include pages
83 * in namespaces that would not be matched on the given
84 * settings.
86 * Return null if no total hits number is supported.
88 * @return int|null
90 public function getTotalHits() {
91 return null;
94 /**
95 * Some search modes will run an alternative query that it thinks gives
96 * a better result than the provided search. Returns true if this has
97 * occurred.
99 * @return bool
101 public function hasRewrittenQuery() {
102 return false;
106 * @return string|null The search the query was internally rewritten to,
107 * or null when the result of the original query was returned.
109 public function getQueryAfterRewrite() {
110 return null;
114 * @return HtmlArmor|string|null Same as self::getQueryAfterRewrite(), but
115 * with changes highlighted if HtmlArmor is returned. Null when the query
116 * was not rewritten.
118 public function getQueryAfterRewriteSnippet() {
119 return null;
123 * Some search modes return a suggested alternate term if there are
124 * no exact hits. Returns true if there is one on this set.
126 * @return bool
128 public function hasSuggestion() {
129 return false;
133 * @return string|null Suggested query, null if none
135 public function getSuggestionQuery() {
136 return null;
140 * @return HtmlArmor|string HTML highlighted suggested query, '' if none
142 public function getSuggestionSnippet() {
143 return '';
147 * Return a result set of hits on other (multiple) wikis associated with this one
149 * @param int $type
150 * @return ISearchResultSet[]|null
152 public function getInterwikiResults( $type = self::SECONDARY_RESULTS ) {
153 return null;
157 * Check if there are results on other wikis
159 * @param int $type
160 * @return bool
162 public function hasInterwikiResults( $type = self::SECONDARY_RESULTS ) {
163 return false;
167 * Did the search contain search syntax? If so, Special:Search won't offer
168 * the user a link to a create a page named by the search string because the
169 * name would contain the search syntax.
170 * @return bool
172 public function searchContainedSyntax() {
173 return $this->containedSyntax;
177 * @return bool True when there are more pages of search results available.
179 public function hasMoreResults() {
180 return $this->hasMoreResults;
184 * @param int $limit Shrink result set to $limit and flag
185 * if more results are available.
187 public function shrink( $limit ) {
188 if ( $this->count() > $limit ) {
189 $this->hasMoreResults = true;
190 // shrinking result set for implementations that
191 // have not implemented extractResults and use
192 // the default cache location. Other implementations
193 // must override this as well.
194 if ( is_array( $this->results ) ) {
195 $this->results = array_slice( $this->results, 0, $limit );
196 $this->titles = null;
197 } else {
198 throw new \UnexpectedValueException(
199 "When overriding result store extending classes must "
200 . " also override " . __METHOD__ );
206 * Extract all the results in the result set as array.
207 * @return SearchResult[]
209 public function extractResults() {
210 if ( $this->results === null ) {
211 $this->results = [];
212 if ( $this->numRows() == 0 ) {
213 // Don't bother if we've got empty result
214 return $this->results;
216 $this->rewind();
217 foreach ( $this as $result ) {
218 $this->results[] = $result;
220 $this->rewind();
222 return $this->results;
226 * Extract all the titles in the result set.
227 * @return Title[]
229 public function extractTitles() {
230 if ( $this->titles === null ) {
231 if ( $this->numRows() == 0 ) {
232 // Don't bother if we've got empty result
233 $this->titles = [];
234 } else {
235 $this->titles = array_map(
236 static function ( SearchResult $result ) {
237 return $result->getTitle();
239 $this->extractResults() );
242 return $this->titles;