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
24 use MediaWiki\Title\Title
;
29 class SearchResultSet
extends BaseSearchResultSet
{
31 use SearchResultSetTrait
;
34 protected $containedSyntax = false;
38 * Lists titles of the result set, in the same order as results.
44 * Cache of results - serialization of the result iterator
51 * @var bool True when there are more pages of search results available.
53 private $hasMoreResults;
56 * @param bool $containedSyntax True when query is not requesting a simple
58 * @param bool $hasMoreResults True when there are more pages of search
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
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() );
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
86 * Return null if no total hits number is supported.
90 public function getTotalHits() {
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
101 public function hasRewrittenQuery() {
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() {
114 * @return HtmlArmor|string|null Same as self::getQueryAfterRewrite(), but
115 * with changes highlighted if HtmlArmor is returned. Null when the query
118 public function getQueryAfterRewriteSnippet() {
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.
128 public function hasSuggestion() {
133 * @return string|null Suggested query, null if none
135 public function getSuggestionQuery() {
140 * @return HtmlArmor|string HTML highlighted suggested query, '' if none
142 public function getSuggestionSnippet() {
147 * Return a result set of hits on other (multiple) wikis associated with this one
150 * @return ISearchResultSet[]|null
152 public function getInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
157 * Check if there are results on other wikis
162 public function hasInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
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.
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;
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 ) {
212 if ( $this->numRows() == 0 ) {
213 // Don't bother if we've got empty result
214 return $this->results
;
217 foreach ( $this as $result ) {
218 $this->results
[] = $result;
222 return $this->results
;
226 * Extract all the titles in the result set.
229 public function extractTitles() {
230 if ( $this->titles
=== null ) {
231 if ( $this->numRows() == 0 ) {
232 // Don't bother if we've got empty result
235 $this->titles
= array_map(
236 static function ( SearchResult
$result ) {
237 return $result->getTitle();
239 $this->extractResults() );
242 return $this->titles
;