1<?php
2
3use Wikimedia\Rdbms\IResultWrapper;
4
5/**
6 * This class is used for different SQL-based search engines shipped with MediaWiki
7 * @ingroup Search
8 */
9class 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	/**
20	 * @param IResultWrapper $resultSet
21	 * @param string[] $terms
22	 * @param int|null $total
23	 */
24	public function __construct( IResultWrapper $resultSet, array $terms, $total = null ) {
25		parent::__construct();
26		$this->resultSet = $resultSet;
27		$this->terms = $terms;
28		$this->totalHits = $total;
29	}
30
31	/**
32	 * @return string[]
33	 * @deprecated since 1.34
34	 */
35	public function termMatches() {
36		return $this->terms;
37	}
38
39	public function numRows() {
40		if ( $this->resultSet === false ) {
41			return false;
42		}
43
44		return $this->resultSet->numRows();
45	}
46
47	public function extractResults() {
48		if ( $this->resultSet === false ) {
49			return [];
50		}
51
52		if ( $this->results === null ) {
53			$this->results = [];
54			$this->resultSet->rewind();
55			$terms = \MediaWiki\MediaWikiServices::getInstance()->getContentLanguage()
56				->convertForSearchResult( $this->terms );
57			while ( ( $row = $this->resultSet->fetchObject() ) !== false ) {
58				$result = new SqlSearchResult(
59					Title::makeTitle( $row->page_namespace, $row->page_title ),
60					$terms
61				);
62				$this->augmentResult( $result );
63				$this->results[] = $result;
64			}
65		}
66		return $this->results;
67	}
68
69	public function getTotalHits() {
70		if ( $this->totalHits !== null ) {
71			return $this->totalHits;
72		} else {
73			// Special:Search expects a number here.
74			return $this->numRows();
75		}
76	}
77}
78