From 3562f0afc75dfaf5ba1c467beba9219cda62fbcd Mon Sep 17 00:00:00 2001 From: Stanislav Malyshev Date: Sun, 19 May 2019 16:12:14 +0200 Subject: [PATCH] Enable configuration to supply options for Special:Search form This for example will allow to display descriptions by setting: $wgSpecialSearchFormOptions['showDescriptions'] = true; Bug: T55652 Change-Id: Ifdbca4c508314cb950f2835ee65caea18e0af5b1 --- includes/DefaultSettings.php | 11 +++++++++++ includes/specials/SpecialSearch.php | 5 +++-- includes/widget/SearchInputWidget.php | 8 ++++++++ includes/widget/search/SearchFormWidget.php | 18 ++++++++++++++---- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 69da9c71d9..ff34c36434 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index 4adc2475e1..ed83aafd53 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -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 diff --git a/includes/widget/SearchInputWidget.php b/includes/widget/SearchInputWidget.php index d4ffed2c8c..2f0c23deeb 100644 --- a/includes/widget/SearchInputWidget.php +++ b/includes/widget/SearchInputWidget.php @@ -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 ); } diff --git a/includes/widget/search/SearchFormWidget.php b/includes/widget/search/SearchFormWidget.php index 7c28b5efe7..62ee9cb6f1 100644 --- a/includes/widget/search/SearchFormWidget.php +++ b/includes/widget/search/SearchFormWidget.php @@ -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 { ] ) . '
' . - $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset ) . + $this->shortDialogHtml( $profile, $term, $numResults, $totalResults, $offset, $options ) . '
' . "
" . "
" . @@ -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 ) === '', -- 2.20.1