Merge "Add semantic tags to license info text"
[lhc/web/wiklou.git] / includes / widget / search / SimpleSearchResultSetWidget.php
1 <?php
2
3 namespace MediaWiki\Widget\Search;
4
5 use MediaWiki\Interwiki\InterwikiLookup;
6 use MediaWiki\Linker\LinkRenderer;
7 use SearchResultSet;
8 use SpecialSearch;
9 use Title;
10 use Html;
11
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.
16 */
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;
28
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;
39 }
40
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
46 */
47 public function render( $term, $resultSets ) {
48 if ( !is_array( $resultSets ) ) {
49 $resultSets = [ $resultSets ];
50 }
51
52 $this->loadCustomCaptions();
53
54 $iwResults = [];
55 foreach ( $resultSets as $resultSet ) {
56 $result = $resultSet->next();
57 while ( $result ) {
58 if ( !$result->isBrokenTitle() ) {
59 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
60 }
61 $result = $resultSet->next();
62 }
63 }
64
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++ );
73 }
74 $out .= "</ul>";
75 }
76
77 return "<div id='mw-search-interwiki'>" .
78 "<div id='mw-search-interwiki-caption'>" .
79 $this->specialSearch->msg( 'search-interwiki-caption' )->parse() .
80 '</div>' .
81 $out .
82 "</div>";
83 }
84
85 /**
86 * Generates an appropriate HTML header for the given interwiki prefix
87 *
88 * @param string $iwPrefix Interwiki prefix of wiki to show header for
89 * @param string $term User provided search term
90 * @return string HTML
91 */
92 protected function headerHtml( $iwPrefix, $term ) {
93 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
94 $caption = $this->customCaptions[$iwPrefix];
95 } else {
96 $interwiki = $this->iwLookup->fetch( $iwPrefix );
97 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
98 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
99 }
100
101 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
102 [ 'search' => $term, 'fulltext' => 1 ]
103 );
104 $searchLink = Html::rawElement(
105 'a',
106 [ 'href' => $href ],
107 $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
108 );
109
110 return "<div class='mw-search-interwiki-project'>" .
111 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
112 $caption .
113 "</div>";
114 }
115
116 protected function loadCustomCaptions() {
117 if ( $this->customCaptions !== null ) {
118 return;
119 }
120
121 $this->customCaptions = [];
122 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
123 foreach ( $customLines as $line ) {
124 $parts = explode( ':', $line, 2 );
125 if ( count( $parts ) === 2 ) {
126 $this->customCaptions[$parts[0]] = $parts[1];
127 }
128 }
129 }
130 }