Extract 'did you mean' widget out of SpecialSearch
authorErik Bernhardson <ebernhardson@wikimedia.org>
Mon, 28 Nov 2016 21:59:03 +0000 (13:59 -0800)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Tue, 17 Jan 2017 23:58:00 +0000 (15:58 -0800)
Bug: T150390
Change-Id: I41d767550ab4112fcd9cc4094b27a14c7d29169b

autoload.php
includes/specials/SpecialSearch.php
includes/widget/search/DidYouMeanWidget.php [new file with mode: 0644]

index ed3cbeb..5d0ca0d 100644 (file)
@@ -933,6 +933,7 @@ $wgAutoloadLocalClasses = [
        'MediaWiki\\Widget\\NamespaceInputWidget' => __DIR__ . '/includes/widget/NamespaceInputWidget.php',
        'MediaWiki\\Widget\\SearchInputWidget' => __DIR__ . '/includes/widget/SearchInputWidget.php',
        'MediaWiki\\Widget\\Search\\BasicSearchResultSetWidget' => __DIR__ . '/includes/widget/search/BasicSearchResultSetWidget.php',
+       'MediaWiki\\Widget\\Search\\DidYouMeanWidget' => __DIR__ . '/includes/widget/search/DidYouMeanWidget.php',
        'MediaWiki\\Widget\\Search\\FullSearchResultWidget' => __DIR__ . '/includes/widget/search/FullSearchResultWidget.php',
        'MediaWiki\\Widget\\Search\\InterwikiSearchResultSetWidget' => __DIR__ . '/includes/widget/search/InterwikiSearchResultSetWidget.php',
        'MediaWiki\\Widget\\Search\\SearchFormWidget' => __DIR__ . '/includes/widget/search/SearchFormWidget.php',
index 735194e..9356a3a 100644 (file)
@@ -340,11 +340,8 @@ class SpecialSearch extends SpecialPage {
 
                // did you mean... suggestions
                if ( $textMatches ) {
-                       if ( $textMatches->hasRewrittenQuery() ) {
-                               $out->addHTML( $this->getDidYouMeanRewrittenHtml( $term, $textMatches ) );
-                       } elseif ( $textMatches->hasSuggestion() ) {
-                               $out->addHTML( $this->getDidYouMeanHtml( $textMatches ) );
-                       }
+                       $dymWidget = new MediaWiki\Widget\Search\DidYouMeanWidget( $this );
+                       $out->addHTML( $dymWidget->render( $term, $textMatches ) );
                }
 
                $out->addHTML( "<div class='searchresults'>" );
@@ -429,93 +426,6 @@ class SpecialSearch extends SpecialPage {
                Hooks::run( 'SpecialSearchResultsAppend', [ $this, $out, $term ] );
        }
 
-       /**
-        * Generates HTML shown to the user when we have a suggestion about a query
-        * that might give more results than their current query.
-        */
-       protected function getDidYouMeanHtml( SearchResultSet $textMatches ) {
-               # mirror Go/Search behavior of original request ..
-               $params = [ 'search' => $textMatches->getSuggestionQuery() ];
-               if ( $this->fulltext === null ) {
-                       $params['fulltext'] = 'Search';
-               } else {
-                       $params['fulltext'] = $this->fulltext;
-               }
-               $stParams = array_merge( $params, $this->powerSearchOptions() );
-
-               $linkRenderer = $this->getLinkRenderer();
-
-               $snippet = $textMatches->getSuggestionSnippet() ?: null;
-               if ( $snippet !== null ) {
-                       $snippet = new HtmlArmor( $snippet );
-               }
-
-               $suggest = $linkRenderer->makeKnownLink(
-                       $this->getPageTitle(),
-                       $snippet,
-                       [ 'id' => 'mw-search-DYM-suggestion' ],
-                       $stParams
-               );
-
-               # HTML of did you mean... search suggestion link
-               return Html::rawElement(
-                       'div',
-                       [ 'class' => 'searchdidyoumean' ],
-                       $this->msg( 'search-suggest' )->rawParams( $suggest )->parse()
-               );
-       }
-
-       /**
-        * Generates HTML shown to user when their query has been internally rewritten,
-        * and the results of the rewritten query are being returned.
-        *
-        * @param string $term The users search input
-        * @param SearchResultSet $textMatches The response to the users initial search request
-        * @return string HTML linking the user to their original $term query, and the one
-        *  suggested by $textMatches.
-        */
-       protected function getDidYouMeanRewrittenHtml( $term, SearchResultSet $textMatches ) {
-               // Showing results for '$rewritten'
-               // Search instead for '$orig'
-
-               $params = [ 'search' => $textMatches->getQueryAfterRewrite() ];
-               if ( $this->fulltext === null ) {
-                       $params['fulltext'] = 'Search';
-               } else {
-                       $params['fulltext'] = $this->fulltext;
-               }
-               $stParams = array_merge( $params, $this->powerSearchOptions() );
-
-               $linkRenderer = $this->getLinkRenderer();
-
-               $snippet = $textMatches->getQueryAfterRewriteSnippet() ?: null;
-               if ( $snippet !== null ) {
-                       $snippet = new HtmlArmor( $snippet );
-               }
-
-               $rewritten = $linkRenderer->makeKnownLink(
-                       $this->getPageTitle(),
-                       $snippet,
-                       [ 'id' => 'mw-search-DYM-rewritten' ],
-                       $stParams
-               );
-
-               $stParams['search'] = $term;
-               $stParams['runsuggestion'] = 0;
-               $original = $linkRenderer->makeKnownLink(
-                       $this->getPageTitle(),
-                       $term,
-                       [ 'id' => 'mw-search-DYM-original' ],
-                       $stParams
-               );
-
-               return Html::rawElement(
-                       'div',
-                       [ 'class' => 'searchdidyoumean' ],
-                       $this->msg( 'search-rewritten' )->rawParams( $rewritten, $original )->escaped()
-               );
-       }
-
        /**
         * @param Title $title
         * @param int $num The number of search results found
@@ -626,10 +536,12 @@ class SpecialSearch extends SpecialPage {
 
        /**
         * Reconstruct the 'power search' options for links
+        * TODO: Instead of exposing this publicly, could we instead expose
+        *  a function for creating search links?
         *
         * @return array
         */
-       protected function powerSearchOptions() {
+       public function powerSearchOptions() {
                $opt = [];
                if ( $this->isPowerSearch() ) {
                        foreach ( $this->namespaces as $n ) {
diff --git a/includes/widget/search/DidYouMeanWidget.php b/includes/widget/search/DidYouMeanWidget.php
new file mode 100644 (file)
index 0000000..3aee87b
--- /dev/null
@@ -0,0 +1,102 @@
+<?php
+
+namespace MediaWiki\Widget\Search;
+
+use Linker;
+use SearchResultSet;
+use SpecialSearch;
+
+/**
+ * Renders a suggested search for the user, or tells the user
+ * a suggested search was run instead of the one provided.
+ */
+class DidYouMeanWidget {
+       /** @var SpecialSearch */
+       protected $specialSearch;
+
+       public function __construct( SpecialSearch $specialSearch ) {
+               $this->specialSearch = $specialSearch;
+       }
+
+       /**
+        * @param string $term The user provided search term
+        * @param SearchResultSet $resultSet
+        * @return string HTML
+        */
+       public function render( $term, SearchResultSet $resultSet ) {
+               if ( $resultSet->hasRewrittenQuery() ) {
+                       $html = $this->rewrittenHtml( $term, $resultSet );
+               } elseif ( $resultSet->hasSuggestion() ) {
+                       $html = $this->suggestionHtml( $resultSet );
+               } else {
+                       return '';
+               }
+
+               return "<div class='searchdidyoumean'>$html</div>";
+       }
+
+       /**
+        * Generates HTML shown to user when their query has been internally
+        * rewritten, and the results of the rewritten query are being returned.
+        *
+        * @param string $term The users search input
+        * @param SearchResultSet $resultSet The response to the search request
+        * @return string HTML Links the user to their original $term query, and the
+        *  one suggested by $resultSet
+        */
+       protected function rewrittenHtml( $term, SearchResultSet $resultSet ) {
+               $params = [
+                       'search' => $resultSet->getQueryAfterRewrite(),
+                       // Don't magic this link into a 'go' link, it should always
+                       // show search results.
+                       'fultext' => 1,
+               ];
+               $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
+
+               $rewritten = Linker::linkKnown(
+                       $this->specialSearch->getPageTitle(),
+                       $resultSet->getQueryAfterRewriteSnippet() ?: null,
+                       [ 'id' => 'mw-search-DYM-rewritten' ],
+                       $stParams
+               );
+
+               $stParams['search'] = $term;
+               $stParams['runsuggestion'] = 0;
+               $original = Linker::linkKnown(
+                       $this->specialSearch->getPageTitle(),
+                       htmlspecialchars( $term, ENT_QUOTES, 'UTF-8' ),
+                       [ 'id' => 'mwsearch-DYM-original' ],
+                       $stParams
+               );
+
+               return $this->specialSearch->msg( 'search-rewritten' )
+                       ->rawParams( $rewritten, $original )
+                       ->escaped();
+       }
+
+       /**
+        * Generates HTML shown to the user when we have a suggestion about
+        * a query that might give more/better results than their current
+        * query.
+        *
+        * @param SearchResultSet $resultSet
+        * @return string HTML
+        */
+       protected function suggestionHtml( SearchResultSet $resultSet ) {
+               $params = [
+                       'search' => $resultSet->getSuggestionQuery(),
+                       'fulltext' => 1,
+               ];
+               $stParams = array_merge( $params, $this->specialSearch->powerSearchOptions() );
+
+               $suggest = Linker::linkKnown(
+                       $this->specialSearch->getPageTitle(),
+                       $resultSet->getSuggestionSnippet() ?: null,
+                       [ 'id' => 'mw-search-DYM-suggestion' ],
+                       $stParams
+               );
+
+               return $this->specialSearch->msg( 'search-suggest' )
+                       ->rawParams( $suggest )->parse();
+       }
+}