]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/widget/search/InterwikiSearchResultSetWidget.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / widget / search / InterwikiSearchResultSetWidget.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 use OOUI;
12
13 /**
14  * Renders one or more SearchResultSets into a sidebar grouped by
15  * interwiki prefix. Includes a per-wiki header indicating where
16  * the results are from.
17  */
18 class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
19         /** @var SpecialSearch */
20         protected $specialSearch;
21         /** @var SearchResultWidget */
22         protected $resultWidget;
23         /** @var string[]|null */
24         protected $customCaptions;
25         /** @var LinkRenderer */
26         protected $linkRenderer;
27         /** @var InterwikiLookup */
28         protected $iwLookup;
29         /** @var $output */
30         protected $output;
31         /** @var bool $showMultimedia */
32         protected $showMultimedia;
33
34         public function __construct(
35                 SpecialSearch $specialSearch,
36                 SearchResultWidget $resultWidget,
37                 LinkRenderer $linkRenderer,
38                 InterwikiLookup $iwLookup,
39                 $showMultimedia = false
40         ) {
41                 $this->specialSearch = $specialSearch;
42                 $this->resultWidget = $resultWidget;
43                 $this->linkRenderer = $linkRenderer;
44                 $this->iwLookup = $iwLookup;
45                 $this->output = $specialSearch->getOutput();
46                 $this->showMultimedia = $showMultimedia;
47         }
48         /**
49          * @param string $term User provided search term
50          * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
51          *  results to render.
52          * @return string HTML
53          */
54         public function render( $term, $resultSets ) {
55                 if ( !is_array( $resultSets ) ) {
56                         $resultSets = [ $resultSets ];
57                 }
58
59                 $this->loadCustomCaptions();
60
61                 if ( $this->showMultimedia ) {
62                         $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
63                 }
64                 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
65
66                 $iwResults = [];
67                 foreach ( $resultSets as $resultSet ) {
68                         $result = $resultSet->next();
69                         while ( $result ) {
70                                 if ( !$result->isBrokenTitle() ) {
71                                         $iwResults[$result->getTitle()->getInterwiki()][] = $result;
72                                 }
73                                 $result = $resultSet->next();
74                         }
75                 }
76
77                 $iwResultSetPos = 1;
78                 $iwResultListOutput = '';
79
80                 foreach ( $iwResults as $iwPrefix => $results ) {
81                         // TODO: Assumes interwiki results are never paginated
82                         $position = 0;
83                         $iwResultItemOutput = '';
84
85                         foreach ( $results as $result ) {
86                                 $iwResultItemOutput .= $this->resultWidget->render( $result, $term, $position++ );
87                         }
88
89                         $footerHtml = $this->footerHtml( $term, $iwPrefix );
90                         $iwResultListOutput .= Html::rawElement( 'li',
91                                 [
92                                         'class' => 'iw-resultset',
93                                         'data-iw-resultset-pos' => $iwResultSetPos,
94                                         'data-iw-resultset-source' => $iwPrefix
95                                 ],
96
97                                 $iwResultItemOutput .
98                                 $footerHtml
99                         );
100
101                         $iwResultSetPos++;
102                 }
103
104                 return Html::rawElement(
105                         'div',
106                         [ 'id' => 'mw-interwiki-results' ],
107                         Html::rawElement(
108                                 'p',
109                                 [ 'class' => 'iw-headline' ],
110                                 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
111                         ) .
112                         Html::rawElement(
113                                 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
114                         )
115                 );
116         }
117
118         /**
119          * Generates an HTML footer for the given interwiki prefix
120          *
121          * @param string $term User provided search term
122          * @param string $iwPrefix Interwiki prefix of wiki to show footer for
123          * @return string HTML
124          */
125         protected function footerHtml( $term, $iwPrefix ) {
126                 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
127                         [ 'search' => $term, 'fulltext' => 1 ]
128                 );
129
130                 $interwiki = $this->iwLookup->fetch( $iwPrefix );
131                 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
132
133                 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
134                         $caption = $this->customCaptions[$iwPrefix];
135                 } else {
136                         $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
137                 }
138
139                 $searchLink = Html::rawElement( 'em', null,
140                         Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
141                 );
142
143                 return Html::rawElement( 'div',
144                         [ 'class' => 'iw-result__footer' ],
145                         $this->iwIcon( $iwPrefix ) . $searchLink );
146         }
147
148         protected function loadCustomCaptions() {
149                 if ( $this->customCaptions !== null ) {
150                         return;
151                 }
152
153                 $this->customCaptions = [];
154                 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
155                 foreach ( $customLines as $line ) {
156                         $parts = explode( ':', $line, 2 );
157                         if ( count( $parts ) === 2 ) {
158                                 $this->customCaptions[$parts[0]] = $parts[1];
159                         }
160                 }
161         }
162
163         /**
164          * Generates a custom OOUI icon element with a favicon as the image.
165          * The favicon image URL is generated by parsing the interwiki URL
166          * and returning the default location of the favicon for that domain,
167          * which is assumed to be '/favicon.ico'.
168          *
169          * @param string $iwPrefix Interwiki prefix
170          * @return OOUI\IconWidget
171          **/
172         protected function iwIcon( $iwPrefix ) {
173                 $interwiki = $this->iwLookup->fetch( $iwPrefix );
174                 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
175
176                 $iwIconUrl = $parsed['scheme'] .
177                         $parsed['delimiter'] .
178                         $parsed['host'] .
179                         ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
180                         '/favicon.ico';
181
182                 $iwIcon = new OOUI\IconWidget( [
183                         'icon' => 'favicon'
184                  ] );
185
186                  $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
187
188                 return $iwIcon;
189         }
190 }