MediaWiki  master
SearchResultSet.php
Go to the documentation of this file.
1 <?php
29 
30  protected $containedSyntax = false;
31 
37  private $titles;
38 
44  protected $results;
45 
49  private $hasMoreResults;
50 
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
61  // search results.
62  wfDeprecated( __METHOD__, '1.32' );
63  }
64  $this->containedSyntax = $containedSyntax;
66  }
67 
68  public function numRows() {
69  return $this->count();
70  }
71 
72  final public function count() {
73  return count( $this->extractResults() );
74  }
75 
86  public function getTotalHits() {
87  return null;
88  }
89 
97  public function hasRewrittenQuery() {
98  return false;
99  }
100 
105  public function getQueryAfterRewrite() {
106  return null;
107  }
108 
113  public function getQueryAfterRewriteSnippet() {
114  return null;
115  }
116 
123  public function hasSuggestion() {
124  return false;
125  }
126 
130  public function getSuggestionQuery() {
131  return null;
132  }
133 
137  public function getSuggestionSnippet() {
138  return '';
139  }
140 
147  public function getInterwikiResults( $type = self::SECONDARY_RESULTS ) {
148  return null;
149  }
150 
157  public function hasInterwikiResults( $type = self::SECONDARY_RESULTS ) {
158  return false;
159  }
160 
167  public function searchContainedSyntax() {
168  return $this->containedSyntax;
169  }
170 
174  public function hasMoreResults() {
175  return $this->hasMoreResults;
176  }
177 
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 );
191  $this->titles = null;
192  } else {
193  throw new \UnexpectedValueException(
194  "When overriding result store extending classes must "
195  . " also override " . __METHOD__ );
196  }
197  }
198  }
199 
204  public function extractResults() {
205  if ( is_null( $this->results ) ) {
206  $this->results = [];
207  if ( $this->numRows() == 0 ) {
208  // Don't bother if we've got empty result
209  return $this->results;
210  }
211  $this->rewind();
212  while ( ( $result = $this->next() ) != false ) {
213  $this->results[] = $result;
214  }
215  $this->rewind();
216  }
217  return $this->results;
218  }
219 
224  public function extractTitles() {
225  if ( is_null( $this->titles ) ) {
226  if ( $this->numRows() == 0 ) {
227  // Don't bother if we've got empty result
228  $this->titles = [];
229  } else {
230  $this->titles = array_map(
231  function ( SearchResult $result ) {
232  return $result->getTitle();
233  },
234  $this->extractResults() );
235  }
236  }
237  return $this->titles;
238  }
239 }
hasInterwikiResults( $type=self::SECONDARY_RESULTS)
Check if there are results on other wikis.
next()
Fetches next search result, or false.
hasSuggestion()
Some search modes return a suggested alternate term if there are no exact hits.
hasRewrittenQuery()
Some search modes will run an alternative query that it thinks gives a better result than the provide...
BaseSearchResultSet is the base class that must be extended by SearchEngine search result set impleme...
searchContainedSyntax()
Did the search contain search syntax? If so, Special:Search won&#39;t offer the user a link to a create a...
SearchResult [] $results
Cache of results - serialization of the result iterator as an array.
boolean $hasMoreResults
True when there are more pages of search results available.
__construct( $containedSyntax=false, $hasMoreResults=false)
getTotalHits()
Some search modes return a total hit count for the query in the entire article database.
rewind()
Rewind result set back to beginning.
extractTitles()
Extract all the titles in the result set.
trait SearchResultSetTrait
Trait useful for SearchResultSet implementations.
getInterwikiResults( $type=self::SECONDARY_RESULTS)
Return a result set of hits on other (multiple) wikis associated with this one.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Title [] $titles
Cache of titles.
extractResults()
Extract all the results in the result set as array.