MediaWiki  1.34.0
InterwikiSearchResultSetWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use SpecialSearch;
9 use Title;
10 use Html;
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;
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 }
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$resultWidget
$resultWidget
Definition: InterwikiSearchResultSetWidget.php:22
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\iwIcon
iwIcon( $iwPrefix)
Generates a custom OOUI icon element with a favicon as the image.
Definition: InterwikiSearchResultSetWidget.php:168
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\loadCustomCaptions
loadCustomCaptions()
Definition: InterwikiSearchResultSetWidget.php:144
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$linkRenderer
$linkRenderer
Definition: InterwikiSearchResultSetWidget.php:26
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$customCaptions
$customCaptions
Definition: InterwikiSearchResultSetWidget.php:24
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
Definition: InterwikiSearchResultSetWidget.php:18
MediaWiki\Widget\Search\SearchResultSetWidget
Renders a set of search results to HTML.
Definition: SearchResultSetWidget.php:10
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:49
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:793
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\render
render( $term, $resultSets)
Definition: InterwikiSearchResultSetWidget.php:55
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$specialSearch
$specialSearch
Definition: InterwikiSearchResultSetWidget.php:20
MediaWiki\Interwiki\InterwikiLookup
Service interface for looking up Interwiki records.
Definition: InterwikiLookup.php:32
MediaWiki\Widget\Search\SearchResultWidget
Renders a single search result to HTML.
Definition: SearchResultWidget.php:10
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
ISearchResultSet
A set of SearchEngine results.
Definition: ISearchResultSet.php:11
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$output
$output
Definition: InterwikiSearchResultSetWidget.php:30
$line
$line
Definition: cdb.php:59
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:38
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$showMultimedia
$showMultimedia
Definition: InterwikiSearchResultSetWidget.php:32
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\footerHtml
footerHtml( $term, $iwPrefix)
Generates an HTML footer for the given interwiki prefix.
Definition: InterwikiSearchResultSetWidget.php:124
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\$iwLookup
$iwLookup
Definition: InterwikiSearchResultSetWidget.php:28
MediaWiki\Widget\Search\InterwikiSearchResultSetWidget\__construct
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup, $showMultimedia=false)
Definition: InterwikiSearchResultSetWidget.php:34
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491