Merge "Warn if stateful ParserOutput transforms are used"
[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 * @deprecated since 1.31. Use InterwikiSearchResultSetWidget
18 */
19 class SimpleSearchResultSetWidget implements SearchResultSetWidget {
20 /** @var SpecialSearch */
21 protected $specialSearch;
22 /** @var SearchResultWidget */
23 protected $resultWidget;
24 /** @var string[]|null */
25 protected $customCaptions;
26 /** @var LinkRenderer */
27 protected $linkRenderer;
28 /** @var InterwikiLookup */
29 protected $iwLookup;
30
31 public function __construct(
32 SpecialSearch $specialSearch,
33 SearchResultWidget $resultWidget,
34 LinkRenderer $linkRenderer,
35 InterwikiLookup $iwLookup
36 ) {
37 wfDeprecated( __METHOD__, '1.31' );
38 $this->specialSearch = $specialSearch;
39 $this->resultWidget = $resultWidget;
40 $this->linkRenderer = $linkRenderer;
41 $this->iwLookup = $iwLookup;
42 }
43
44 /**
45 * @param string $term User provided search term
46 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
47 * results to render.
48 * @return string HTML
49 */
50 public function render( $term, $resultSets ) {
51 if ( !is_array( $resultSets ) ) {
52 $resultSets = [ $resultSets ];
53 }
54
55 $this->loadCustomCaptions();
56
57 $iwResults = [];
58 foreach ( $resultSets as $resultSet ) {
59 $result = $resultSet->next();
60 while ( $result ) {
61 if ( !$result->isBrokenTitle() ) {
62 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
63 }
64 $result = $resultSet->next();
65 }
66 }
67
68 $out = '';
69 foreach ( $iwResults as $iwPrefix => $results ) {
70 $out .= $this->headerHtml( $iwPrefix, $term );
71 $out .= "<ul class='mw-search-iwresults'>";
72 // TODO: Assumes interwiki results are never paginated
73 $position = 0;
74 foreach ( $results as $result ) {
75 $out .= $this->resultWidget->render( $result, $term, $position++ );
76 }
77 $out .= "</ul>";
78 }
79
80 return "<div id='mw-search-interwiki'>" .
81 "<div id='mw-search-interwiki-caption'>" .
82 $this->specialSearch->msg( 'search-interwiki-caption' )->parse() .
83 '</div>' .
84 $out .
85 "</div>";
86 }
87
88 /**
89 * Generates an appropriate HTML header for the given interwiki prefix
90 *
91 * @param string $iwPrefix Interwiki prefix of wiki to show header for
92 * @param string $term User provided search term
93 * @return string HTML
94 */
95 protected function headerHtml( $iwPrefix, $term ) {
96 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
97 $caption = $this->customCaptions[$iwPrefix];
98 } else {
99 $interwiki = $this->iwLookup->fetch( $iwPrefix );
100 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
101 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
102 }
103
104 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
105 [ 'search' => $term, 'fulltext' => 1 ]
106 );
107 $searchLink = Html::rawElement(
108 'a',
109 [ 'href' => $href ],
110 $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
111 );
112
113 return "<div class='mw-search-interwiki-project'>" .
114 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
115 $caption .
116 "</div>";
117 }
118
119 protected function loadCustomCaptions() {
120 if ( $this->customCaptions !== null ) {
121 return;
122 }
123
124 $this->customCaptions = [];
125 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
126 foreach ( $customLines as $line ) {
127 $parts = explode( ':', $line, 2 );
128 if ( count( $parts ) === 2 ) {
129 $this->customCaptions[$parts[0]] = $parts[1];
130 }
131 }
132 }
133 }