MediaWiki  1.34.0
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  } else {
192  throw new \UnexpectedValueException(
193  "When overriding result store extending classes must "
194  . " also override " . __METHOD__ );
195  }
196  }
197  }
198 
203  public function extractResults() {
204  if ( is_null( $this->results ) ) {
205  $this->results = [];
206  if ( $this->numRows() == 0 ) {
207  // Don't bother if we've got empty result
208  return $this->results;
209  }
210  $this->rewind();
211  while ( ( $result = $this->next() ) != false ) {
212  $this->results[] = $result;
213  }
214  $this->rewind();
215  }
216  return $this->results;
217  }
218 
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
227  $this->titles = [];
228  } else {
229  $this->titles = array_map(
230  function ( SearchResult $result ) {
231  return $result->getTitle();
232  },
233  $this->extractResults() );
234  }
235  }
236  return $this->titles;
237  }
238 }
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:38
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:147
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:167
BaseSearchResultSet
BaseSearchResultSet is the base class that must be extended by SearchEngine search result set impleme...
Definition: BaseSearchResultSet.php:14
BaseSearchResultSet\next
next()
Fetches next search result, or false.
Definition: BaseSearchResultSet.php:26
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)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
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:123
SearchResultSet\getSuggestionSnippet
getSuggestionSnippet()
Definition: SearchResultSet.php:137
SearchResultSet\extractResults
extractResults()
Extract all the results in the result set as array.
Definition: SearchResultSet.php:203
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:42
SearchResultSet\hasInterwikiResults
hasInterwikiResults( $type=self::SECONDARY_RESULTS)
Check if there are results on other wikis.
Definition: SearchResultSet.php:157
SearchResultSet\$hasMoreResults
boolean $hasMoreResults
True when there are more pages of search results available.
Definition: SearchResultSet.php:49
SearchResultSet\shrink
shrink( $limit)
Definition: SearchResultSet.php:182
SearchResultSetTrait
trait SearchResultSetTrait
Trait useful for SearchResultSet implementations.
Definition: SearchResultSetTrait.php:12
SearchResultSet\getQueryAfterRewriteSnippet
getQueryAfterRewriteSnippet()
Definition: SearchResultSet.php:113
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:174
SearchResultSet\getSuggestionQuery
getSuggestionQuery()
Definition: SearchResultSet.php:130
SearchResultSet\extractTitles
extractTitles()
Extract all the titles in the result set.
Definition: SearchResultSet.php:223
SearchResultSet\$containedSyntax
$containedSyntax
Definition: SearchResultSet.php:30
$type
$type
Definition: testCompression.php:48