Merge "Update type of PermissionManager::resultToError"
[lhc/web/wiklou.git] / includes / search / SqlSearchResultSet.php
1 <?php
2
3 use Wikimedia\Rdbms\IResultWrapper;
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 /** @noinspection PhpMissingParentConstructorInspection */
11
12 /** @var IResultWrapper Result object from database */
13 protected $resultSet;
14 /** @var string[] Requested search query */
15 protected $terms;
16 /** @var int|null Total number of hits for $terms */
17 protected $totalHits;
18
19 function __construct( IResultWrapper $resultSet, $terms, $total = null ) {
20 $this->resultSet = $resultSet;
21 $this->terms = $terms;
22 $this->totalHits = $total;
23 }
24
25 function termMatches() {
26 return $this->terms;
27 }
28
29 function numRows() {
30 if ( $this->resultSet === false ) {
31 return false;
32 }
33
34 return $this->resultSet->numRows();
35 }
36
37 public function extractResults() {
38 if ( $this->resultSet === false ) {
39 return [];
40 }
41
42 if ( $this->results === null ) {
43 $this->results = [];
44 $this->resultSet->rewind();
45 while ( ( $row = $this->resultSet->fetchObject() ) !== false ) {
46 $this->results[] = SearchResult::newFromTitle(
47 Title::makeTitle( $row->page_namespace, $row->page_title ), $this
48 );
49 }
50 }
51 return $this->results;
52 }
53
54 function free() {
55 if ( $this->resultSet === false ) {
56 return;
57 }
58
59 $this->resultSet->free();
60 }
61
62 function getTotalHits() {
63 if ( !is_null( $this->totalHits ) ) {
64 return $this->totalHits;
65 } else {
66 // Special:Search expects a number here.
67 return $this->numRows();
68 }
69 }
70 }