2016-01-03 23:10:32 +00:00
|
|
|
<?php
|
2017-02-19 05:03:13 +00:00
|
|
|
|
2019-06-16 22:13:02 +00:00
|
|
|
use Wikimedia\Rdbms\IResultWrapper;
|
2017-02-19 05:03:13 +00:00
|
|
|
|
2016-01-03 23:10:32 +00:00
|
|
|
/**
|
|
|
|
|
* This class is used for different SQL-based search engines shipped with MediaWiki
|
|
|
|
|
* @ingroup Search
|
|
|
|
|
*/
|
|
|
|
|
class SqlSearchResultSet extends SearchResultSet {
|
2019-06-16 22:13:02 +00:00
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */
|
|
|
|
|
|
|
|
|
|
/** @var IResultWrapper Result object from database */
|
2016-01-03 23:10:32 +00:00
|
|
|
protected $resultSet;
|
2019-06-07 18:43:02 +00:00
|
|
|
/** @var string[] Requested search query */
|
2016-01-03 23:10:32 +00:00
|
|
|
protected $terms;
|
2018-05-10 22:03:55 +00:00
|
|
|
/** @var int|null Total number of hits for $terms */
|
2016-01-03 23:10:32 +00:00
|
|
|
protected $totalHits;
|
|
|
|
|
|
2019-06-12 09:02:10 +00:00
|
|
|
/**
|
|
|
|
|
* @param IResultWrapper $resultSet
|
|
|
|
|
* @param string[] $terms
|
|
|
|
|
* @param int|null $total
|
|
|
|
|
*/
|
2019-11-30 23:03:59 +00:00
|
|
|
public function __construct( IResultWrapper $resultSet, array $terms, $total = null ) {
|
2019-06-12 09:02:10 +00:00
|
|
|
parent::__construct();
|
2016-01-03 23:10:32 +00:00
|
|
|
$this->resultSet = $resultSet;
|
|
|
|
|
$this->terms = $terms;
|
|
|
|
|
$this->totalHits = $total;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-12 09:02:10 +00:00
|
|
|
/**
|
|
|
|
|
* @return string[]
|
|
|
|
|
* @deprecated since 1.34
|
|
|
|
|
*/
|
2020-05-17 02:07:58 +00:00
|
|
|
public function termMatches() {
|
2016-01-03 23:10:32 +00:00
|
|
|
return $this->terms;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 02:07:58 +00:00
|
|
|
public function numRows() {
|
2016-01-03 23:10:32 +00:00
|
|
|
if ( $this->resultSet === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->resultSet->numRows();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 22:03:55 +00:00
|
|
|
public function extractResults() {
|
2016-01-03 23:10:32 +00:00
|
|
|
if ( $this->resultSet === false ) {
|
2018-05-10 22:03:55 +00:00
|
|
|
return [];
|
2016-01-03 23:10:32 +00:00
|
|
|
}
|
|
|
|
|
|
2018-05-10 22:03:55 +00:00
|
|
|
if ( $this->results === null ) {
|
|
|
|
|
$this->results = [];
|
2016-04-13 19:21:54 +00:00
|
|
|
$this->resultSet->rewind();
|
2019-06-12 09:02:10 +00:00
|
|
|
$terms = \MediaWiki\MediaWikiServices::getInstance()->getContentLanguage()
|
|
|
|
|
->convertForSearchResult( $this->terms );
|
2018-05-10 22:03:55 +00:00
|
|
|
while ( ( $row = $this->resultSet->fetchObject() ) !== false ) {
|
2019-06-12 09:02:10 +00:00
|
|
|
$result = new SqlSearchResult(
|
|
|
|
|
Title::makeTitle( $row->page_namespace, $row->page_title ),
|
|
|
|
|
$terms
|
2018-05-10 22:03:55 +00:00
|
|
|
);
|
2019-06-12 09:02:10 +00:00
|
|
|
$this->augmentResult( $result );
|
|
|
|
|
$this->results[] = $result;
|
2018-05-10 22:03:55 +00:00
|
|
|
}
|
2016-04-13 19:21:54 +00:00
|
|
|
}
|
2018-05-10 22:03:55 +00:00
|
|
|
return $this->results;
|
2016-04-13 19:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
2020-05-17 02:07:58 +00:00
|
|
|
public function getTotalHits() {
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $this->totalHits !== null ) {
|
2016-01-03 23:10:32 +00:00
|
|
|
return $this->totalHits;
|
|
|
|
|
} else {
|
|
|
|
|
// Special:Search expects a number here.
|
|
|
|
|
return $this->numRows();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|