MediaWiki  master
SimpleSearchResultSetWidget.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use SpecialSearch;
9 use Title;
10 use Html;
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 .= "<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 .= "</ul>";
76  }
77 
78  return "<div id='mw-search-interwiki'>" .
79  "<div id='mw-search-interwiki-caption'>" .
80  $this->specialSearch->msg( 'search-interwiki-caption' )->parse() .
81  '</div>' .
82  $out .
83  "</div>";
84  }
85 
93  protected function headerHtml( $iwPrefix, $term ) {
94  if ( isset( $this->customCaptions[$iwPrefix] ) ) {
95  $caption = $this->customCaptions[$iwPrefix];
96  } else {
97  $interwiki = $this->iwLookup->fetch( $iwPrefix );
98  $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
99  $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
100  }
101 
102  $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL(
103  [ 'search' => $term, 'fulltext' => 1 ]
104  );
105  $searchLink = Html::rawElement(
106  'a',
107  [ 'href' => $href ],
108  $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
109  );
110 
111  return "<div class='mw-search-interwiki-project'>" .
112  "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
113  $caption .
114  "</div>";
115  }
116 
117  protected function loadCustomCaptions() {
118  if ( $this->customCaptions !== null ) {
119  return;
120  }
121 
122  $this->customCaptions = [];
123  $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() );
124  foreach ( $customLines as $line ) {
125  $parts = explode( ':', $line, 2 );
126  if ( count( $parts ) === 2 ) {
127  $this->customCaptions[$parts[0]] = $parts[1];
128  }
129  }
130  }
131 }
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
const NS_SPECIAL
Definition: Defines.php:49
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix. ...
Class that generates HTML links for pages.
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup)
Service interface for looking up Interwiki records.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:584
Renders a set of search results to HTML.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
$line
Definition: mcc.php:119
Renders a single search result to HTML.
headerHtml( $iwPrefix, $term)
Generates an appropriate HTML header for the given interwiki prefix.