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