MediaWiki  1.34.0
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, $term, $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 }
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\$customCaptions
string[] null $customCaptions
Definition: SimpleSearchResultSetWidget.php:25
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
MediaWiki\Widget\Search
Definition: BasicSearchResultSetWidget.php:3
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
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
Definition: GlobalFunctions.php:1044
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\$iwLookup
InterwikiLookup $iwLookup
Definition: SimpleSearchResultSetWidget.php:29
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
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\$linkRenderer
LinkRenderer $linkRenderer
Definition: SimpleSearchResultSetWidget.php:27
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
MediaWiki\Widget\Search\SimpleSearchResultSetWidget
Renders one or more ISearchResultSets into a sidebar grouped by interwiki prefix.
Definition: SimpleSearchResultSetWidget.php:19
ISearchResultSet
A set of SearchEngine results.
Definition: ISearchResultSet.php:11
$line
$line
Definition: cdb.php:59
SpecialSearch
implements Special:Search - Run text & title search and display the output
Definition: SpecialSearch.php:38
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\$resultWidget
SearchResultWidget $resultWidget
Definition: SimpleSearchResultSetWidget.php:23
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\loadCustomCaptions
loadCustomCaptions()
Definition: SimpleSearchResultSetWidget.php:117
Title
Represents a title within MediaWiki.
Definition: Title.php:42
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\$specialSearch
SpecialSearch $specialSearch
Definition: SimpleSearchResultSetWidget.php:21
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\render
render( $term, $resultSets)
Definition: SimpleSearchResultSetWidget.php:50
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\headerHtml
headerHtml( $iwPrefix, $term)
Generates an appropriate HTML header for the given interwiki prefix.
Definition: SimpleSearchResultSetWidget.php:93
MediaWiki\Widget\Search\SimpleSearchResultSetWidget\__construct
__construct(SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, InterwikiLookup $iwLookup)
Definition: SimpleSearchResultSetWidget.php:31
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491