MediaWiki REL1_32
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1<?php
2
4
9use Title;
10use Html;
11use OOUI;
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 }
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
66 $iwResults = [];
67 foreach ( $resultSets as $resultSet ) {
68 foreach ( $resultSet as $result ) {
69 if ( !$result->isBrokenTitle() ) {
70 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
71 }
72 }
73 }
74
75 $iwResultSetPos = 1;
76 $iwResultListOutput = '';
77
78 foreach ( $iwResults as $iwPrefix => $results ) {
79 // TODO: Assumes interwiki results are never paginated
80 $position = 0;
81 $iwResultItemOutput = '';
82
83 foreach ( $results as $result ) {
84 $iwResultItemOutput .= $this->resultWidget->render( $result, $term, $position++ );
85 }
86
87 $footerHtml = $this->footerHtml( $term, $iwPrefix );
88 $iwResultListOutput .= Html::rawElement( 'li',
89 [
90 'class' => 'iw-resultset',
91 'data-iw-resultset-pos' => $iwResultSetPos,
92 'data-iw-resultset-source' => $iwPrefix
93 ],
94
95 $iwResultItemOutput .
96 $footerHtml
97 );
98
99 $iwResultSetPos++;
100 }
101
102 return Html::rawElement(
103 'div',
104 [ 'id' => 'mw-interwiki-results' ],
105 Html::rawElement(
106 'p',
107 [ 'class' => 'iw-headline' ],
108 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
109 ) .
110 Html::rawElement(
111 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
112 )
113 );
114 }
115
123 protected function footerHtml( $term, $iwPrefix ) {
124 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
125 [ 'search' => $term, 'fulltext' => 1 ]
126 );
127
128 $interwiki = $this->iwLookup->fetch( $iwPrefix );
129 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
130
131 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
132 $caption = $this->customCaptions[$iwPrefix];
133 } else {
134 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
135 }
136
137 $searchLink = Html::rawElement( 'em', null,
138 Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
139 );
140
141 return Html::rawElement( 'div',
142 [ 'class' => 'iw-result__footer' ],
143 $this->iwIcon( $iwPrefix ) . $searchLink );
144 }
145
146 protected function loadCustomCaptions() {
147 if ( $this->customCaptions !== null ) {
148 return;
149 }
150
151 $this->customCaptions = [];
152 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
153 foreach ( $customLines as $line ) {
154 $parts = explode( ':', $line, 2 );
155 if ( count( $parts ) === 2 ) {
156 $this->customCaptions[$parts[0]] = $parts[1];
157 }
158 }
159 }
160
170 protected function iwIcon( $iwPrefix ) {
171 $interwiki = $this->iwLookup->fetch( $iwPrefix );
172 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
173
174 $iwIconUrl = $parsed['scheme'] .
175 $parsed['delimiter'] .
176 $parsed['host'] .
177 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
178 '/favicon.ico';
179
180 $iwIcon = new OOUI\IconWidget( [
181 'icon' => 'favicon'
182 ] );
183
184 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
185
186 return $iwIcon;
187 }
188}
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
$line
Definition cdb.php:59
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 SearchResultSets into a sidebar grouped by 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)
footerHtml( $term, $iwPrefix)
Generates an HTML footer for the given interwiki prefix.
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:39
namespace being checked & $result
Definition hooks.txt:2385
For QUnit the mediawiki tests qunit testrunner dependency will be added to any module whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2926
const NS_SPECIAL
Definition Defines.php:53
Service interface for looking up Interwiki records.
Renders a set of search results to HTML.
Renders a single search result to HTML.