MediaWiki REL1_33
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 }
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, $term, $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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
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:40
namespace being checked & $result
Definition hooks.txt:2340
whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2889
const NS_SPECIAL
Definition Defines.php:62
Service interface for looking up Interwiki records.
Renders a set of search results to HTML.
Renders a single search result to HTML.