| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Search\SearchWidgets; |
| 4 |
|
| 5 |
use ISearchResultSet; |
| 6 |
use MediaWiki\MediaWikiServices; |
| 7 |
use MediaWiki\Message\Message; |
| 8 |
use MediaWiki\Specials\SpecialSearch; |
| 9 |
use MediaWiki\Status\Status; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class BasicSearchResultSetWidget { |
| 16 |
|
| 17 |
protected $specialPage; |
| 18 |
|
| 19 |
protected $resultWidget; |
| 20 |
|
| 21 |
protected $sidebarWidget; |
| 22 |
|
| 23 |
public function __construct( |
| 24 |
SpecialSearch $specialPage, |
| 25 |
SearchResultWidget $resultWidget, |
| 26 |
SearchResultSetWidget $sidebarWidget |
| 27 |
) { |
| 28 |
$this->specialPage = $specialPage; |
| 29 |
$this->resultWidget = $resultWidget; |
| 30 |
$this->sidebarWidget = $sidebarWidget; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function render( |
| 41 |
$term, |
| 42 |
$offset, |
| 43 |
?ISearchResultSet $titleResultSet = null, |
| 44 |
?ISearchResultSet $textResultSet = null |
| 45 |
) { |
| 46 |
$hasTitle = $titleResultSet && $titleResultSet->numRows() > 0; |
| 47 |
$hasText = $textResultSet && $textResultSet->numRows() > 0; |
| 48 |
$hasSecondary = |
| 49 |
$textResultSet && |
| 50 |
$textResultSet->hasInterwikiResults( ISearchResultSet::SECONDARY_RESULTS ); |
| 51 |
$hasSecondaryInline = |
| 52 |
$textResultSet && |
| 53 |
$textResultSet->hasInterwikiResults( ISearchResultSet::INLINE_RESULTS ); |
| 54 |
|
| 55 |
if ( !$hasTitle && !$hasText && !$hasSecondary && !$hasSecondaryInline ) { |
| 56 |
return ''; |
| 57 |
} |
| 58 |
|
| 59 |
$out = '<div class="mw-search-results-container">'; |
| 60 |
|
| 61 |
if ( $hasTitle ) { |
| 62 |
$out .= $this->header( $this->specialPage->msg( 'titlematches' ) ) |
| 63 |
|
| 64 |
. $this->renderResultSet( $titleResultSet, $offset ); |
| 65 |
} |
| 66 |
|
| 67 |
if ( $hasText ) { |
| 68 |
if ( $hasTitle ) { |
| 69 |
$out .= "<div class='mw-search-visualclear'></div>" . |
| 70 |
$this->header( $this->specialPage->msg( 'textmatches' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
$out .= $this->renderResultSet( $textResultSet, $offset ); |
| 74 |
} |
| 75 |
|
| 76 |
if ( $hasSecondaryInline ) { |
| 77 |
$iwResults = $textResultSet->getInterwikiResults( ISearchResultSet::INLINE_RESULTS ); |
| 78 |
foreach ( $iwResults as $interwiki => $results ) { |
| 79 |
if ( $results instanceof Status || $results->numRows() === 0 ) { |
| 80 |
|
| 81 |
continue; |
| 82 |
} |
| 83 |
$out .= |
| 84 |
"<h2 class='mw-search-interwiki-header mw-search-visualclear'>" . |
| 85 |
$this->specialPage->msg( "search-interwiki-results-{$interwiki}" )->parse() . |
| 86 |
"</h2>"; |
| 87 |
$out .= |
| 88 |
"<div class='mw-search-interwiki-results'>" . |
| 89 |
$this->renderResultSet( $results, $offset ) . |
| 90 |
"</div>"; |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
$out .= '</div>'; |
| 96 |
|
| 97 |
if ( $hasSecondary ) { |
| 98 |
$out .= $this->sidebarWidget->render( |
| 99 |
$term, |
| 100 |
$textResultSet->getInterwikiResults( ISearchResultSet::SECONDARY_RESULTS ) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
$converter = MediaWikiServices::getInstance()->getLanguageConverterFactory() |
| 107 |
->getLanguageConverter(); |
| 108 |
return $converter->convert( $out ); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
protected function header( Message $msg ) { |
| 120 |
return "<h2>" . $msg->escaped() . "</h2>"; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
protected function renderResultSet( ISearchResultSet $resultSet, $offset ) { |
| 129 |
$hits = []; |
| 130 |
foreach ( $resultSet as $result ) { |
| 131 |
$hits[] = $this->resultWidget->render( $result, $offset++ ); |
| 132 |
} |
| 133 |
|
| 134 |
return "<ul class='mw-search-results'>" . implode( '', $hits ) . "</ul>"; |
| 135 |
} |
| 136 |
} |