MediaWiki master
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1<?php
2
4
13use OOUI;
14
22 protected $specialSearch;
24 protected $resultWidget;
26 protected $customCaptions;
28 protected $linkRenderer;
30 protected $iwLookup;
32 protected $output;
34 protected $showMultimedia;
37
38 public function __construct(
43 $showMultimedia = false
44 ) {
45 $this->specialSearch = $specialSearch;
46 $this->resultWidget = $resultWidget;
47 $this->linkRenderer = $linkRenderer;
48 $this->iwLookup = $iwLookup;
49 $this->output = $specialSearch->getOutput();
50 $this->showMultimedia = $showMultimedia;
51 $this->iwLogoOverrides = $this->specialSearch->getConfig()->get( MainConfigNames::InterwikiLogoOverride );
52 }
53
60 public function render( $term, $resultSets ) {
61 if ( !is_array( $resultSets ) ) {
62 $resultSets = [ $resultSets ];
63 }
64
65 $this->loadCustomCaptions();
66
67 if ( $this->showMultimedia ) {
68 $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
69 }
70 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
71 $this->output->addModuleStyles( 'oojs-ui.styles.icons-wikimedia' );
72
73 $iwResults = [];
74 foreach ( $resultSets as $resultSet ) {
75 foreach ( $resultSet as $result ) {
76 if ( !$result->isBrokenTitle() ) {
77 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
78 }
79 }
80 }
81
82 $iwResultSetPos = 1;
83 $iwResultListOutput = '';
84
85 foreach ( $iwResults as $iwPrefix => $results ) {
86 // TODO: Assumes interwiki results are never paginated
87 $position = 0;
88 $iwResultItemOutput = '';
89
90 foreach ( $results as $result ) {
91 $iwResultItemOutput .= $this->resultWidget->render( $result, $position++ );
92 }
93
94 $headerHtml = $this->headerHtml( $term, $iwPrefix );
95 $footerHtml = $this->footerHtml( $term, $iwPrefix );
96 $iwResultListOutput .= Html::rawElement( 'li',
97 [
98 'class' => 'iw-resultset',
99 'data-iw-resultset-pos' => $iwResultSetPos,
100 'data-iw-resultset-source' => $iwPrefix
101 ],
102
103 $headerHtml .
104 $iwResultItemOutput .
105 $footerHtml
106 );
107 $iwResultSetPos++;
108 }
109
110 return Html::rawElement(
111 'div',
112 [ 'id' => 'mw-interwiki-results' ],
113 Html::rawElement(
114 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
115 )
116 );
117 }
118
126 protected function headerHtml( $term, $iwPrefix ) {
127 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
128 [ 'search' => $term, 'fulltext' => 1 ]
129 );
130
131 $interwiki = $this->iwLookup->fetch( $iwPrefix );
132 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
133
134 $caption = $this->customCaptions[$iwPrefix] ?? $parsed['host'];
135
136 $searchLink = Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption );
137
138 return Html::rawElement( 'div',
139 [ 'class' => 'iw-result__header' ],
140 $this->iwIcon( $iwPrefix ) . $searchLink );
141 }
142
150 protected function footerHtml( $term, $iwPrefix ) {
151 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
152 [ 'search' => $term, 'fulltext' => 1 ]
153 );
154
155 $interwiki = $this->iwLookup->fetch( $iwPrefix );
156 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
157
158 $caption = $this->specialSearch->msg( 'search-interwiki-resultset-link', $parsed['host'] )->escaped();
159
160 $searchLink = Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption );
161
162 return Html::rawElement( 'div',
163 [ 'class' => 'iw-result__footer' ],
164 $searchLink );
165 }
166
167 protected function loadCustomCaptions() {
168 if ( $this->customCaptions !== null ) {
169 return;
170 }
171
172 $this->customCaptions = [];
173 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
174 foreach ( $customLines as $line ) {
175 $parts = explode( ':', $line, 2 );
176 if ( count( $parts ) === 2 ) {
177 $this->customCaptions[$parts[0]] = $parts[1];
178 }
179 }
180 }
181
190 protected function iwIcon( $iwPrefix ) {
191 $logoName = $this->generateLogoName( $iwPrefix );
192 // If the value is an URL we use the favicon
193 if ( filter_var( $logoName, FILTER_VALIDATE_URL ) || $logoName === "/" ) {
194 return $this->generateIconFromFavicon( $logoName );
195 }
196
197 $iwIcon = new OOUI\IconWidget( [
198 'icon' => $logoName
199 ] );
200
201 return $iwIcon;
202 }
203
214 protected function generateLogoName( $prefix ) {
215 $logoOverridesKeys = array_keys( $this->iwLogoOverrides );
216 if ( in_array( $prefix, $logoOverridesKeys ) ) {
217 return $this->iwLogoOverrides[ $prefix ];
218 }
219
220 $interwiki = $this->iwLookup->fetch( $prefix );
221 return $interwiki ? $interwiki->getURL() : '/';
222 }
223
230 protected function generateIconFromFavicon( $logoUrl ) {
231 $parsed = wfParseUrl( wfExpandUrl( $logoUrl ) );
232 $iwIconUrl = $parsed['scheme'] .
233 $parsed['delimiter'] .
234 $parsed['host'] .
235 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
236 '/favicon.ico';
237
238 $iwIcon = new OOUI\IconWidget( [
239 'icon' => 'favicon'
240 ] );
241
242 return $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
243 }
244}
const NS_SPECIAL
Definition Defines.php:54
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL using $wgServer (or one of its alternatives).
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Class that generates HTML for internal links.
A class containing constants representing the names of configuration variables.
const InterwikiLogoOverride
Name constant for the InterwikiLogoOverride setting, for use with Config::get()
This is one of the Core classes and should be read at least once by any new developers.
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
generateIconFromFavicon( $logoUrl)
Fetches the favicon of the provided URL.
headerHtml( $term, $iwPrefix)
Generates an HTML header for the given interwiki prefix.
footerHtml( $term, $iwPrefix)
Generates an HTML footer for the given interwiki prefix.
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup, $showMultimedia=false)
generateLogoName( $prefix)
Generates the logo name used to render the interwiki icon.
getOutput()
Get the OutputPage being used for this instance.
Run text & title search and display the output.
Represents a title within MediaWiki.
Definition Title.php:79
A set of SearchEngine results.
Service interface for looking up Interwiki records.
Renders a single search result to HTML.