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 IteratorAggregate
{
30 * Types of interwiki results
33 * Results that are displayed only together with existing main wiki results
36 const SECONDARY_RESULTS
= 0;
38 * Results that can displayed even if no existing main wiki results exist
41 const INLINE_RESULTS
= 1;
43 protected $containedSyntax = false;
47 * Lists titles of the result set, in the same order as results.
53 * Cache of results - serialization of the result iterator
60 * Set of result's extra data, indexed per result id
61 * and then per data item name.
63 * PAGE_ID => [ augmentor name => data, ... ]
66 protected $extraData = [];
68 /** @var ArrayIterator|null Iterator supporting BC iteration methods */
71 public function __construct( $containedSyntax = false ) {
72 if ( static::class === __CLASS__
) {
73 // This class will eventually be abstract. SearchEngine implementations
74 // already have to extend this class anyways to provide the actual
76 wfDeprecated( __METHOD__
, 1.32 );
78 $this->containedSyntax
= $containedSyntax;
82 * Fetch an array of regular expression fragments for matching
83 * the search terms as parsed by this engine in a text extract.
88 function termMatches() {
97 * Some search modes return a total hit count for the query
98 * in the entire article database. This may include pages
99 * in namespaces that would not be matched on the given
102 * Return null if no total hits number is supported.
106 function getTotalHits() {
111 * Some search modes will run an alternative query that it thinks gives
112 * a better result than the provided search. Returns true if this has
117 function hasRewrittenQuery() {
122 * @return string|null The search the query was internally rewritten to,
123 * or null when the result of the original query was returned.
125 function getQueryAfterRewrite() {
130 * @return string|null Same as self::getQueryAfterRewrite(), but in HTML
131 * and with changes highlighted. Null when the query was not rewritten.
133 function getQueryAfterRewriteSnippet() {
138 * Some search modes return a suggested alternate term if there are
139 * no exact hits. Returns true if there is one on this set.
143 function hasSuggestion() {
148 * @return string|null Suggested query, null if none
150 function getSuggestionQuery() {
155 * @return string HTML highlighted suggested query, '' if none
157 function getSuggestionSnippet() {
162 * Return a result set of hits on other (multiple) wikis associated with this one
165 * @return SearchResultSet[]
167 function getInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
172 * Check if there are results on other wikis
177 function hasInterwikiResults( $type = self
::SECONDARY_RESULTS
) {
182 * Fetches next search result, or false.
183 * @deprecated since 1.32; Use self::extractResults() or foreach
184 * @return SearchResult|false
186 public function next() {
187 wfDeprecated( __METHOD__
, '1.32' );
188 $it = $this->bcIterator();
189 $searchResult = $it->current();
191 return $searchResult === null ?
false : $searchResult;
195 * Rewind result set back to beginning
196 * @deprecated since 1.32; Use self::extractResults() or foreach
198 public function rewind() {
199 wfDeprecated( __METHOD__
, '1.32' );
200 $this->bcIterator()->rewind();
203 private function bcIterator() {
204 if ( $this->bcIterator
=== null ) {
205 $this->bcIterator
= 'RECURSION';
206 $this->bcIterator
= $this->getIterator();
207 } elseif ( $this->bcIterator
=== 'RECURSION' ) {
208 // Either next/rewind or extractResults must be implemented. This
209 // class was potentially instantiated directly. It should be
210 // abstract with abstract methods to enforce this but that's a
211 // breaking change...
212 wfDeprecated( static::class . ' without implementing extractResults', '1.32' );
213 $this->bcIterator
= new ArrayIterator( [] );
215 return $this->bcIterator
;
219 * Frees the result set, if applicable.
226 * Did the search contain search syntax? If so, Special:Search won't offer
227 * the user a link to a create a page named by the search string because the
228 * name would contain the search syntax.
231 public function searchContainedSyntax() {
232 return $this->containedSyntax
;
236 * Extract all the results in the result set as array.
237 * @return SearchResult[]
239 public function extractResults() {
240 if ( is_null( $this->results
) ) {
242 if ( $this->numRows() == 0 ) {
243 // Don't bother if we've got empty result
244 return $this->results
;
247 while ( ( $result = $this->next() ) != false ) {
248 $this->results
[] = $result;
252 return $this->results
;
256 * Extract all the titles in the result set.
259 public function extractTitles() {
260 if ( is_null( $this->titles
) ) {
261 if ( $this->numRows() == 0 ) {
262 // Don't bother if we've got empty result
265 $this->titles
= array_map(
266 function ( SearchResult
$result ) {
267 return $result->getTitle();
269 $this->extractResults() );
272 return $this->titles
;
276 * Sets augmented data for result set.
277 * @param string $name Extra data item name
278 * @param array[] $data Extra data as PAGEID => data
280 public function setAugmentedData( $name, $data ) {
281 foreach ( $data as $id => $resultData ) {
282 $this->extraData
[$id][$name] = $resultData;
287 * Returns extra data for specific result and store it in SearchResult object.
288 * @param SearchResult $result
290 public function augmentResult( SearchResult
$result ) {
291 $id = $result->getTitle()->getArticleID();
295 $result->setExtensionData( function () use ( $id ) {
296 if ( isset( $this->extraData
[$id] ) ) {
297 return $this->extraData
[$id];
305 * @return int|null The offset the current page starts at. Typically
306 * this should be null to allow the UI to decide on its own, but in
307 * special cases like interleaved AB tests specifying explicitly is
310 public function getOffset() {
314 final public function getIterator() {
315 return new ArrayIterator( $this->extractResults() );