| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Search\SearchWidgets; |
| 4 |
|
| 5 |
use ISearchResultSet; |
| 6 |
use MediaWiki\Html\Html; |
| 7 |
use MediaWiki\Interwiki\InterwikiLookup; |
| 8 |
use MediaWiki\Linker\LinkRenderer; |
| 9 |
use MediaWiki\MainConfigNames; |
| 10 |
use MediaWiki\Output\OutputPage; |
| 11 |
use MediaWiki\Specials\SpecialSearch; |
| 12 |
use MediaWiki\Title\Title; |
| 13 |
use OOUI; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class InterwikiSearchResultSetWidget implements SearchResultSetWidget { |
| 21 |
|
| 22 |
protected $specialSearch; |
| 23 |
|
| 24 |
protected $resultWidget; |
| 25 |
|
| 26 |
protected $customCaptions; |
| 27 |
|
| 28 |
protected $linkRenderer; |
| 29 |
|
| 30 |
protected $iwLookup; |
| 31 |
|
| 32 |
protected $output; |
| 33 |
|
| 34 |
protected $showMultimedia; |
| 35 |
|
| 36 |
protected $iwLogoOverrides; |
| 37 |
|
| 38 |
public function __construct( |
| 39 |
SpecialSearch $specialSearch, |
| 40 |
SearchResultWidget $resultWidget, |
| 41 |
LinkRenderer $linkRenderer, |
| 42 |
InterwikiLookup $iwLookup, |
| 43 |
$showMultimedia = false |
| 44 |
) { |
| 45 |
$this->specialSearch = $specialSearch; |
| 46 |
$this->resultWidget = $resultWidget; |
| 47 |
$this->linkRenderer = $linkRenderer; |
| 48 |
$this->iwLookup = $iwLookup; |
| 49 |
$this->output = $specialSearch->getOutput(); |
| 50 |
$this->showMultimedia = $showMultimedia; |
| 51 |
$this->iwLogoOverrides = $this->specialSearch->getConfig()->get( MainConfigNames::InterwikiLogoOverride ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function render( $term, $resultSets ) { |
| 61 |
if ( !is_array( $resultSets ) ) { |
| 62 |
$resultSets = [ $resultSets ]; |
| 63 |
} |
| 64 |
|
| 65 |
$this->loadCustomCaptions(); |
| 66 |
|
| 67 |
if ( $this->showMultimedia ) { |
| 68 |
$this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' ); |
| 69 |
} |
| 70 |
$this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' ); |
| 71 |
$this->output->addModuleStyles( 'oojs-ui.styles.icons-wikimedia' ); |
| 72 |
|
| 73 |
$iwResults = []; |
| 74 |
foreach ( $resultSets as $resultSet ) { |
| 75 |
foreach ( $resultSet as $result ) { |
| 76 |
if ( !$result->isBrokenTitle() ) { |
| 77 |
$iwResults[$result->getTitle()->getInterwiki()][] = $result; |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$iwResultSetPos = 1; |
| 83 |
$iwResultListOutput = ''; |
| 84 |
|
| 85 |
foreach ( $iwResults as $iwPrefix => $results ) { |
| 86 |
|
| 87 |
$position = 0; |
| 88 |
$iwResultItemOutput = ''; |
| 89 |
|
| 90 |
foreach ( $results as $result ) { |
| 91 |
$iwResultItemOutput .= $this->resultWidget->render( $result, $position++ ); |
| 92 |
} |
| 93 |
|
| 94 |
$headerHtml = $this->headerHtml( $term, $iwPrefix ); |
| 95 |
$footerHtml = $this->footerHtml( $term, $iwPrefix ); |
| 96 |
$iwResultListOutput .= Html::rawElement( 'li', |
| 97 |
[ |
| 98 |
'class' => 'iw-resultset', |
| 99 |
'data-iw-resultset-pos' => $iwResultSetPos, |
| 100 |
'data-iw-resultset-source' => $iwPrefix |
| 101 |
], |
| 102 |
|
| 103 |
$headerHtml . |
| 104 |
$iwResultItemOutput . |
| 105 |
$footerHtml |
| 106 |
); |
| 107 |
$iwResultSetPos++; |
| 108 |
} |
| 109 |
|
| 110 |
return Html::rawElement( |
| 111 |
'div', |
| 112 |
[ 'id' => 'mw-interwiki-results' ], |
| 113 |
Html::rawElement( |
| 114 |
'ul', [ 'class' => 'iw-results', ], $iwResultListOutput |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
protected function headerHtml( $term, $iwPrefix ) { |
| 127 |
$href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL( |
| 128 |
[ 'search' => $term, 'fulltext' => 1 ] |
| 129 |
); |
| 130 |
|
| 131 |
$interwiki = $this->iwLookup->fetch( $iwPrefix ); |
| 132 |
|
| 133 |
|
| 134 |
$iwHost = $interwiki ? parse_url( $interwiki->getURL(), PHP_URL_HOST ) : false; |
| 135 |
|
| 136 |
$captionText = $this->customCaptions[$iwPrefix] ?? $iwHost ?: $iwPrefix; |
| 137 |
$searchLink = Html::element( 'a', [ 'href' => $href, 'target' => '_blank' ], $captionText ); |
| 138 |
|
| 139 |
return Html::rawElement( 'div', |
| 140 |
[ 'class' => 'iw-result__header' ], |
| 141 |
$this->iwIcon( $iwPrefix ) . $searchLink ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
protected function footerHtml( $term, $iwPrefix ) { |
| 152 |
$href = Title::makeTitle( NS_SPECIAL, 'Search', '', $iwPrefix )->getLocalURL( |
| 153 |
[ 'search' => $term, 'fulltext' => 1 ] |
| 154 |
); |
| 155 |
|
| 156 |
$captionText = $this->specialSearch->msg( 'search-interwiki-resultset-link' )->text(); |
| 157 |
$searchLink = Html::element( 'a', [ 'href' => $href, 'target' => '_blank' ], $captionText ); |
| 158 |
|
| 159 |
return Html::rawElement( 'div', |
| 160 |
[ 'class' => 'iw-result__footer' ], |
| 161 |
$searchLink ); |
| 162 |
} |
| 163 |
|
| 164 |
protected function loadCustomCaptions() { |
| 165 |
if ( $this->customCaptions !== null ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
$this->customCaptions = []; |
| 170 |
$customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->text() ); |
| 171 |
foreach ( $customLines as $line ) { |
| 172 |
$parts = explode( ':', $line, 2 ); |
| 173 |
if ( count( $parts ) === 2 ) { |
| 174 |
$this->customCaptions[$parts[0]] = $parts[1]; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
protected function iwIcon( $iwPrefix ) { |
| 188 |
$logoName = $this->generateLogoName( $iwPrefix ); |
| 189 |
|
| 190 |
if ( filter_var( $logoName, FILTER_VALIDATE_URL ) || $logoName === "/" ) { |
| 191 |
return $this->generateIconFromFavicon( $logoName ); |
| 192 |
} |
| 193 |
|
| 194 |
$iwIcon = new OOUI\IconWidget( [ |
| 195 |
'icon' => $logoName |
| 196 |
] ); |
| 197 |
|
| 198 |
return $iwIcon; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
protected function generateLogoName( $prefix ) { |
| 212 |
$logoOverridesKeys = array_keys( $this->iwLogoOverrides ); |
| 213 |
if ( in_array( $prefix, $logoOverridesKeys ) ) { |
| 214 |
return $this->iwLogoOverrides[ $prefix ]; |
| 215 |
} |
| 216 |
|
| 217 |
$interwiki = $this->iwLookup->fetch( $prefix ); |
| 218 |
return $interwiki ? $interwiki->getURL() : '/'; |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
protected function generateIconFromFavicon( $logoUrl ) { |
| 228 |
$parsed = wfGetUrlUtils()->parse( (string)wfGetUrlUtils()->expand( $logoUrl, PROTO_CURRENT ) ); |
| 229 |
'@phan-var array $parsed'; |
| 230 |
$iwIconUrl = $parsed['scheme'] . |
| 231 |
$parsed['delimiter'] . |
| 232 |
$parsed['host'] . |
| 233 |
( isset( $parsed['port'] ) ? ':' . $parsed['port'] : '' ) . |
| 234 |
'/favicon.ico'; |
| 235 |
|
| 236 |
$iwIcon = new OOUI\IconWidget( [ |
| 237 |
'icon' => 'favicon' |
| 238 |
] ); |
| 239 |
|
| 240 |
return $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] ); |
| 241 |
} |
| 242 |
} |