3 namespace MediaWiki\Widget\Search
;
5 use MediaWiki\Interwiki\InterwikiLookup
;
6 use MediaWiki\Linker\LinkRenderer
;
13 * Renders one or more SearchResultSets into a sidebar grouped by
14 * interwiki prefix. Includes a per-wiki header indicating where
15 * the results are from.
17 * @deprecated since 1.31. Use InterwikiSearchResultSetWidget
19 class SimpleSearchResultSetWidget
implements SearchResultSetWidget
{
20 /** @var SpecialSearch */
21 protected $specialSearch;
22 /** @var SearchResultWidget */
23 protected $resultWidget;
24 /** @var string[]|null */
25 protected $customCaptions;
26 /** @var LinkRenderer */
27 protected $linkRenderer;
28 /** @var InterwikiLookup */
31 public function __construct(
32 SpecialSearch
$specialSearch,
33 SearchResultWidget
$resultWidget,
34 LinkRenderer
$linkRenderer,
35 InterwikiLookup
$iwLookup
37 wfDeprecated( __METHOD__
, '1.31' );
38 $this->specialSearch
= $specialSearch;
39 $this->resultWidget
= $resultWidget;
40 $this->linkRenderer
= $linkRenderer;
41 $this->iwLookup
= $iwLookup;
45 * @param string $term User provided search term
46 * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki
50 public function render( $term, $resultSets ) {
51 if ( !is_array( $resultSets ) ) {
52 $resultSets = [ $resultSets ];
55 $this->loadCustomCaptions();
58 foreach ( $resultSets as $resultSet ) {
59 $result = $resultSet->next();
61 if ( !$result->isBrokenTitle() ) {
62 $iwResults[$result->getTitle()->getInterwiki()][] = $result;
64 $result = $resultSet->next();
69 foreach ( $iwResults as $iwPrefix => $results ) {
70 $out .= $this->headerHtml( $iwPrefix, $term );
71 $out .= "<ul class='mw-search-iwresults'>";
72 // TODO: Assumes interwiki results are never paginated
74 foreach ( $results as $result ) {
75 $out .= $this->resultWidget
->render( $result, $term, $position++
);
80 return "<div id='mw-search-interwiki'>" .
81 "<div id='mw-search-interwiki-caption'>" .
82 $this->specialSearch
->msg( 'search-interwiki-caption' )->parse() .
89 * Generates an appropriate HTML header for the given interwiki prefix
91 * @param string $iwPrefix Interwiki prefix of wiki to show header for
92 * @param string $term User provided search term
95 protected function headerHtml( $iwPrefix, $term ) {
96 if ( isset( $this->customCaptions
[$iwPrefix] ) ) {
97 $caption = $this->customCaptions
[$iwPrefix];
99 $interwiki = $this->iwLookup
->fetch( $iwPrefix );
100 $parsed = wfParseUrl( wfExpandUrl( $interwiki ?
$interwiki->getURL() : '/' ) );
101 $caption = $this->specialSearch
->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
104 $href = Title
::makeTitle( NS_SPECIAL
, 'Search', null, $iwPrefix )->getLocalURL(
105 [ 'search' => $term, 'fulltext' => 1 ]
107 $searchLink = Html
::rawElement(
110 $this->specialSearch
->msg( 'search-interwiki-more' )->escaped()
113 return "<div class='mw-search-interwiki-project'>" .
114 "<span class='mw-search-interwiki-more'>{$searchLink}</span>" .
119 protected function loadCustomCaptions() {
120 if ( $this->customCaptions
!== null ) {
124 $this->customCaptions
= [];
125 $customLines = explode( "\n", $this->specialSearch
->msg( 'search-interwiki-custom' )->escaped() );
126 foreach ( $customLines as $line ) {
127 $parts = explode( ':', $line, 2 );
128 if ( count( $parts ) === 2 ) {
129 $this->customCaptions
[$parts[0]] = $parts[1];