Move ILoadBalancer to Rdbms namespace
[mediawiki.git] / includes / widget / search / BasicSearchResultSetWidget.php
blob07094afca693eaf97351c4200a367d41332dcafb
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use Message;
6 use SearchResultSet;
7 use SpecialSearch;
8 use Status;
10 /**
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
26 ) {
27 $this->specialPage = $specialPage;
28 $this->resultWidget = $resultWidget;
29 $this->sidebarWidget = $sidebarWidget;
32 /**
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.
37 * @return string HTML
39 public function render(
40 $term,
41 $offset,
42 SearchResultSet $titleResultSet = null,
43 SearchResultSet $textResultSet = null
44 ) {
45 global $wgContLang;
47 $hasTitle = $titleResultSet ? $titleResultSet->numRows() > 0 : false;
48 $hasText = $textResultSet ? $textResultSet->numRows() > 0 : false;
49 $hasSecondary = $textResultSet
50 ? $textResultSet->hasInterwikiResults( SearchResultSet::SECONDARY_RESULTS )
51 : false;
52 $hasSecondaryInline = $textResultSet
53 ? $textResultSet->hasInterwikiResults( SearchResultSet::INLINE_RESULTS )
54 : false;
56 if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) {
57 return '';
60 $out = '';
61 if ( $hasTitle ) {
62 $out .= $this->header( $this->specialPage->msg( 'titlematches' ) )
63 . $this->renderResultSet( $titleResultSet, $offset );
66 if ( $hasText ) {
67 if ( $hasTitle ) {
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
79 continue;
81 $out .=
82 "<p class='mw-search-interwiki-header mw-search-visualclear'>" .
83 $this->specialPage->msg( "search-interwiki-results-{$interwiki}" )->parse() .
84 "</p>";
85 $out .= $this->renderResultSet( $results, $offset );
89 if ( $hasSecondary ) {
90 $out .= $this->sidebarWidget->render(
91 $term,
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 ) {
110 return
111 "<h2>" .
112 "<span class='mw-headline'>" . $msg->escaped() . "</span>" .
113 "</h2>";
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 ) {
122 global $wgContLang;
124 $terms = $wgContLang->convertForSearchResult( $resultSet->termMatches() );
126 $hits = [];
127 $result = $resultSet->next();
128 while ( $result ) {
129 $hits[] .= $this->resultWidget->render( $result, $terms, $offset++ );
130 $result = $resultSet->next();
133 return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";