3 namespace MediaWiki\Widget\Search
;
5 use MediaWiki\MediaWikiServices
;
12 * Renders the search result area. Handles Title and Full-Text search results,
13 * along with inline and sidebar secondary (interwiki) results.
15 class BasicSearchResultSetWidget
{
16 /** @var SpecialSearch */
17 protected $specialPage;
18 /** @var SearchResultWidget */
19 protected $resultWidget;
20 /** @var InterwikiSearchResultSetWidget */
21 protected $sidebarWidget;
23 public function __construct(
24 SpecialSearch
$specialPage,
25 SearchResultWidget
$resultWidget,
26 SearchResultSetWidget
$sidebarWidget
28 $this->specialPage
= $specialPage;
29 $this->resultWidget
= $resultWidget;
30 $this->sidebarWidget
= $sidebarWidget;
34 * @param string $term The search term to highlight
35 * @param int $offset The offset of the first result in the result set
36 * @param SearchResultSet|null $titleResultSet Results of searching only page titles
37 * @param SearchResultSet|null $textResultSet Results of general full text search.
40 public function render(
43 SearchResultSet
$titleResultSet = null,
44 SearchResultSet
$textResultSet = null
46 $hasTitle = $titleResultSet ?
$titleResultSet->numRows() > 0 : false;
47 $hasText = $textResultSet ?
$textResultSet->numRows() > 0 : false;
48 $hasSecondary = $textResultSet
49 ?
$textResultSet->hasInterwikiResults( SearchResultSet
::SECONDARY_RESULTS
)
51 $hasSecondaryInline = $textResultSet
52 ?
$textResultSet->hasInterwikiResults( SearchResultSet
::INLINE_RESULTS
)
55 if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) {
61 $out .= $this->header( $this->specialPage
->msg( 'titlematches' ) )
62 . $this->renderResultSet( $titleResultSet, $offset );
67 $out .= "<div class='mw-search-visualclear'></div>" .
68 $this->header( $this->specialPage
->msg( 'textmatches' ) );
70 $out .= $this->renderResultSet( $textResultSet, $offset );
73 if ( $hasSecondaryInline ) {
74 $iwResults = $textResultSet->getInterwikiResults( SearchResultSet
::INLINE_RESULTS
);
75 foreach ( $iwResults as $interwiki => $results ) {
76 if ( $results instanceof Status ||
$results->numRows() === 0 ) {
77 // ignore bad interwikis for now
81 "<h2 class='mw-search-interwiki-header mw-search-visualclear'>" .
82 $this->specialPage
->msg( "search-interwiki-results-{$interwiki}" )->parse() .
84 $out .= $this->renderResultSet( $results, $offset );
88 if ( $hasSecondary ) {
89 $out .= $this->sidebarWidget
->render(
91 $textResultSet->getInterwikiResults( SearchResultSet
::SECONDARY_RESULTS
)
95 // Convert the whole thing to desired language variant
96 // TODO: Move this up to Special:Search?
97 return MediaWikiServices
::getInstance()->getContentLanguage()->convert( $out );
101 * Generate a headline for a section of the search results. In prior
102 * implementations this was rendering wikitext of '==$1==', but seems
103 * a waste to call the full parser to generate this tiny bit of html
105 * @param Message $msg i18n message to use as header
106 * @return string HTML
108 protected function header( Message
$msg ) {
110 "<span class='mw-headline'>" . $msg->escaped() . "</span>" .
115 * @param SearchResultSet $resultSet The search results to render
116 * @param int $offset Offset of the first result in $resultSet
117 * @return string HTML
119 protected function renderResultSet( SearchResultSet
$resultSet, $offset ) {
121 foreach ( $resultSet as $result ) {
122 $hits[] = $this->resultWidget
->render( $result, $offset++
);
125 return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";