Merge "Special:Newpages feed now shows first revision instead of latest revision"
[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
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>";
84 }
85
86 /**
87 * Generates an appropriate HTML header for the given interwiki prefix
88 *
89 * @param string $iwPrefix Interwiki prefix of wiki to show header for
90 * @param string $term User provided search term
91 * @return string HTML
92 */
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();
100 }
101
102 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
103 [ 'search' => $term, 'fulltext' => 1 ]
104 );
105 $searchLink = Html::rawElement(
106 'a',
107 [ 'href' => $href ],
108 $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
109 );
110
111 return
112 "<div class='mw-search-interwiki-project'>" .
113 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
114 $caption .
115 "</div>";
116 }
117
118 protected function loadCustomCaptions() {
119 if ( $this->customCaptions !== null ) {
120 return;
121 }
122
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];
129 }
130 }
131 }
132 }