MediaWiki  1.34.0
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 = '';
60  if ( $hasTitle ) {
61  $out .= $this->header( $this->specialPage->msg( 'titlematches' ) )
62  . $this->renderResultSet( $titleResultSet, $offset );
63  }
64 
65  if ( $hasText ) {
66  if ( $hasTitle ) {
67  $out .= "<div class='mw-search-visualclear'></div>" .
68  $this->header( $this->specialPage->msg( 'textmatches' ) );
69  }
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 .= $this->renderResultSet( $results, $offset );
85  }
86  }
87 
88  if ( $hasSecondary ) {
89  $out .= $this->sidebarWidget->render(
90  $term,
91  $textResultSet->getInterwikiResults( ISearchResultSet::SECONDARY_RESULTS )
92  );
93  }
94 
95  // Convert the whole thing to desired language variant
96  // TODO: Move this up to Special:Search?
97  return MediaWikiServices::getInstance()->getContentLanguage()->convert( $out );
98  }
99 
108  protected function header( Message $msg ) {
109  return "<h2>" .
110  "<span class='mw-headline'>" . $msg->escaped() . "</span>" .
111  "</h2>";
112  }
113 
119  protected function renderResultSet( ISearchResultSet $resultSet, $offset ) {
120  $hits = [];
121  foreach ( $resultSet as $result ) {
122  $hits[] = $this->resultWidget->render( $result, $offset++ );
123  }
124 
125  return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>";
126  }
127 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:117
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
MediaWiki\Widget\Search\BasicSearchResultSetWidget\$sidebarWidget
InterwikiSearchResultSetWidget $sidebarWidget
Definition: BasicSearchResultSetWidget.php:21
Message
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
Definition: InterwikiSearchResultSetWidget.php:18
MediaWiki\Widget\Search\BasicSearchResultSetWidget
Renders the search result area.
Definition: BasicSearchResultSetWidget.php:15
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:40
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:138
MediaWiki\Widget\Search\SearchResultSetWidget
Renders a set of search results to HTML.
Definition: SearchResultSetWidget.php:10
MediaWiki\Widget\Search\BasicSearchResultSetWidget\__construct
__construct(SpecialSearch $specialPage, SearchResultWidget $resultWidget, SearchResultSetWidget $sidebarWidget)
Definition: BasicSearchResultSetWidget.php:23
MediaWiki\Widget\Search\SearchResultWidget
Renders a single search result to HTML.
Definition: SearchResultWidget.php:10
MediaWiki\Widget\Search\BasicSearchResultSetWidget\$specialPage
SpecialSearch $specialPage
Definition: BasicSearchResultSetWidget.php:17
MediaWiki\Widget\Search\BasicSearchResultSetWidget\header
header(Message $msg)
Generate a headline for a section of the search results.
Definition: BasicSearchResultSetWidget.php:108
ISearchResultSet
A set of SearchEngine results.
Definition: ISearchResultSet.php:11
ISearchResultSet\INLINE_RESULTS
const INLINE_RESULTS
Identifier for interwiki results that can be displayed even if no existing main wiki results exist.
Definition: ISearchResultSet.php:22
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:38
MediaWiki\Widget\Search\BasicSearchResultSetWidget\$resultWidget
SearchResultWidget $resultWidget
Definition: BasicSearchResultSetWidget.php:19
MediaWiki\Widget\Search\BasicSearchResultSetWidget\render
render( $term, $offset, ISearchResultSet $titleResultSet=null, ISearchResultSet $textResultSet=null)
Definition: BasicSearchResultSetWidget.php:40
ISearchResultSet\SECONDARY_RESULTS
const SECONDARY_RESULTS
Identifier for interwiki results that are displayed only together with existing main wiki results.
Definition: ISearchResultSet.php:16
MediaWiki\Widget\Search\BasicSearchResultSetWidget\renderResultSet
renderResultSet(ISearchResultSet $resultSet, $offset)
Definition: BasicSearchResultSetWidget.php:119