Use HTML::hidden to create input fields
[lhc/web/wiklou.git] / includes / widget / search / BasicSearchResultSetWidget.php
1 <?php
2
3 namespace MediaWiki\Widget\Search;
4
5 use Message;
6 use SearchResultSet;
7 use SpecialSearch;
8 use Status;
9
10 /**
11 * Renders the search result area. Handles Title and Full-Text search results,
12 * along with inline and sidebar secondary (interwiki) results.
13 */
14 class BasicSearchResultSetWidget {
15 /** @var SpecialSearch */
16 protected $specialPage;
17 /** @var SearchResultWidget */
18 protected $resultWidget;
19 /** @var InterwikiSearchResultSetWidget */
20 protected $sidebarWidget;
21
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;
30 }
31
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
38 */
39 public function render(
40 $term,
41 $offset,
42 SearchResultSet $titleResultSet = null,
43 SearchResultSet $textResultSet = null
44 ) {
45 global $wgContLang;
46
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;
55
56 if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) {
57 return '';
58 }
59
60 $out = '';
61 if ( $hasTitle ) {
62 $out .= $this->header( $this->specialPage->msg( 'titlematches' ) )
63 . $this->renderResultSet( $titleResultSet, $offset );
64 }
65
66 if ( $hasText ) {
67 if ( $hasTitle ) {
68 $out .= "<div class='mw-search-visualclear'></div>" .
69 $this->header( $this->specialPage->msg( 'textmatches' ) );
70 }
71 $out .= $this->renderResultSet( $textResultSet, $offset );
72 }
73
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;
80 }
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 );
86 }
87 }
88
89 if ( $hasSecondary ) {
90 $out .= $this->sidebarWidget->render(
91 $term,
92 $textResultSet->getInterwikiResults( SearchResultSet::SECONDARY_RESULTS )
93 );
94 }
95
96 // Convert the whole thing to desired language variant
97 // TODO: Move this up to Special:Search?
98 return $wgContLang->convert( $out );
99 }
100
101 /**
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
105 *
106 * @param Message $msg i18n message to use as header
107 * @return string HTML
108 */
109 protected function header( Message $msg ) {
110 return
111 "<h2>" .
112 "<span class='mw-headline'>" . $msg->escaped() . "</span>" .
113 "</h2>";
114 }
115
116 /**
117 * @param SearchResultSet $resultSet The search results to render
118 * @param int $offset Offset of the first result in $resultSet
119 * @return string HTML
120 */
121 protected function renderResultSet( SearchResultSet $resultSet, $offset ) {
122 global $wgContLang;
123
124 $terms = $wgContLang->convertForSearchResult( $resultSet->termMatches() );
125
126 $hits = [];
127 $result = $resultSet->next();
128 while ( $result ) {
129 $hits[] .= $this->resultWidget->render( $result, $terms, $offset++ );
130 $result = $resultSet->next();
131 }
132
133 return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";
134 }
135 }