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 
114  public function getQueryAfterRewriteSnippet() {
115  return null;
116  }
117 
124  public function hasSuggestion() {
125  return false;
126  }
127 
131  public function getSuggestionQuery() {
132  return null;
133  }
134 
138  public function getSuggestionSnippet() {
139  return '';
140  }
141 
148  public function getInterwikiResults( $type = self::SECONDARY_RESULTS ) {
149  return null;
150  }
151 
158  public function hasInterwikiResults( $type = self::SECONDARY_RESULTS ) {
159  return false;
160  }
161 
168  public function searchContainedSyntax() {
169  return $this->containedSyntax;
170  }
171 
175  public function hasMoreResults() {
176  return $this->hasMoreResults;
177  }
178 
183  public function shrink( $limit ) {
184  if ( $this->count() > $limit ) {
185  $this->hasMoreResults = true;
186  // shrinking result set for implementations that
187  // have not implemented extractResults and use
188  // the default cache location. Other implementations
189  // must override this as well.
190  if ( is_array( $this->results ) ) {
191  $this->results = array_slice( $this->results, 0, $limit );
192  $this->titles = null;
193  } else {
194  throw new \UnexpectedValueException(
195  "When overriding result store extending classes must "
196  . " also override " . __METHOD__ );
197  }
198  }
199  }
200 
205  public function extractResults() {
206  if ( $this->results === null ) {
207  $this->results = [];
208  if ( $this->numRows() == 0 ) {
209  // Don't bother if we've got empty result
210  return $this->results;
211  }
212  $this->rewind();
213  while ( ( $result = $this->next() ) != false ) {
214  $this->results[] = $result;
215  }
216  $this->rewind();
217  }
218  return $this->results;
219  }
220 
225  public function extractTitles() {
226  if ( $this->titles === null ) {
227  if ( $this->numRows() == 0 ) {
228  // Don't bother if we've got empty result
229  $this->titles = [];
230  } else {
231  $this->titles = array_map(
232  function ( SearchResult $result ) {
233  return $result->getTitle();
234  },
235  $this->extractResults() );
236  }
237  }
238  return $this->titles;
239  }
240 }
SearchResultSet\count
count()
Definition: SearchResultSet.php:72
SearchResultSet\getQueryAfterRewrite
getQueryAfterRewrite()
Definition: SearchResultSet.php:105
BaseSearchResultSet\rewind
rewind()
Rewind result set back to beginning.
Definition: BaseSearchResultSet.php:39
SearchResultSet\__construct
__construct( $containedSyntax=false, $hasMoreResults=false)
Definition: SearchResultSet.php:57
SearchResultSet\getInterwikiResults
getInterwikiResults( $type=self::SECONDARY_RESULTS)
Return a result set of hits on other (multiple) wikis associated with this one.
Definition: SearchResultSet.php:148
SearchResultSet\searchContainedSyntax
searchContainedSyntax()
Did the search contain search syntax? If so, Special:Search won't offer the user a link to a create a...
Definition: SearchResultSet.php:168
BaseSearchResultSet
BaseSearchResultSet is the base class that must be extended by SearchEngine search result set impleme...
Definition: BaseSearchResultSet.php:15
BaseSearchResultSet\next
next()
Fetches next search result, or false.
Definition: BaseSearchResultSet.php:27
SearchResultSet\$titles
Title[] $titles
Cache of titles.
Definition: SearchResultSet.php:37
SearchResult
NOTE: this class is being refactored into an abstract base class.
Definition: SearchResult.php:38
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1027
SearchResultSet\numRows
numRows()
Definition: SearchResultSet.php:68
SearchResultSet
Definition: SearchResultSet.php:27
SearchResultSet\hasSuggestion
hasSuggestion()
Some search modes return a suggested alternate term if there are no exact hits.
Definition: SearchResultSet.php:124
SearchResultSet\getSuggestionSnippet
getSuggestionSnippet()
Definition: SearchResultSet.php:138
SearchResultSet\extractResults
extractResults()
Extract all the results in the result set as array.
Definition: SearchResultSet.php:205
SearchResultSet\getTotalHits
getTotalHits()
Some search modes return a total hit count for the query in the entire article database.
Definition: SearchResultSet.php:86
SearchResultSet\hasRewrittenQuery
hasRewrittenQuery()
Some search modes will run an alternative query that it thinks gives a better result than the provide...
Definition: SearchResultSet.php:97
Title
Represents a title within MediaWiki.
Definition: Title.php:41
SearchResultSet\hasInterwikiResults
hasInterwikiResults( $type=self::SECONDARY_RESULTS)
Check if there are results on other wikis.
Definition: SearchResultSet.php:158
SearchResultSet\shrink
shrink( $limit)
Definition: SearchResultSet.php:183
SearchResultSetTrait
trait SearchResultSetTrait
Trait useful for SearchResultSet implementations.
Definition: SearchResultSetTrait.php:12
SearchResultSet\getQueryAfterRewriteSnippet
getQueryAfterRewriteSnippet()
Definition: SearchResultSet.php:114
SearchResultSet\$results
SearchResult[] $results
Cache of results - serialization of the result iterator as an array.
Definition: SearchResultSet.php:44
SearchResultSet\hasMoreResults
hasMoreResults()
Definition: SearchResultSet.php:175
SearchResultSet\getSuggestionQuery
getSuggestionQuery()
Definition: SearchResultSet.php:131
SearchResultSet\$hasMoreResults
bool $hasMoreResults
True when there are more pages of search results available.
Definition: SearchResultSet.php:49
SearchResultSet\extractTitles
extractTitles()
Extract all the titles in the result set.
Definition: SearchResultSet.php:225
SearchResultSet\$containedSyntax
$containedSyntax
Definition: SearchResultSet.php:30
$type
$type
Definition: testCompression.php:52