3 namespace MediaWiki\Widget\Search
;
11 * Renders the search result area. Handles Title and Full-Text search results,
12 * along with inline and sidebar secondary (interwiki) results.
14 class BasicSearchResultSetWidget
{
15 /** @var SpecialSearch */
16 protected $specialPage;
17 /** @var SearchResultWidget */
18 protected $resultWidget;
19 /** @var InterwikiSearchResultSetWidget */
20 protected $sidebarWidget;
22 public function __construct(
23 SpecialSearch
$specialPage,
24 SearchResultWidget
$resultWidget,
25 SearchResultSetWidget
$sidebarWidget
27 $this->specialPage
= $specialPage;
28 $this->resultWidget
= $resultWidget;
29 $this->sidebarWidget
= $sidebarWidget;
33 * @param string $term The search term to highlight
34 * @param int $offset The offset of the first result in the result set
35 * @param SearchResultSet|null $titleResultSet Results of searching only page titles
36 * @param SearchResultSet|null $textResultSet Results of general full text search.
39 public function render(
42 SearchResultSet
$titleResultSet = null,
43 SearchResultSet
$textResultSet = null
47 $hasTitle = $titleResultSet ?
$titleResultSet->numRows() > 0 : false;
48 $hasText = $textResultSet ?
$textResultSet->numRows() > 0 : false;
49 $hasSecondary = $textResultSet
50 ?
$textResultSet->hasInterwikiResults( SearchResultSet
::SECONDARY_RESULTS
)
52 $hasSecondaryInline = $textResultSet
53 ?
$textResultSet->hasInterwikiResults( SearchResultSet
::INLINE_RESULTS
)
56 if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) {
62 $out .= $this->header( $this->specialPage
->msg( 'titlematches' ) )
63 . $this->renderResultSet( $titleResultSet, $offset );
68 $out .= "<div class='mw-search-visualclear'></div>" .
69 $this->header( $this->specialPage
->msg( 'textmatches' ) );
71 $out .= $this->renderResultSet( $textResultSet, $offset );
74 if ( $hasSecondaryInline ) {
75 $iwResults = $textResultSet->getInterwikiResults( SearchResultSet
::INLINE_RESULTS
);
76 foreach ( $iwResults as $interwiki => $results ) {
77 if ( $results instanceof Status ||
$results->numRows() === 0 ) {
78 // ignore bad interwikis for now
82 "<p class='mw-search-interwiki-header mw-search-visualclear'>" .
83 $this->specialPage
->msg( "search-interwiki-results-{$interwiki}" )->parse() .
85 $out .= $this->renderResultSet( $results, $offset );
89 if ( $hasSecondary ) {
90 $out .= $this->sidebarWidget
->render(
92 $textResultSet->getInterwikiResults( SearchResultSet
::SECONDARY_RESULTS
)
96 // Convert the whole thing to desired language variant
97 // TODO: Move this up to Special:Search?
98 return $wgContLang->convert( $out );
102 * Generate a headline for a section of the search results. In prior
103 * implementations this was rendering wikitext of '==$1==', but seems
104 * a waste to call the full parser to generate this tiny bit of html
106 * @param Message $msg i18n message to use as header
107 * @return string HTML
109 protected function header( Message
$msg ) {
112 "<span class='mw-headline'>" . $msg->escaped() . "</span>" .
117 * @param SearchResultSet $resultSet The search results to render
118 * @param int $offset Offset of the first result in $resultSet
119 * @return string HTML
121 protected function renderResultSet( SearchResultSet
$resultSet, $offset ) {
124 $terms = $wgContLang->convertForSearchResult( $resultSet->termMatches() );
127 $result = $resultSet->next();
129 $hits[] .= $this->resultWidget
->render( $result, $terms, $offset++
);
130 $result = $resultSet->next();
133 return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";