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', null, $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 }
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\$customCaptions
string[] null $customCaptions
Definition: SimpleSearchResultSetWidget.php:25
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\$resultWidget
SearchResultWidget $resultWidget
Definition: SimpleSearchResultSetWidget.php:23
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:43
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\loadCustomCaptions
loadCustomCaptions()
Definition: SimpleSearchResultSetWidget.php:125
MediaWiki\Search\SearchWidgets\SearchResultSetWidget
Renders a set of search results to HTML.
Definition: SearchResultSetWidget.php:10
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\$specialSearch
SpecialSearch $specialSearch
Definition: SimpleSearchResultSetWidget.php:21
NS_SPECIAL
const NS_SPECIAL
Definition: Defines.php:53
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:316
wfParseUrl
wfParseUrl( $url)
parse_url() work-alike, but non-broken.
Definition: GlobalFunctions.php:776
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\render
render( $term, $resultSets)
Definition: SimpleSearchResultSetWidget.php:50
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Definition: GlobalFunctions.php:997
MediaWiki\Interwiki\InterwikiLookup
Service interface for looking up Interwiki records.
Definition: InterwikiLookup.php:32
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\__construct
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup)
Definition: SimpleSearchResultSetWidget.php:31
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
ISearchResultSet
A set of SearchEngine results.
Definition: ISearchResultSet.php:12
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\headerHtml
headerHtml( $iwPrefix, $term)
Generates an appropriate HTML header for the given interwiki prefix.
Definition: SimpleSearchResultSetWidget.php:96
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\$linkRenderer
LinkRenderer $linkRenderer
Definition: SimpleSearchResultSetWidget.php:27
$line
$line
Definition: mcc.php:119
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:41
MediaWiki\Search\SearchWidgets\SearchResultWidget
Renders a single search result to HTML.
Definition: SearchResultWidget.php:10
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
Definition: SimpleSearchResultSetWidget.php:19
Title
Represents a title within MediaWiki.
Definition: Title.php:48
MediaWiki\Search\SearchWidgets
Definition: BasicSearchResultSetWidget.php:3
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:252
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
MediaWiki\Search\SearchWidgets\SimpleSearchResultSetWidget\$iwLookup
InterwikiLookup $iwLookup
Definition: SimpleSearchResultSetWidget.php:29
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:474
Html
This class is a collection of static functions that serve two purposes:
Definition: Html.php:49