MediaWiki  master
SimpleSearchResultSetWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use Html;
9 use SpecialSearch;
10 use Title;
11 
21  protected $specialSearch;
23  protected $resultWidget;
25  protected $customCaptions;
27  protected $linkRenderer;
29  protected $iwLookup;
30 
31  public function __construct(
36  ) {
37  wfDeprecated( __METHOD__, '1.31' );
38  $this->specialSearch = $specialSearch;
39  $this->resultWidget = $resultWidget;
40  $this->linkRenderer = $linkRenderer;
41  $this->iwLookup = $iwLookup;
42  }
43 
50  public function render( $term, $resultSets ) {
51  if ( !is_array( $resultSets ) ) {
52  $resultSets = [ $resultSets ];
53  }
54 
55  $this->loadCustomCaptions();
56 
57  $iwResults = [];
58  foreach ( $resultSets as $resultSet ) {
59  foreach ( $resultSet as $result ) {
60  if ( !$result->isBrokenTitle() ) {
61  $iwResults[$result->getTitle()->getInterwiki()][] = $result;
62  }
63  }
64  }
65 
66  $out = '';
67  foreach ( $iwResults as $iwPrefix => $results ) {
68  $out .= $this->headerHtml( $iwPrefix, $term );
69  $out .= Html::openElement( 'ul', [ 'class' => 'mw-search-iwresults' ] );
70  // TODO: Assumes interwiki results are never paginated
71  $position = 0;
72  foreach ( $results as $result ) {
73  $out .= $this->resultWidget->render( $result, $position++ );
74  }
75  $out .= Html::closeElement( 'ul' );
76  }
77 
78  return Html::rawElement(
79  'div',
80  [ 'id' => 'mw-search-interwiki' ],
82  'div',
83  [ 'id' => 'mw-search-interwiki-caption' ],
84  $this->specialSearch->msg( 'search-interwiki-caption' )->parse()
85  ) . $out
86  );
87  }
88 
96  protected function headerHtml( $iwPrefix, $term ) {
97  if ( isset( $this->customCaptions[$iwPrefix] ) ) {
98  $caption = $this->customCaptions[$iwPrefix];
99  } else {
100  $interwiki = $this->iwLookup->fetch( $iwPrefix );
101  $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
102  $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
103  }
104 
105  $href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL(
106  [ 'search' => $term, 'fulltext' => 1 ]
107  );
108  $searchLink = Html::rawElement(
109  'a',
110  [ 'href' => $href ],
111  $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
112  );
113 
114  return Html::rawElement(
115  'div',
116  [ 'class' => 'mw-search-interwiki-project' ],
118  'span',
119  [ 'class' => 'mw-search-interwiki-more' ],
120  $searchLink
121  ) . $caption
122  );
123  }
124 
125  protected function loadCustomCaptions() {
126  if ( $this->customCaptions !== null ) {
127  return;
128  }
129 
130  $this->customCaptions = [];
131  $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
132  foreach ( $customLines as $line ) {
133  $parts = explode( ':', $line, 2 );
134  if ( count( $parts ) === 2 ) {
135  $this->customCaptions[$parts[0]] = $parts[1];
136  }
137  }
138  }
139 }
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.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:51
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:214
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:256
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:320
Class that generates HTML anchor link elements for pages.
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup)
headerHtml( $iwPrefix, $term)
Generates an appropriate HTML header for the given interwiki prefix.
implements Special:Search - Run text & title search and display the output
Represents a title within MediaWiki.
Definition: Title.php:52
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:641
A set of SearchEngine results.
Service interface for looking up Interwiki records.
Renders a single search result to HTML.
$line
Definition: mcc.php:119