MediaWiki master
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1<?php
2
4
13use OOUI;
14
26 protected bool $showMultimedia;
28 protected array $iwLogoOverrides;
30 protected ?array $customCaptions = null;
31
32 public function __construct(
37 bool $showMultimedia = false
38 ) {
39 $this->specialSearch = $specialSearch;
40 $this->resultWidget = $resultWidget;
41 $this->linkRenderer = $linkRenderer;
42 $this->iwLookup = $iwLookup;
43 $this->output = $specialSearch->getOutput();
44 $this->showMultimedia = $showMultimedia;
45 $this->iwLogoOverrides = $this->specialSearch->getConfig()->get( MainConfigNames::InterwikiLogoOverride );
46 }
47
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 $this->output->addModuleStyles( 'oojs-ui.styles.icons-wikimedia' );
66
67 $iwResults = [];
68 foreach ( $resultSets as $resultSet ) {
69 foreach ( $resultSet as $result ) {
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
72 }
73 }
74 }
75
76 $iwResultSetPos = 1;
77 $iwResultListOutput = '';
78
79 foreach ( $iwResults as $iwPrefix => $results ) {
80 // TODO: Assumes interwiki results are never paginated
81 $position = 0;
82 $iwResultItemOutput = '';
83
84 foreach ( $results as $result ) {
85 $iwResultItemOutput .= $this->resultWidget->render( $result, $position++ );
86 }
87
88 $headerHtml = $this->headerHtml( $term, $iwPrefix );
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 $headerHtml .
98 $iwResultItemOutput .
99 $footerHtml
100 );
101 $iwResultSetPos++;
102 }
103
104 return Html::rawElement(
105 'div',
106 [ 'id' => 'mw-interwiki-results' ],
107 Html::rawElement(
108 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
109 )
110 );
111 }
112
120 protected function headerHtml( $term, $iwPrefix ) {
121 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
122 [ 'search' => $term, 'fulltext' => 1 ]
123 );
124
125 $interwiki = $this->iwLookup->fetch( $iwPrefix );
126 // This is false if the lookup fails, or if the other wiki is on the same
127 // domain name (i.e. /en-wiki/ and /de-wiki/)
128 $iwHost = $interwiki ? parse_url( $interwiki->getURL(), PHP_URL_HOST ) : false;
129
130 $captionText = $this->customCaptions[$iwPrefix] ?? $iwHost ?: $iwPrefix;
131 $searchLink = Html::element( 'a', [ 'href' => $href, 'target' => '_blank' ], $captionText );
132
133 return Html::rawElement( 'div',
134 [ 'class' => 'iw-result__header' ],
135 $this->iwIcon( $iwPrefix ) . $searchLink );
136 }
137
145 protected function footerHtml( $term, $iwPrefix ) {
146 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
147 [ 'search' => $term, 'fulltext' => 1 ]
148 );
149
150 $captionText = $this->specialSearch->msg( 'search-interwiki-resultset-link' )->text();
151 $searchLink = Html::element( 'a', [ 'href' => $href, 'target' => '_blank' ], $captionText );
152
153 return Html::rawElement( 'div',
154 [ 'class' => 'iw-result__footer' ],
155 $searchLink );
156 }
157
158 protected function loadCustomCaptions() {
159 if ( $this->customCaptions !== null ) {
160 return;
161 }
162
163 $this->customCaptions = [];
164 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->text() );
165 foreach ( $customLines as $line ) {
166 $parts = explode( ':', $line, 2 );
167 if ( count( $parts ) === 2 ) {
168 $this->customCaptions[$parts[0]] = $parts[1];
169 }
170 }
171 }
172
181 protected function iwIcon( $iwPrefix ) {
182 $logoName = $this->generateLogoName( $iwPrefix );
183 // If the value is an URL we use the favicon
184 if ( filter_var( $logoName, FILTER_VALIDATE_URL ) || $logoName === "/" ) {
185 return $this->generateIconFromFavicon( $logoName );
186 }
187
188 $iwIcon = new OOUI\IconWidget( [
189 'icon' => $logoName
190 ] );
191
192 return $iwIcon;
193 }
194
205 protected function generateLogoName( $prefix ) {
206 if ( isset( $this->iwLogoOverrides[ $prefix ] ) ) {
207 return $this->iwLogoOverrides[ $prefix ];
208 }
209
210 $interwiki = $this->iwLookup->fetch( $prefix );
211 return $interwiki ? $interwiki->getURL() : '/';
212 }
213
220 protected function generateIconFromFavicon( $logoUrl ) {
221 $parsed = wfGetUrlUtils()->parse( (string)wfGetUrlUtils()->expand( $logoUrl, PROTO_CURRENT ) );
222 '@phan-var array $parsed'; // Valid URL
223 $iwIconUrl = $parsed['scheme'] .
224 $parsed['delimiter'] .
225 $parsed['host'] .
226 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
227 '/favicon.ico';
228
229 $iwIcon = new OOUI\IconWidget( [
230 'icon' => 'favicon'
231 ] );
232
233 return $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
234 }
235}
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.
generateLogoName( $prefix)
Generates the logo name used to render the interwiki icon.
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup, bool $showMultimedia=false)
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)