X-Git-Url: https://git.cyclocoop.org/%27.WWW_URL.%27admin/?a=blobdiff_plain;f=includes%2Fwidget%2Fsearch%2FInterwikiSearchResultSetWidget.php;h=6d942de1ab9a9f628f5b61ca24d1242d9c629bfb;hb=36127c29c2d564cc723b6e1cfccca9ba1a0240c8;hp=c7384835621939e77a40e3806966355185708da2;hpb=5b1dcdc344f5d251120a161637bcb89e01b0f6a4;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/widget/search/InterwikiSearchResultSetWidget.php b/includes/widget/search/InterwikiSearchResultSetWidget.php index c738483562..6d942de1ab 100644 --- a/includes/widget/search/InterwikiSearchResultSetWidget.php +++ b/includes/widget/search/InterwikiSearchResultSetWidget.php @@ -7,13 +7,15 @@ use MediaWiki\Linker\LinkRenderer; use SearchResultSet; use SpecialSearch; use Title; +use Html; +use OOUI; /** * Renders one or more SearchResultSets into a sidebar grouped by * interwiki prefix. Includes a per-wiki header indicating where * the results are from. */ -class InterwikiSearchResultSetWidget { +class InterwikiSearchResultSetWidget implements SearchResultSetWidget { /** @var SpecialSearch */ protected $specialSearch; /** @var SearchResultWidget */ @@ -24,19 +26,25 @@ class InterwikiSearchResultSetWidget { protected $linkRenderer; /** @var InterwikiLookup */ protected $iwLookup; + /** @var $output */ + protected $output; + /** @var bool $showMultimedia */ + protected $showMultimedia; public function __construct( SpecialSearch $specialSearch, SearchResultWidget $resultWidget, LinkRenderer $linkRenderer, - InterwikiLookup $iwLookup + InterwikiLookup $iwLookup, + $showMultimedia = false ) { $this->specialSearch = $specialSearch; $this->resultWidget = $resultWidget; $this->linkRenderer = $linkRenderer; $this->iwLookup = $iwLookup; + $this->output = $specialSearch->getOutput(); + $this->showMultimedia = $showMultimedia; } - /** * @param string $term User provided search term * @param SearchResultSet|SearchResultSet[] $resultSets List of interwiki @@ -50,6 +58,11 @@ class InterwikiSearchResultSetWidget { $this->loadCustomCaptions(); + if ( $this->showMultimedia ) { + $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' ); + } + $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' ); + $iwResults = []; foreach ( $resultSets as $resultSet ) { $result = $resultSet->next(); @@ -61,56 +74,75 @@ class InterwikiSearchResultSetWidget { } } - $out = ''; + $iwResultSetPos = 1; + $iwResultListOutput = ''; + foreach ( $iwResults as $iwPrefix => $results ) { - $out .= $this->headerHtml( $iwPrefix, $term ); - $out .= ""; + + $footerHtml = $this->footerHtml( $term, $iwPrefix ); + $iwResultListOutput .= Html::rawElement( 'li', + [ + 'class' => 'iw-resultset', + 'data-iw-resultset-pos' => $iwResultSetPos + ], + + $iwResultItemOutput . + $footerHtml + ); + + $iwResultSetPos++; } - return - "
" . - "
" . - $this->specialSearch->msg( 'search-interwiki-caption' )->text() . - '
' . - $out . - "
"; + return Html::rawElement( + 'div', + [ 'id' => 'mw-interwiki-results' ], + Html::rawElement( + 'p', + [ 'class' => 'iw-headline' ], + $this->specialSearch->msg( 'search-interwiki-caption' )->parse() + ) . + Html::rawElement( + 'ul', [ 'class' => 'iw-results', ], $iwResultListOutput + ) + ); } /** - * Generates an appropriate HTML header for the given interwiki prefix + * Generates an HTML footer for the given interwiki prefix * - * @param string $iwPrefix Interwiki prefix of wiki to show header for * @param string $term User provided search term + * @param string $iwPrefix Interwiki prefix of wiki to show footer for * @return string HTML */ - protected function headerHtml( $iwPrefix, $term ) { + protected function footerHtml( $term, $iwPrefix ) { + + $href = Title::makeTitle( NS_SPECIAL, 'Search', null, $iwPrefix )->getLocalURL( + [ 'search' => $term, 'fulltext' => 1 ] + ); + + $interwiki = $this->iwLookup->fetch( $iwPrefix ); + $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) ); + if ( isset( $this->customCaptions[$iwPrefix] ) ) { $caption = $this->customCaptions[$iwPrefix]; } else { - $interwiki = $this->iwLookup->fetch( $iwPrefix ); - $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) ); - $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->text(); + $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped(); } - $searchLink = $this->linkRenderer->makeLink( - Title::newFromText( "$iwPrefix:Special:Search" ), - $this->specialSearch->msg( 'search-interwiki-more' )->text(), - [], - [ - 'search' => $term, - 'fulltext' => 1, - ] + + $searchLink = Html::rawElement( 'em', null, + Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption ) ); - return - "
" . - "{$searchLink}" . - $caption . - "
"; + + return Html::rawElement( 'div', + [ 'class' => 'iw-result__footer' ], + $this->iwIcon( $iwPrefix ) . $searchLink ); } protected function loadCustomCaptions() { @@ -119,7 +151,7 @@ class InterwikiSearchResultSetWidget { } $this->customCaptions = []; - $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->text() ); + $customLines = explode( "\n", $this->specialSearch->msg( 'search-interwiki-custom' )->escaped() ); foreach ( $customLines as $line ) { $parts = explode( ':', $line, 2 ); if ( count( $parts ) === 2 ) { @@ -127,4 +159,33 @@ class InterwikiSearchResultSetWidget { } } } + + /** + * Generates a custom OOUI icon element with a favicon as the image. + * The favicon image URL is generated by parsing the interwiki URL + * and returning the default location of the favicon for that domain, + * which is assumed to be '/favicon.ico'. + * + * @param string $iwPrefix Interwiki prefix + * @return OOUI\IconWidget + **/ + protected function iwIcon( $iwPrefix ) { + + $interwiki = $this->iwLookup->fetch( $iwPrefix ); + $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) ); + + $iwIconUrl = $parsed['scheme'] . + $parsed['delimiter'] . + $parsed['host'] . + ( $parsed['port'] ? ':' . $parsed['port'] : '' ) . + '/favicon.ico'; + + $iwIcon = new OOUI\IconWidget( [ + 'icon' => 'favicon' + ] ); + + $iwIcon->setAttributes( [ 'style' => "background-image:url($iwIconUrl);" ] ); + + return $iwIcon; + } }