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
extends BaseSearchResultSet
{
28 use SearchResultSetTrait
;
30 protected $containedSyntax = false;
34 * Lists titles of the result set, in the same order as results.
40 * Cache of results - serialization of the result iterator
47 * @var boolean True when there are more pages of search results available.
49 private $hasMoreResults;
52 * @param bool $containedSyntax True when query is not requesting a simple
54 * @param bool $hasMoreResults True when there are more pages of search
57 public function __construct( $containedSyntax = false, $hasMoreResults = false ) {
58 if ( static::class === self
::class ) {
59 // This class will eventually be abstract. SearchEngine implementations
60 // already have to extend this class anyways to provide the actual
62 wfDeprecated( __METHOD__
, '1.32' );
64 $this->containedSyntax
= $containedSyntax;
65 $this->hasMoreResults
= $hasMoreResults;
68 public function numRows() {
69 return $this->count();
72 final public function count() {
73 return count( $this->extractResults() );
77 * Some search modes return a total hit count for the query
78 * in the entire article database. This may include pages
79 * in namespaces that would not be matched on the given
82 * Return null if no total hits number is supported.
86 public function getTotalHits() {
91 * Some search modes will run an alternative query that it thinks gives
92 * a better result than the provided search. Returns true if this has
97 public function hasRewrittenQuery() {
102 * @return string|null The search the query was internally rewritten to,
103 * or null when the result of the original query was returned.
105 public function getQueryAfterRewrite() {
110 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
111 * and with changes highlighted. Null when the query was not rewritten.
113 public function getQueryAfterRewriteSnippet() {
118 * Some search modes return a suggested alternate term if there are
119 * no exact hits. Returns true if there is one on this set.
123 public function hasSuggestion() {
128 * @return string|null Suggested query, null if none
130 public function getSuggestionQuery() {
135 * @return string HTML highlighted suggested query, '' if none
137 public function getSuggestionSnippet() {
142 * Return a result set of hits on other (multiple) wikis associated with this one
145 * @return ISearchResultSet[]
147 public function getInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
152 * Check if there are results on other wikis
157 public function hasInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
162 * Did the search contain search syntax? If so, Special:Search won't offer
163 * the user a link to a create a page named by the search string because the
164 * name would contain the search syntax.
167 public function searchContainedSyntax() {
168 return $this->containedSyntax
;
172 * @return bool True when there are more pages of search results available.
174 public function hasMoreResults() {
175 return $this->hasMoreResults
;
179 * @param int $limit Shrink result set to $limit and flag
180 * if more results are available.
182 public function shrink( $limit ) {
183 if ( $this->count() > $limit ) {
184 $this->hasMoreResults
= true;
185 // shrinking result set for implementations that
186 // have not implemented extractResults and use
187 // the default cache location. Other implementations
188 // must override this as well.
189 if ( is_array( $this->results
) ) {
190 $this->results
= array_slice( $this->results
, 0, $limit );
192 throw new \
UnexpectedValueException(
193 "When overriding result store extending classes must "
194 . " also override " . __METHOD__
);
200 * Extract all the results in the result set as array.
201 * @return SearchResult[]
203 public function extractResults() {
204 if ( is_null( $this->results
) ) {
206 if ( $this->numRows() == 0 ) {
207 // Don't bother if we've got empty result
208 return $this->results
;
211 while ( ( $result = $this->next() ) != false ) {
212 $this->results
[] = $result;
216 return $this->results
;
220 * Extract all the titles in the result set.
223 public function extractTitles() {
224 if ( is_null( $this->titles
) ) {
225 if ( $this->numRows() == 0 ) {
226 // Don't bother if we've got empty result
229 $this->titles
= array_map(
230 function ( SearchResult
$result ) {
231 return $result->getTitle();
233 $this->extractResults() );
236 return $this->titles
;