MediaWiki  master
BasicSearchResultSetWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
7 use Message;
8 use SpecialSearch;
9 use Status;
10 
17  protected $specialPage;
19  protected $resultWidget;
21  protected $sidebarWidget;
22 
23  public function __construct(
27  ) {
28  $this->specialPage = $specialPage;
29  $this->resultWidget = $resultWidget;
30  $this->sidebarWidget = $sidebarWidget;
31  }
32 
40  public function render(
41  $term,
42  $offset,
43  ISearchResultSet $titleResultSet = null,
44  ISearchResultSet $textResultSet = null
45  ) {
46  $hasTitle = $titleResultSet ? $titleResultSet->numRows() > 0 : false;
47  $hasText = $textResultSet ? $textResultSet->numRows() > 0 : false;
48  $hasSecondary = $textResultSet
49  ? $textResultSet->hasInterwikiResults( ISearchResultSet::SECONDARY_RESULTS )
50  : false;
51  $hasSecondaryInline = $textResultSet
52  ? $textResultSet->hasInterwikiResults( ISearchResultSet::INLINE_RESULTS )
53  : false;
54 
55  if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) {
56  return '';
57  }
58 
59  $out = '<div class="mw-search-results-container">';
60 
61  if ( $hasTitle ) {
62  $out .= $this->header( $this->specialPage->msg( 'titlematches' ) )
63  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable titleResultSet is set when used here
64  . $this->renderResultSet( $titleResultSet, $offset );
65  }
66 
67  if ( $hasText ) {
68  if ( $hasTitle ) {
69  $out .= "<div class='mw-search-visualclear'></div>" .
70  $this->header( $this->specialPage->msg( 'textmatches' ) );
71  }
72  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable textResultSet is set when used
73  $out .= $this->renderResultSet( $textResultSet, $offset );
74  }
75 
76  if ( $hasSecondaryInline ) {
77  $iwResults = $textResultSet->getInterwikiResults( ISearchResultSet::INLINE_RESULTS );
78  foreach ( $iwResults as $interwiki => $results ) {
79  if ( $results instanceof Status || $results->numRows() === 0 ) {
80  // ignore bad interwikis for now
81  continue;
82  }
83  $out .=
84  "<h2 class='mw-search-interwiki-header mw-search-visualclear'>" .
85  $this->specialPage->msg( "search-interwiki-results-{$interwiki}" )->parse() .
86  "</h2>";
87  $out .=
88  "<div class='mw-search-interwiki-results'>" .
89  $this->renderResultSet( $results, $offset ) .
90  "</div>";
91  }
92  }
93 
94  // Close <div class='mw-search-results-container'>
95  $out .= '</div>';
96 
97  if ( $hasSecondary ) {
98  $out .= $this->sidebarWidget->render(
99  $term,
100  $textResultSet->getInterwikiResults( ISearchResultSet::SECONDARY_RESULTS )
101  );
102  }
103 
104  // Convert the whole thing to desired language variant
105  // TODO: Move this up to Special:Search?
106  $converter = MediaWikiServices::getInstance()->getLanguageConverterFactory()
107  ->getLanguageConverter();
108  return $converter->convert( $out );
109  }
110 
119  protected function header( Message $msg ) {
120  return "<h2>" .
121  "<span class='mw-headline'>" . $msg->escaped() . "</span>" .
122  "</h2>";
123  }
124 
130  protected function renderResultSet( ISearchResultSet $resultSet, $offset ) {
131  $hits = [];
132  foreach ( $resultSet as $result ) {
133  $hits[] = $this->resultWidget->render( $result, $offset++ );
134  }
135 
136  return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";
137  }
138 }
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
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)
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition: Message.php:142
escaped()
Returns the message text.
Definition: Message.php:1085
implements Special:Search - Run text & title search and display the output
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:45
A set of SearchEngine results.
const INLINE_RESULTS
Identifier for interwiki results that can be displayed even if no existing main wiki results exist.
const SECONDARY_RESULTS
Identifier for interwiki results that are displayed only together with existing main wiki results.
Renders a single search result to HTML.