Enable configuration to supply options for Special:Search form
authorStanislav Malyshev <smalyshev@gmail.com>
Sun, 19 May 2019 14:12:14 +0000 (16:12 +0200)
committerStanislav Malyshev <smalyshev@gmail.com>
Thu, 23 May 2019 00:30:53 +0000 (17:30 -0700)
This for example will allow to display descriptions by setting:

$wgSpecialSearchFormOptions['showDescriptions'] = true;

Bug: T55652

Change-Id: Ifdbca4c508314cb950f2835ee65caea18e0af5b1

includes/DefaultSettings.php
includes/specials/SpecialSearch.php
includes/widget/SearchInputWidget.php
includes/widget/search/SearchFormWidget.php

index 69da9c7..ff34c36 100644 (file)
@@ -9082,6 +9082,17 @@ $wgReportToEndpoints = [];
  */
 $wgFeaturePolicyReportOnly = [];
 
+/**
+ * Options for Special:Search completion widget form created by SearchFormWidget class.
+ * Settings that can be used:
+ * - showDescriptions: true/false - whether to show opensearch description results
+ * - performSearchOnClick:  true/false - whether to perform search on click
+ * See also TitleWidget.js UI widget.
+ * @since 1.34
+ * @var array
+ */
+$wgSpecialSearchFormOptions = [];
+
 /**
  * For really cool vim folding this needs to be at the end:
  * vim: foldmarker=@{,@} foldmethod=marker
index 4adc247..ed83aaf 100644 (file)
@@ -290,6 +290,7 @@ class SpecialSearch extends SpecialPage {
                }
 
                $out = $this->getOutput();
+               $widgetOptions = $this->getConfig()->get( 'SpecialSearchFormOptions' );
                $formWidget = new MediaWiki\Widget\Search\SearchFormWidget(
                        $this,
                        $this->searchConfig,
@@ -308,7 +309,7 @@ class SpecialSearch extends SpecialPage {
                        // only do the form render here for the empty $term case. Rendering
                        // the form when a search is provided is repeated below.
                        $out->addHTML( $formWidget->render(
-                               $this->profile, $term, 0, 0, $this->offset, $this->isPowerSearch()
+                               $this->profile, $term, 0, 0, $this->offset, $this->isPowerSearch(), $widgetOptions
                        ) );
                        return;
                }
@@ -365,7 +366,7 @@ class SpecialSearch extends SpecialPage {
                // start rendering the page
                $out->enableOOUI();
                $out->addHTML( $formWidget->render(
-                       $this->profile, $term, $num, $totalRes, $this->offset, $this->isPowerSearch()
+                       $this->profile, $term, $num, $totalRes, $this->offset, $this->isPowerSearch(), $widgetOptions
                ) );
 
                // did you mean... suggestions
index d4ffed2..2f0c23d 100644 (file)
@@ -14,6 +14,7 @@ class SearchInputWidget extends TitleInputWidget {
        protected $validateTitle = false;
        protected $highlightFirst = false;
        protected $dataLocation = 'header';
+       protected $showDescriptions = false;
 
        /**
         * @param array $config Configuration options
@@ -41,6 +42,10 @@ class SearchInputWidget extends TitleInputWidget {
                        $this->dataLocation = $config['dataLocation'];
                }
 
+               if ( !empty( $config['showDescriptions'] ) ) {
+                       $this->showDescriptions = true;
+               }
+
                // Initialization
                $this->addClasses( [ 'mw-widget-searchInputWidget' ] );
        }
@@ -58,6 +63,9 @@ class SearchInputWidget extends TitleInputWidget {
                if ( $this->dataLocation ) {
                        $config['dataLocation'] = $this->dataLocation;
                }
+               if ( $this->showDescriptions ) {
+                       $config['showDescriptions'] = true;
+               }
                $config['$overlay'] = true;
                return parent::getConfig( $config );
        }
index 7c28b5e..62ee9cb 100644 (file)
@@ -40,6 +40,7 @@ class SearchFormWidget {
         * @param int $totalResults The total estimated results found
         * @param int $offset Current offset in search results
         * @param bool $isPowerSearch Is the 'advanced' section open?
+        * @param array $options Widget options
         * @return string HTML
         */
        public function render(
@@ -48,7 +49,8 @@ class SearchFormWidget {
                $numResults,
                $totalResults,
                $offset,
-               $isPowerSearch
+               $isPowerSearch,
+               array $options = []
        ) {
                $user = $this->specialSearch->getUser();
 
@@ -63,7 +65,7 @@ class SearchFormWidget {
                                ]
                        ) .
                                '<div id="mw-search-top-table">' .
-                                       $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) .
+                                       $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset, $options ) .
                                '</div>' .
                                "<div class='mw-search-visualclear'></div>" .
                                "<div class='mw-search-profile-tabs'>" .
@@ -81,12 +83,20 @@ class SearchFormWidget {
         * @param int $numResults The number of results shown
         * @param int $totalResults The total estimated results found
         * @param int $offset Current offset in search results
+        * @param array $options Widget options
         * @return string HTML
         */
-       protected function shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) {
+       protected function shortDialogHtml(
+               $profile,
+               $term,
+               $numResults,
+               $totalResults,
+               $offset,
+               array $options = []
+       ) {
                $html = '';
 
-               $searchWidget = new SearchInputWidget( [
+               $searchWidget = new SearchInputWidget( $options + [
                        'id' => 'searchText',
                        'name' => 'search',
                        'autofocus' => trim( $term ) === '',