MediaWiki master
BasicSearchResultSetWidget.php
Go to the documentation of this file.
1<?php
2
4
10
19
20 public function __construct(
24 ) {
25 $this->specialPage = $specialPage;
26 $this->resultWidget = $resultWidget;
27 $this->sidebarWidget = $sidebarWidget;
28 }
29
37 public function render(
38 $term,
39 $offset,
40 ?ISearchResultSet $titleResultSet = null,
41 ?ISearchResultSet $textResultSet = null
42 ) {
43 $hasTitle = $titleResultSet && $titleResultSet->numRows() > 0;
44 $hasText = $textResultSet && $textResultSet->numRows() > 0;
45 $hasSecondary =
46 $textResultSet &&
47 $textResultSet->hasInterwikiResults( ISearchResultSet::SECONDARY_RESULTS );
48 $hasSecondaryInline =
49 $textResultSet &&
50 $textResultSet->hasInterwikiResults( ISearchResultSet::INLINE_RESULTS );
51
52 if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) {
53 return '';
54 }
55
56 $out = '<div class="mw-search-results-container">';
57
58 if ( $hasTitle ) {
59 $out .= $this->header( $this->specialPage->msg( 'titlematches' ) )
60 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable titleResultSet is set when used here
61 . $this->renderResultSet( $titleResultSet, $offset );
62 }
63
64 if ( $hasText ) {
65 if ( $hasTitle ) {
66 $out .= "<div class='mw-search-visualclear'></div>" .
67 $this->header( $this->specialPage->msg( 'textmatches' ) );
68 }
69 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable textResultSet is set when used
70 $out .= $this->renderResultSet( $textResultSet, $offset );
71 }
72
73 if ( $hasSecondaryInline ) {
74 $iwResults = $textResultSet->getInterwikiResults( ISearchResultSet::INLINE_RESULTS );
75 foreach ( $iwResults as $interwiki => $results ) {
76 if ( $results instanceof Status || $results->numRows() === 0 ) {
77 // ignore bad interwikis for now
78 continue;
79 }
80 $out .=
81 "<h2 class='mw-search-interwiki-header mw-search-visualclear'>" .
82 $this->specialPage->msg( "search-interwiki-results-{$interwiki}" )->parse() .
83 "</h2>";
84 $out .=
85 "<div class='mw-search-interwiki-results'>" .
86 $this->renderResultSet( $results, $offset ) .
87 "</div>";
88 }
89 }
90
91 // Close <div class='mw-search-results-container'>
92 $out .= '</div>';
93
94 if ( $hasSecondary ) {
95 $out .= $this->sidebarWidget->render(
96 $term,
97 $textResultSet->getInterwikiResults( ISearchResultSet::SECONDARY_RESULTS )
98 );
99 }
100
101 // Convert the whole thing to desired language variant
102 // TODO: Move this up to Special:Search?
103 $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
104 ->getLanguageConverter();
105 return $converter->convert( $out );
106 }
107
116 protected function header( Message $msg ) {
117 return "<h2>" . $msg->escaped() . "</h2>";
118 }
119
125 protected function renderResultSet( ISearchResultSet $resultSet, $offset ) {
126 $hits = [];
127 foreach ( $resultSet as $result ) {
128 $hits[] = $this->resultWidget->render( $result, $offset++ );
129 }
130
131 return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";
132 }
133}
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:155
escaped()
Returns the message text.
Definition Message.php:1139
header(Message $msg)
Generate a headline for a section of the search results.
__construct(SpecialSearch $specialPage, SearchResultWidget $resultWidget, SearchResultSetWidget $sidebarWidget)
render( $term, $offset, ?ISearchResultSet $titleResultSet=null, ?ISearchResultSet $textResultSet=null)
Run text & title search and display the output.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
A set of SearchEngine results.
Renders a single search result to HTML.