MediaWiki REL1_30
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 $result = $resultSet->next();
69 while ( $result ) {
70 if ( !$result->isBrokenTitle() ) {
71 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
72 }
73 $result = $resultSet->next();
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, $term, $position++ );
87 }
88
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 $iwResultItemOutput .
98 $footerHtml
99 );
100
101 $iwResultSetPos++;
102 }
103
104 return Html::rawElement(
105 'div',
106 [ 'id' => 'mw-interwiki-results' ],
107 Html::rawElement(
108 'p',
109 [ 'class' => 'iw-headline' ],
110 $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
111 ) .
112 Html::rawElement(
113 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
114 )
115 );
116 }
117
125 protected function footerHtml( $term, $iwPrefix ) {
126 $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
127 [ 'search' => $term, 'fulltext' => 1 ]
128 );
129
130 $interwiki = $this->iwLookup->fetch( $iwPrefix );
131 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
132
133 if ( isset( $this->customCaptions[$iwPrefix] ) ) {
134 $caption = $this->customCaptions[$iwPrefix];
135 } else {
136 $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
137 }
138
139 $searchLink = Html::rawElement( 'em', null,
140 Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
141 );
142
143 return Html::rawElement( 'div',
144 [ 'class' => 'iw-result__footer' ],
145 $this->iwIcon( $iwPrefix ) . $searchLink );
146 }
147
148 protected function loadCustomCaptions() {
149 if ( $this->customCaptions !== null ) {
150 return;
151 }
152
153 $this->customCaptions = [];
154 $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
155 foreach ( $customLines as $line ) {
156 $parts = explode( ':', $line, 2 );
157 if ( count( $parts ) === 2 ) {
158 $this->customCaptions[$parts[0]] = $parts[1];
159 }
160 }
161 }
162
172 protected function iwIcon( $iwPrefix ) {
173 $interwiki = $this->iwLookup->fetch( $iwPrefix );
174 $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
175
176 $iwIconUrl = $parsed['scheme'] .
177 $parsed['delimiter'] .
178 $parsed['host'] .
179 ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
180 '/favicon.ico';
181
182 $iwIcon = new OOUI\IconWidget( [
183 'icon' => 'favicon'
184 ] );
185
186 $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
187
188 return $iwIcon;
189 }
190}
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:58
This class is a collection of static functions that serve two purposes:
Definition Html.php:48
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
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
external whereas SearchGetNearMatch runs after $term
Definition hooks.txt:2814
namespace being checked & $result
Definition hooks.txt:2293
const NS_SPECIAL
Definition Defines.php:54
Service interface for looking up Interwiki records.
Renders a set of search results to HTML.
Renders a single search result to HTML.