Convert SearchResultSet to typical iteration
[lhc/web/wiklou.git] / includes / search / SqlSearchResultSet.php
1 <?php
2
3 use Wikimedia\Rdbms\ResultWrapper;
4
5 /**
6 * This class is used for different SQL-based search engines shipped with MediaWiki
7 * @ingroup Search
8 */
9 class SqlSearchResultSet extends SearchResultSet {
10 /** @var ResultWrapper Result object from database */
11 protected $resultSet;
12 /** @var string Requested search query */
13 protected $terms;
14 /** @var int|null Total number of hits for $terms */
15 protected $totalHits;
16
17 function __construct( ResultWrapper $resultSet, $terms, $total = null ) {
18 $this->resultSet = $resultSet;
19 $this->terms = $terms;
20 $this->totalHits = $total;
21 }
22
23 function termMatches() {
24 return $this->terms;
25 }
26
27 function numRows() {
28 if ( $this->resultSet === false ) {
29 return false;
30 }
31
32 return $this->resultSet->numRows();
33 }
34
35 public function extractResults() {
36 if ( $this->resultSet === false ) {
37 return [];
38 }
39
40 if ( $this->results === null ) {
41 $this->results = [];
42 $this->resultSet->rewind();
43 while ( ( $row = $this->resultSet->fetchObject() ) !== false ) {
44 $this->results[] = SearchResult::newFromTitle(
45 Title::makeTitle( $row->page_namespace, $row->page_title ), $this
46 );
47 }
48 }
49 return $this->results;
50 }
51
52 function free() {
53 if ( $this->resultSet === false ) {
54 return false;
55 }
56
57 $this->resultSet->free();
58 }
59
60 function getTotalHits() {
61 if ( !is_null( $this->totalHits ) ) {
62 return $this->totalHits;
63 } else {
64 // Special:Search expects a number here.
65 return $this->numRows();
66 }
67 }
68 }