MediaWiki  master
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
11 use OOUI;
12 
20  protected $specialSearch;
22  protected $resultWidget;
24  protected $customCaptions;
26  protected $linkRenderer;
28  protected $iwLookup;
30  protected $output;
32  protected $showMultimedia;
34  protected $iwLogoOverrides;
35 
36  public function __construct(
41  $showMultimedia = false
42  ) {
43  $this->specialSearch = $specialSearch;
44  $this->resultWidget = $resultWidget;
45  $this->linkRenderer = $linkRenderer;
46  $this->iwLookup = $iwLookup;
47  $this->output = $specialSearch->getOutput();
48  $this->showMultimedia = $showMultimedia;
49  $this->iwLogoOverrides = $this->specialSearch->getConfig()->get( 'InterwikiLogoOverride' );
50  }
51 
58  public function render( $term, $resultSets ) {
59  if ( !is_array( $resultSets ) ) {
60  $resultSets = [ $resultSets ];
61  }
62 
63  $this->loadCustomCaptions();
64 
65  if ( $this->showMultimedia ) {
66  $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
67  }
68  $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
69  $this->output->addModuleStyles( 'oojs-ui.styles.icons-wikimedia' );
70 
71  $iwResults = [];
72  foreach ( $resultSets as $resultSet ) {
73  foreach ( $resultSet as $result ) {
74  if ( !$result->isBrokenTitle() ) {
75  $iwResults[$result->getTitle()->getInterwiki()][] = $result;
76  }
77  }
78  }
79 
80  $iwResultSetPos = 1;
81  $iwResultListOutput = '';
82 
83  foreach ( $iwResults as $iwPrefix => $results ) {
84  // TODO: Assumes interwiki results are never paginated
85  $position = 0;
86  $iwResultItemOutput = '';
87 
88  foreach ( $results as $result ) {
89  $iwResultItemOutput .= $this->resultWidget->render( $result, $position++ );
90  }
91 
92  $headerHtml = $this->headerHtml( $term, $iwPrefix );
93  $footerHtml = $this->footerHtml( $term, $iwPrefix );
94  $iwResultListOutput .= Html::rawElement( 'li',
95  [
96  'class' => 'iw-resultset',
97  'data-iw-resultset-pos' => $iwResultSetPos,
98  'data-iw-resultset-source' => $iwPrefix
99  ],
100 
101  $headerHtml .
102  $iwResultItemOutput .
103  $footerHtml
104  );
105  $iwResultSetPos++;
106  }
107 
108  return Html::rawElement(
109  'div',
110  [ 'id' => 'mw-interwiki-results' ],
112  'ul', [ 'class' => 'iw-results', ], $iwResultListOutput
113  )
114  );
115  }
116 
124  protected function headerHtml( $term, $iwPrefix ) {
125  $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $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] ?? $parsed['host'];
133 
134  $searchLink = Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption );
135 
136  return Html::rawElement( 'div',
137  [ 'class' => 'iw-result__header' ],
138  $this->iwIcon( $iwPrefix ) . $searchLink );
139  }
140 
148  protected function footerHtml( $term, $iwPrefix ) {
149  $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
150  [ 'search' => $term, 'fulltext' => 1 ]
151  );
152 
153  $interwiki = $this->iwLookup->fetch( $iwPrefix );
154  $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
155 
156  $caption = $this->specialSearch->msg( 'search-interwiki-resultset-link', $parsed['host'] )->escaped();
157 
158  $searchLink = Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption );
159 
160  return Html::rawElement( 'div',
161  [ 'class' => 'iw-result__footer' ],
162  $searchLink );
163  }
164 
165  protected function loadCustomCaptions() {
166  if ( $this->customCaptions !== null ) {
167  return;
168  }
169 
170  $this->customCaptions = [];
171  $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
172  foreach ( $customLines as $line ) {
173  $parts = explode( ':', $line, 2 );
174  if ( count( $parts ) === 2 ) {
175  $this->customCaptions[$parts[0]] = $parts[1];
176  }
177  }
178  }
179 
188  protected function iwIcon( $iwPrefix ) {
189  $logoName = $this->generateLogoName( $iwPrefix );
190  // If the value is an URL we use the favicon
191  if ( filter_var( $logoName, FILTER_VALIDATE_URL ) || $logoName === "/" ) {
192  return $this->generateIconFromFavicon( $logoName );
193  }
194 
195  $iwIcon = new OOUI\IconWidget( [
196  'icon' => $logoName
197  ] );
198 
199  return $iwIcon;
200  }
201 
212  protected function generateLogoName( $prefix ) {
213  $logoOverridesKeys = array_keys( $this->iwLogoOverrides );
214  if ( in_array( $prefix, $logoOverridesKeys ) ) {
215  return $this->iwLogoOverrides[ $prefix ];
216  }
217 
218  $interwiki = $this->iwLookup->fetch( $prefix );
219  return $interwiki ? $interwiki->getURL() : '/';
220  }
221 
228  protected function generateIconFromFavicon( $logoUrl ) {
229  $parsed = wfParseUrl( wfExpandUrl( $logoUrl ) );
230  $iwIconUrl = $parsed['scheme'] .
231  $parsed['delimiter'] .
232  $parsed['host'] .
233  ( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) .
234  '/favicon.ico';
235 
236  $iwIcon = new OOUI\IconWidget( [
237  'icon' => 'favicon'
238  ] );
239 
240  return $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] );
241  }
242 }
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 using $wgServer (or one of its alternatives).
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:239
Class that generates HTML for internal links.
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
generateIconFromFavicon( $logoUrl)
Fetches the favicon of the provided URL.
headerHtml( $term, $iwPrefix)
Generates an HTML header for the given interwiki prefix.
footerHtml( $term, $iwPrefix)
Generates an HTML footer for the given interwiki prefix.
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup, $showMultimedia=false)
generateLogoName( $prefix)
Generates the logo name used to render the interwiki icon.
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:76
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:624
A set of SearchEngine results.
Service interface for looking up Interwiki records.
Renders a single search result to HTML.