MediaWiki master
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1<?php
2
4
13use OOUI;
14
26
28 protected $showMultimedia;
31
33 protected $customCaptions;
34
35 public function __construct(
40 $showMultimedia = false
41 ) {
42 $this->specialSearch = $specialSearch;
43 $this->resultWidget = $resultWidget;
44 $this->linkRenderer = $linkRenderer;
45 $this->iwLookup = $iwLookup;
46 $this->output = $specialSearch->getOutput();
47 $this->showMultimedia = $showMultimedia;
48 $this->iwLogoOverrides = $this->specialSearch->getConfig()->get( MainConfigNames::InterwikiLogoOverride );
49 }
50
57 public function render( $term, $resultSets ) {
58 if ( !is_array( $resultSets ) ) {
59 $resultSets = [ $resultSets ];
60 }
61
62 $this->loadCustomCaptions();
63
64 if ( $this->showMultimedia ) {
65 $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
66 }
67 $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
68 $this->output->addModuleStyles( 'oojs-ui.styles.icons-wikimedia' );
69
70 $iwResults = [];
71 foreach ( $resultSets as $resultSet ) {
72 foreach ( $resultSet as $result ) {
73 if ( !$result->isBrokenTitle() ) {
74 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
75 }
76 }
77 }
78
79 $iwResultSetPos = 1;
80 $iwResultListOutput = '';
81
82 foreach ( $iwResults as $iwPrefix => $results ) {
83 // TODO: Assumes interwiki results are never paginated
84 $position = 0;
85 $iwResultItemOutput = '';
86
87 foreach ( $results as $result ) {
88 $iwResultItemOutput .= $this->resultWidget->render( $result, $position++ );
89 }
90
91 $headerHtml = $this->headerHtml( $term, $iwPrefix );
92 $footerHtml = $this->footerHtml( $term, $iwPrefix );
93 $iwResultListOutput .= Html::rawElement( 'li',
94 [
95 'class' => 'iw-resultset',
96 'data-iw-resultset-pos' => $iwResultSetPos,
97 'data-iw-resultset-source' => $iwPrefix
98 ],
99
100 $headerHtml .
101 $iwResultItemOutput .
102 $footerHtml
103 );
104 $iwResultSetPos++;
105 }
106
107 return Html::rawElement(
108 'div',
109 [ 'id' => 'mw-interwiki-results' ],
110 Html::rawElement(
111 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
112 )
113 );
114 }
115
123 protected function headerHtml( $term, $iwPrefix ) {
124 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
125 [ 'search' => $term, 'fulltext' => 1 ]
126 );
127
128 $interwiki = $this->iwLookup->fetch( $iwPrefix );
129 // This is false if the lookup fails, or if the other wiki is on the same
130 // domain name (i.e. /en-wiki/ and /de-wiki/)
131 $iwHost = $interwiki ? parse_url( $interwiki->getURL(), PHP_URL_HOST ) : false;
132
133 $captionText = $this->customCaptions[$iwPrefix] ?? $iwHost ?: $iwPrefix;
134 $searchLink = Html::element( 'a', [ 'href' => $href, 'target' => '_blank' ], $captionText );
135
136 return Html::rawElement( 'div',
137 [ 'class' => 'iw-result__header' ],
138 $this->iwIcon( $iwPrefix ) . $searchLink );
139 }
140
148 protected function footerHtml( $term, $iwPrefix ) {
149 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
150 [ 'search' => $term, 'fulltext' => 1 ]
151 );
152
153 $captionText = $this->specialSearch->msg( 'search-interwiki-resultset-link' )->text();
154 $searchLink = Html::element( 'a', [ 'href' => $href, 'target' => '_blank' ], $captionText );
155
156 return Html::rawElement( 'div',
157 [ 'class' => 'iw-result__footer' ],
158 $searchLink );
159 }
160
161 protected function loadCustomCaptions() {
162 if ( $this->customCaptions !== null ) {
163 return;
164 }
165
166 $this->customCaptions = [];
167 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->text() );
168 foreach ( $customLines as $line ) {
169 $parts = explode( ':', $line, 2 );
170 if ( count( $parts ) === 2 ) {
171 $this->customCaptions[$parts[0]] = $parts[1];
172 }
173 }
174 }
175
184 protected function iwIcon( $iwPrefix ) {
185 $logoName = $this->generateLogoName( $iwPrefix );
186 // If the value is an URL we use the favicon
187 if ( filter_var( $logoName, FILTER_VALIDATE_URL ) || $logoName === "/" ) {
188 return $this->generateIconFromFavicon( $logoName );
189 }
190
191 $iwIcon = new OOUI\IconWidget( [
192 'icon' => $logoName
193 ] );
194
195 return $iwIcon;
196 }
197
208 protected function generateLogoName( $prefix ) {
209 $logoOverridesKeys = array_keys( $this->iwLogoOverrides );
210 if ( in_array( $prefix, $logoOverridesKeys ) ) {
211 return $this->iwLogoOverrides[ $prefix ];
212 }
213
214 $interwiki = $this->iwLookup->fetch( $prefix );
215 return $interwiki ? $interwiki->getURL() : '/';
216 }
217
224 protected function generateIconFromFavicon( $logoUrl ) {
225 $parsed = wfGetUrlUtils()->parse( (string)wfGetUrlUtils()->expand( $logoUrl, PROTO_CURRENT ) );
226 '@phan-var array $parsed'; // Valid URL
227 $iwIconUrl = $parsed['scheme'] .
228 $parsed['delimiter'] .
229 $parsed['host'] .
230 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
231 '/favicon.ico';
232
233 $iwIcon = new OOUI\IconWidget( [
234 'icon' => 'favicon'
235 ] );
236
237 return $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
238 }
239}
const PROTO_CURRENT
Definition Defines.php:236
const NS_SPECIAL
Definition Defines.php:54
wfGetUrlUtils()
This class is a collection of static functions that serve two purposes:
Definition Html.php:57
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:78
A set of SearchEngine results.
Service interface for looking up Interwiki records.
Renders a single search result to HTML.
element(SerializerNode $parent, SerializerNode $node, $contents)