MediaWiki REL1_37
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 $footerHtml = $this->footerHtml( $term, $iwPrefix );
89 $iwResultListOutput .= Html::rawElement( 'li',
90 [
91 'class' => 'iw-resultset',
92 'data-iw-resultset-pos' => $iwResultSetPos,
93 'data-iw-resultset-source' => $iwPrefix
94 ],
95
96 $iwResultItemOutput .
97 $footerHtml
98 );
99
100 $iwResultSetPos++;
101 }
102
103 return Html::rawElement(
104 'div',
105 [ 'id' => 'mw-interwiki-results' ],
106 Html::rawElement(
107 'p',
108 [ 'class' => 'iw-headline' ],
109 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
110 ) .
111 Html::rawElement(
112 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
113 )
114 );
115 }
116
124 protected function footerHtml( $term, $iwPrefix ) {
125 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
126 [ 'search' => $term, 'fulltext' => 1 ]
127 );
128
129 $interwiki = $this->iwLookup->fetch( $iwPrefix );
130 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
131
132 $caption = $this->customCaptions[$iwPrefix] ??
133 $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
134
135 $searchLink = Html::rawElement( 'em', null,
136 Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
137 );
138
139 return Html::rawElement( 'div',
140 [ 'class' => 'iw-result__footer' ],
141 $this->iwIcon( $iwPrefix ) . $searchLink );
142 }
143
144 protected function loadCustomCaptions() {
145 if ( $this->customCaptions !== null ) {
146 return;
147 }
148
149 $this->customCaptions = [];
150 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
151 foreach ( $customLines as $line ) {
152 $parts = explode( ':', $line, 2 );
153 if ( count( $parts ) === 2 ) {
154 $this->customCaptions[$parts[0]] = $parts[1];
155 }
156 }
157 }
158
168 protected function iwIcon( $iwPrefix ) {
169 $interwiki = $this->iwLookup->fetch( $iwPrefix );
170 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
171
172 $iwIconUrl = $parsed['scheme'] .
173 $parsed['delimiter'] .
174 $parsed['host'] .
175 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
176 '/favicon.ico';
177
178 $iwIcon = new OOUI\IconWidget( [
179 'icon' => 'favicon'
180 ] );
181
182 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
183
184 return $iwIcon;
185 }
186}
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:49
Class that generates HTML links for pages.
Renders one or more ISearchResultSets into a sidebar grouped by 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:48
A set of SearchEngine results.
Service interface for looking up Interwiki records.
Renders a single search result to HTML.
$line
Definition mcc.php:119