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
implements ISearchResultSet
{
29 protected $containedSyntax = false;
33 * Lists titles of the result set, in the same order as results.
39 * Cache of results - serialization of the result iterator
46 * Set of result's extra data, indexed per result id
47 * and then per data item name.
49 * PAGE_ID => [ augmentor name => data, ... ]
52 protected $extraData = [];
55 * @var boolean True when there are more pages of search results available.
57 private $hasMoreResults;
60 * @var ArrayIterator|null Iterator supporting BC iteration methods
65 * @param bool $containedSyntax True when query is not requesting a simple
67 * @param bool $hasMoreResults True when there are more pages of search
70 public function __construct( $containedSyntax = false, $hasMoreResults = false ) {
71 if ( static::class === self
::class ) {
72 // This class will eventually be abstract. SearchEngine implementations
73 // already have to extend this class anyways to provide the actual
75 wfDeprecated( __METHOD__
, '1.32' );
77 $this->containedSyntax
= $containedSyntax;
78 $this->hasMoreResults
= $hasMoreResults;
82 * Fetch an array of regular expression fragments for matching
83 * the search terms as parsed by this engine in a text extract.
87 * @deprecated since 1.34 (use SqlSearchResult)
89 public function termMatches() {
93 public function numRows() {
94 return $this->count();
97 final public function count() {
98 return count( $this->extractResults() );
102 * Some search modes return a total hit count for the query
103 * in the entire article database. This may include pages
104 * in namespaces that would not be matched on the given
107 * Return null if no total hits number is supported.
111 public function getTotalHits() {
116 * Some search modes will run an alternative query that it thinks gives
117 * a better result than the provided search. Returns true if this has
122 public function hasRewrittenQuery() {
127 * @return string|null The search the query was internally rewritten to,
128 * or null when the result of the original query was returned.
130 public function getQueryAfterRewrite() {
135 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
136 * and with changes highlighted. Null when the query was not rewritten.
138 public function getQueryAfterRewriteSnippet() {
143 * Some search modes return a suggested alternate term if there are
144 * no exact hits. Returns true if there is one on this set.
148 public function hasSuggestion() {
153 * @return string|null Suggested query, null if none
155 public function getSuggestionQuery() {
160 * @return string HTML highlighted suggested query, '' if none
162 public function getSuggestionSnippet() {
167 * Return a result set of hits on other (multiple) wikis associated with this one
170 * @return ISearchResultSet[]
172 public function getInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
177 * Check if there are results on other wikis
182 public function hasInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
187 * Fetches next search result, or false.
188 * @deprecated since 1.32; Use self::extractResults() or foreach
189 * @return SearchResult|false
191 public function next() {
192 wfDeprecated( __METHOD__
, '1.32' );
193 $it = $this->bcIterator();
194 $searchResult = $it->current();
196 return $searchResult ??
false;
200 * Rewind result set back to beginning
201 * @deprecated since 1.32; Use self::extractResults() or foreach
203 public function rewind() {
204 wfDeprecated( __METHOD__
, '1.32' );
205 $this->bcIterator()->rewind();
208 private function bcIterator() {
209 if ( $this->bcIterator
=== null ) {
210 $this->bcIterator
= 'RECURSION';
211 $this->bcIterator
= $this->getIterator();
212 } elseif ( $this->bcIterator
=== 'RECURSION' ) {
213 // Either next/rewind or extractResults must be implemented. This
214 // class was potentially instantiated directly. It should be
215 // abstract with abstract methods to enforce this but that's a
216 // breaking change...
217 wfDeprecated( static::class . ' without implementing extractResults', '1.32' );
218 $this->bcIterator
= new ArrayIterator( [] );
220 return $this->bcIterator
;
224 * Frees the result set, if applicable.
225 * @deprecated noop since 1.34
227 public function free() {
231 * Did the search contain search syntax? If so, Special:Search won't offer
232 * the user a link to a create a page named by the search string because the
233 * name would contain the search syntax.
236 public function searchContainedSyntax() {
237 return $this->containedSyntax
;
241 * @return bool True when there are more pages of search results available.
243 public function hasMoreResults() {
244 return $this->hasMoreResults
;
248 * @param int $limit Shrink result set to $limit and flag
249 * if more results are available.
251 public function shrink( $limit ) {
252 if ( $this->count() > $limit ) {
253 $this->hasMoreResults
= true;
254 // shrinking result set for implementations that
255 // have not implemented extractResults and use
256 // the default cache location. Other implementations
257 // must override this as well.
258 if ( is_array( $this->results
) ) {
259 $this->results
= array_slice( $this->results
, 0, $limit );
261 throw new \
UnexpectedValueException(
262 "When overriding result store extending classes must "
263 . " also override " . __METHOD__
);
269 * Extract all the results in the result set as array.
270 * @return SearchResult[]
272 public function extractResults() {
273 if ( is_null( $this->results
) ) {
275 if ( $this->numRows() == 0 ) {
276 // Don't bother if we've got empty result
277 return $this->results
;
280 while ( ( $result = $this->next() ) != false ) {
281 $this->results
[] = $result;
285 return $this->results
;
289 * Extract all the titles in the result set.
292 public function extractTitles() {
293 if ( is_null( $this->titles
) ) {
294 if ( $this->numRows() == 0 ) {
295 // Don't bother if we've got empty result
298 $this->titles
= array_map(
299 function ( SearchResult
$result ) {
300 return $result->getTitle();
302 $this->extractResults() );
305 return $this->titles
;
309 * Sets augmented data for result set.
310 * @param string $name Extra data item name
311 * @param array[] $data Extra data as PAGEID => data
313 public function setAugmentedData( $name, $data ) {
314 foreach ( $data as $id => $resultData ) {
315 $this->extraData
[$id][$name] = $resultData;
320 * Returns extra data for specific result and store it in SearchResult object.
321 * @param SearchResult $result
323 public function augmentResult( SearchResult
$result ) {
324 $id = $result->getTitle()->getArticleID();
328 $result->setExtensionData( function () use ( $id ) {
329 return $this->extraData
[$id] ??
[];
334 * @return int|null The offset the current page starts at. Typically
335 * this should be null to allow the UI to decide on its own, but in
336 * special cases like interleaved AB tests specifying explicitly is
339 public function getOffset() {
343 final public function getIterator() {
344 return new ArrayIterator( $this->extractResults() );