From 362be5bb90ec45fddeeeb5f7860b601c114d62ac Mon Sep 17 00:00:00 2001 From: Erik Bernhardson Date: Tue, 8 Nov 2016 16:41:11 -0800 Subject: [PATCH] Accept 'OK' status results from search engine Historically code that talks to the SearchEngine has been treating anything other than a 'Good' result, such as results but with a warning (perhaps the query timed out and only gave partial results) as a complete failure. Adjust the handling to report results from an 'OK' status response while attaching any warnings to the api response, or in a notiable red box on Special:Search. Bug: T134157 Change-Id: Ic5e0db727790f4fd189caa54ea5f01672d6a8ea4 --- includes/api/ApiQuerySearch.php | 21 ++++++++++-- includes/specials/SpecialSearch.php | 52 +++++++++++++++++++---------- languages/i18n/en.json | 1 + languages/i18n/qqq.json | 3 +- 4 files changed, 55 insertions(+), 22 deletions(-) diff --git a/includes/api/ApiQuerySearch.php b/includes/api/ApiQuerySearch.php index 6be51986a6..9962d5ec20 100644 --- a/includes/api/ApiQuerySearch.php +++ b/includes/api/ApiQuerySearch.php @@ -107,10 +107,25 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { $matches = $search->searchText( $query ); } } - if ( is_null( $matches ) ) { + + if ( $matches instanceof Status ) { + $status = $matches; + $matches = $status->getValue(); + } else { + $status = null; + } + + if ( $status ) { + if ( $status->isOK() ) { + $this->getMain()->getErrorFormatter()->addMessagesFromStatus( + $this->getModuleName(), + $status + ); + } else { + $this->dieUsage( $status->getWikiText( false, false, 'en' ), 'search-error' ); + } + } elseif ( is_null( $matches ) ) { $this->dieUsage( "{$what} search is disabled", "search-{$what}-disabled" ); - } elseif ( $matches instanceof Status && !$matches->isGood() ) { - $this->dieUsage( $matches->getWikiText( false, false, 'en' ), 'search-error' ); } if ( $resultPageSet === null ) { diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index 9280b0478f..9f83832e13 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -295,12 +295,12 @@ class SpecialSearch extends SpecialPage { $textStatus = null; if ( $textMatches instanceof Status ) { $textStatus = $textMatches; - $textMatches = null; + $textMatches = $textStatus->getValue(); } // did you mean... suggestions $didYouMeanHtml = ''; - if ( $showSuggestion && $textMatches && !$textStatus ) { + if ( $showSuggestion && $textMatches ) { if ( $textMatches->hasRewrittenQuery() ) { $didYouMeanHtml = $this->getDidYouMeanRewrittenHtml( $term, $textMatches ); } elseif ( $textMatches->hasSuggestion() ) { @@ -360,6 +360,25 @@ class SpecialSearch extends SpecialPage { $out->addHTML( "
" ); + $hasErrors = $textStatus && $textStatus->getErrors(); + if ( $hasErrors ) { + list( $error, $warning ) = $textStatus->splitByErrorType(); + if ( $error->getErrors() ) { + $out->addHTML( Html::rawElement( + 'div', + [ 'class' => 'errorbox' ], + $error->getHTML( 'search-error' ) + ) ); + } + if ( $warning->getErrors() ) { + $out->addHTML( Html::rawElement( + 'div', + [ 'class' => 'warningbox' ], + $warning->getHTML( 'search-warning' ) + ) ); + } + } + // prev/next links $prevnext = null; if ( $num || $this->offset ) { @@ -388,7 +407,8 @@ class SpecialSearch extends SpecialPage { } $titleMatches->free(); } - if ( $textMatches && !$textStatus ) { + + if ( $textMatches ) { // output appropriate heading if ( $numTextMatches > 0 && $numTitleMatches > 0 ) { $out->addHTML( '
' ); @@ -412,22 +432,18 @@ class SpecialSearch extends SpecialPage { $hasOtherResults = $textMatches && $textMatches->hasInterwikiResults( SearchResultSet::INLINE_RESULTS ); - if ( $num === 0 ) { - if ( $textStatus ) { - $out->addHTML( '
' . - $textStatus->getMessage( 'search-error' ) . '
' ); - } else { - if ( !$this->offset ) { - // If we have an offset the create link was rendered earlier in this function. - // This class needs a good de-spaghettification, but for now this will - // do the job. - $this->showCreateLink( $title, $num, $titleMatches, $textMatches ); - } - $out->wrapWikiMsg( "

\n$1

", - [ $hasOtherResults ? 'search-nonefound-thiswiki' : 'search-nonefound', - wfEscapeWikiText( $term ) - ] ); + // If we have no results and we have not already displayed an error message + if ( $num === 0 && !$hasErrors ) { + if ( !$this->offset ) { + // If we have an offset the create link was rendered earlier in this function. + // This class needs a good de-spaghettification, but for now this will + // do the job. + $this->showCreateLink( $title, $num, $titleMatches, $textMatches ); } + $out->wrapWikiMsg( "

\n$1

", [ + $hasOtherResults ? 'search-nonefound-thiswiki' : 'search-nonefound', + wfEscapeWikiText( $term ) + ] ); } if ( $hasOtherResults ) { diff --git a/languages/i18n/en.json b/languages/i18n/en.json index a033eddb69..d3643dd398 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -1021,6 +1021,7 @@ "searchdisabled": "{{SITENAME}} search is disabled.\nYou can search via Google in the meantime.\nNote that their indexes of {{SITENAME}} content may be out of date.", "googlesearch": "
\n\t\n\t\n\t\n\t\n\n\t\n\t\n
\n\t\n\t\n
\n
", "search-error": "An error has occurred while searching: $1", + "search-warning": "A warning has occured while searching: $1", "opensearch-desc": "{{SITENAME}} ({{CONTENTLANGUAGE}})", "preferences": "Preferences", "preferences-summary": "", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 8e2b441b32..b9a28c5726 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -1204,7 +1204,8 @@ "search-external": "Legend of the fieldset for the input form when the internal search is disabled. Inside the fieldset [[MediaWiki:Searchdisabled]] and [[MediaWiki:Googlesearch]] is shown.", "searchdisabled": "{{doc-singularthey}}\nIn this sentence, \"their indexes\" refers to \"Google's indexes\".\n\nShown on [[Special:Search]] when the internal search is disabled.", "googlesearch": "{{notranslate}}\nShown when [[mw:Manual:$wgDisableTextSearch|$wgDisableTextSearch]] is set to true and no [[mw:Manual:$wgSearchForwardUrl|$wgSearchForwardUrl]] is set.\n\nParameters:\n* $1 - the search term\n* $2 - \"UTF-8\" (hard-coded)\n* $3 - the message {{msg-mw|Searchbutton}}", - "search-error": "Shown when an error has occurred when performing a search. Parameters:\n* $1 - the localized error that was returned", + "search-error": "Shown when an error has occurred when performing a search. Parameters:\n* $1 - the localized error that was returned.", + "search-warning": "Shown when a warning has occured when performing a search. Parameters:\n* $1 - the localized warning that was returned.", "opensearch-desc": "{{ignored}}Link description of the [www.opensearch.org/ OpenSearch] link in the HTML head of pages.", "preferences": "Title of the [[Special:Preferences]] page.\n{{Identical|Preferences}}", "preferences-summary": "{{doc-specialpagesummary|preferences}}", -- 2.20.1