Move ILoadBalancer to Rdbms namespace
[mediawiki.git] / includes / widget / search / SimpleSearchResultSetWidget.php
blob04e1e2100f2f87bdf795066093e6fa767a5645c3
1 <?php
3 namespace MediaWiki\Widget\Search;
5 use MediaWiki\Interwiki\InterwikiLookup;
6 use MediaWiki\Linker\LinkRenderer;
7 use SearchResultSet;
8 use SpecialSearch;
9 use Title;
10 use Html;
12 /**
13 * Renders one or more SearchResultSets into a sidebar grouped by
14 * interwiki prefix. Includes a per-wiki header indicating where
15 * the results are from.
17 class SimpleSearchResultSetWidget implements SearchResultSetWidget{
18 /** @var SpecialSearch */
19 protected $specialSearch;
20 /** @var SearchResultWidget */
21 protected $resultWidget;
22 /** @var string[]|null */
23 protected $customCaptions;
24 /** @var LinkRenderer */
25 protected $linkRenderer;
26 /** @var InterwikiLookup */
27 protected $iwLookup;
29 public function __construct(
30 SpecialSearch $specialSearch,
31 SearchResultWidget $resultWidget,
32 LinkRenderer $linkRenderer,
33 InterwikiLookup $iwLookup
34 ) {
35 $this->specialSearch = $specialSearch;
36 $this->resultWidget = $resultWidget;
37 $this->linkRenderer = $linkRenderer;
38 $this->iwLookup = $iwLookup;
41 /**
42 * @param string $term User provided search term
43 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
44 * results to render.
45 * @return string HTML
47 public function render( $term, $resultSets ) {
48 if ( !is_array( $resultSets ) ) {
49 $resultSets = [ $resultSets ];
52 $this->loadCustomCaptions();
54 $iwResults = [];
55 foreach ( $resultSets as $resultSet ) {
56 $result = $resultSet->next();
57 while ( $result ) {
58 if ( !$result->isBrokenTitle() ) {
59 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
61 $result = $resultSet->next();
65 $out = '';
66 foreach ( $iwResults as $iwPrefix => $results ) {
67 $out .= $this->headerHtml( $iwPrefix, $term );
68 $out .= "<ul class='mw-search-iwresults'>";
69 // TODO: Assumes interwiki results are never paginated
70 $position = 0;
71 foreach ( $results as $result ) {
72 $out .= $this->resultWidget->render( $result, $term, $position++ );
74 $out .= "</ul>";
77 return
78 "<div id='mw-search-interwiki'>" .
79 "<div id='mw-search-interwiki-caption'>" .
80 $this->specialSearch->msg( 'search-interwiki-caption' )->parse() .
81 '</div>' .
82 $out .
83 "</div>";
86 /**
87 * Generates an appropriate HTML header for the given interwiki prefix
89 * @param string $iwPrefix Interwiki prefix of wiki to show header for
90 * @param string $term User provided search term
91 * @return string HTML
93 protected function headerHtml( $iwPrefix, $term ) {
94 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
95 $caption = $this->customCaptions[$iwPrefix];
96 } else {
97 $interwiki = $this->iwLookup->fetch( $iwPrefix );
98 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
99 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
102 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
103 [ 'search' => $term, 'fulltext' => 1 ]
105 $searchLink = Html::rawElement(
106 'a',
107 [ 'href' => $href ],
108 $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
111 return
112 "<div class='mw-search-interwiki-project'>" .
113 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
114 $caption .
115 "</div>";
118 protected function loadCustomCaptions() {
119 if ( $this->customCaptions !== null ) {
120 return;
123 $this->customCaptions = [];
124 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
125 foreach ( $customLines as $line ) {
126 $parts = explode( ':', $line, 2 );
127 if ( count( $parts ) === 2 ) {
128 $this->customCaptions[$parts[0]] = $parts[1];