MediaWiki REL1_39
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1<?php
2
4
5use Html;
9use OOUI;
11use Title;
12
20 protected $specialSearch;
22 protected $resultWidget;
24 protected $customCaptions;
26 protected $linkRenderer;
28 protected $iwLookup;
30 protected $output;
32 protected $showMultimedia;
33
34 public function __construct(
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
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
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 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
127
128 $caption = $this->customCaptions[$iwPrefix] ?? $parsed['host'];
129
130 $searchLink = Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption );
131
132 return Html::rawElement( 'div',
133 [ 'class' => 'iw-result__header' ],
134 $this->iwIcon( $iwPrefix ) . $searchLink );
135 }
136
144 protected function footerHtml( $term, $iwPrefix ) {
145 $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
146 [ 'search' => $term, 'fulltext' => 1 ]
147 );
148
149 $interwiki = $this->iwLookup->fetch( $iwPrefix );
150 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
151
152 $caption = $this->specialSearch->msg( 'search-interwiki-resultset-link', $parsed['host'] )->escaped();
153
154 $searchLink = Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption );
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' )->escaped() );
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
185 protected function iwIcon( $iwPrefix ) {
186 $interwiki = $this->iwLookup->fetch( $iwPrefix );
187 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
188
189 $iwIconUrl = $parsed['scheme'] .
190 $parsed['delimiter'] .
191 $parsed['host'] .
192 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
193 '/favicon.ico';
194
195 $iwIcon = new OOUI\IconWidget( [
196 'icon' => 'favicon'
197 ] );
198
199 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
200
201 return $iwIcon;
202 }
203}
const NS_SPECIAL
Definition Defines.php:53
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
This class is a collection of static functions that serve two purposes:
Definition Html.php:51
Class that generates HTML anchor link elements for pages.
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
headerHtml( $term, $iwPrefix)
Generates an HTML header for the given interwiki prefix.
footerHtml( $term, $iwPrefix)
Generates an HTML footer for the given interwiki prefix.
iwIcon( $iwPrefix)
Generates a custom OOUI icon element with a favicon as the image.
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup, $showMultimedia=false)
This is one of the Core classes and should be read at least once by any new developers.
getOutput()
Get the OutputPage being used for this instance.
implements Special:Search - Run text & title search and display the output
Represents a title within MediaWiki.
Definition Title.php:49
A set of SearchEngine results.
Service interface for looking up Interwiki records.
Renders a single search result to HTML.
$line
Definition mcc.php:119