Merge "RCFilters: Style the Saved Links placeholder and add a title"
[lhc/web/wiklou.git] / includes / widget / search / InterwikiSearchResultSetWidget.php
index 76b9b01..6d942de 100644 (file)
@@ -8,6 +8,7 @@ use SearchResultSet;
 use SpecialSearch;
 use Title;
 use Html;
+use OOUI;
 
 /**
  * Renders one or more SearchResultSets into a sidebar grouped by
@@ -27,23 +28,22 @@ class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
        protected $iwLookup;
        /** @var $output */
        protected $output;
-       /** @var $iwPrefixDisplayTypes */
-       protected $iwPrefixDisplayTypes;
+       /** @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->iwPrefixDisplayTypes = $specialSearch->getConfig()->get(
-                       'InterwikiPrefixDisplayTypes'
-               );
+               $this->showMultimedia = $showMultimedia;
        }
        /**
         * @param string $term User provided search term
@@ -58,7 +58,9 @@ class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
 
                $this->loadCustomCaptions();
 
-               $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
+               if ( $this->showMultimedia ) {
+                       $this->output->addModules( 'mediawiki.special.search.commonsInterwikiWidget' );
+               }
                $this->output->addModuleStyles( 'mediawiki.special.search.interwikiwidget.styles' );
 
                $iwResults = [];
@@ -80,22 +82,17 @@ class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
                        $position = 0;
                        $iwResultItemOutput = '';
 
-                       $iwDisplayType = isset( $this->iwPrefixDisplayTypes[$iwPrefix] )
-                               ? $this->iwPrefixDisplayTypes[$iwPrefix]
-                               : "";
-
                        foreach ( $results as $result ) {
                                $iwResultItemOutput .= $this->resultWidget->render( $result, $term, $position++ );
                        }
 
-                       $headerHtml = $this->headerHtml( $term, $iwPrefix );
                        $footerHtml = $this->footerHtml( $term, $iwPrefix );
                        $iwResultListOutput .= Html::rawElement( 'li',
                                [
-                                       'class' => 'iw-resultset iw-resultset--' . $iwDisplayType,
+                                       'class' => 'iw-resultset',
                                        'data-iw-resultset-pos' => $iwResultSetPos
                                ],
-                               $headerHtml .
+
                                $iwResultItemOutput .
                                $footerHtml
                        );
@@ -117,34 +114,6 @@ class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
                );
        }
 
-       /**
-        * Generates an appropriate HTML header for the given interwiki prefix
-        *
-        * @param string $term User provided search term
-        * @param string $iwPrefix Interwiki prefix of wiki to show header for
-        * @return string HTML
-        */
-       protected function headerHtml( $term, $iwPrefix ) {
-
-               $iwDisplayType = isset( $this->iwPrefixDisplayTypes[$iwPrefix] )
-                       ? $this->iwPrefixDisplayTypes[$iwPrefix]
-                       : "";
-
-               if ( isset( $this->customCaptions[$iwPrefix] ) ) {
-                       /* customCaptions composed by loadCustomCaptions() with pre-escaped content.*/
-                       $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'] )->escaped();
-               }
-
-               return Html::rawElement( 'div', [ 'class' => 'iw-result__header' ],
-                       Html::rawElement( 'span', [ 'class' => 'iw-result__icon iw-result__icon--' . $iwDisplayType ] )
-                       . $caption
-               );
-       }
-
        /**
         * Generates an HTML footer for the given interwiki prefix
         *
@@ -158,13 +127,22 @@ class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
                        [ 'search' => $term, 'fulltext' => 1 ]
                );
 
-               $searchLink = Html::rawElement(
-                       'a',
-                       [ 'href' => $href ],
-                       $this->specialSearch->msg( 'search-interwiki-more' )->escaped()
+               $interwiki = $this->iwLookup->fetch( $iwPrefix );
+               $parsed = wfParseUrl( wfExpandUrl( $interwiki ? $interwiki->getURL() : '/' ) );
+
+               if ( isset( $this->customCaptions[$iwPrefix] ) ) {
+                       $caption = $this->customCaptions[$iwPrefix];
+               } else {
+                       $caption = $this->specialSearch->msg( 'search-interwiki-default', $parsed['host'] )->escaped();
+               }
+
+               $searchLink = Html::rawElement( 'em', null,
+                       Html::rawElement( 'a', [ 'href' => $href, 'target' => '_blank' ], $caption )
                );
 
-               return Html::rawElement( 'div', [ 'class' => 'iw-result__footer' ], $searchLink );
+               return Html::rawElement( 'div',
+                       [ 'class' => 'iw-result__footer' ],
+                       $this->iwIcon( $iwPrefix ) . $searchLink );
        }
 
        protected function loadCustomCaptions() {
@@ -181,4 +159,33 @@ class InterwikiSearchResultSetWidget implements SearchResultSetWidget {
                        }
                }
        }
+
+       /**
+        * 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;
+       }
 }