Merge "mediawiki.api.watch: Use formatversion=2 for API requests"
[lhc/web/wiklou.git] / includes / specials / SpecialSearch.php
index 252ac31..727179a 100644 (file)
@@ -234,11 +234,6 @@ class SpecialSearch extends SpecialPage {
 
                        return;
                }
-               # No match, generate an edit URL
-               $title = Title::newFromText( $term );
-               if ( !is_null( $title ) ) {
-                       Hooks::run( 'SpecialSearchNogomatch', [ &$title ] );
-               }
                $this->showResults( $term );
        }
 
@@ -300,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() ) {
@@ -365,6 +360,25 @@ class SpecialSearch extends SpecialPage {
 
                $out->addHTML( "<div class='searchresults'>" );
 
+               $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 ) {
@@ -393,7 +407,8 @@ class SpecialSearch extends SpecialPage {
                        }
                        $titleMatches->free();
                }
-               if ( $textMatches && !$textStatus ) {
+
+               if ( $textMatches ) {
                        // output appropriate heading
                        if ( $numTextMatches > 0 && $numTitleMatches > 0 ) {
                                $out->addHTML( '<div class="mw-search-visualclear"></div>' );
@@ -417,17 +432,18 @@ class SpecialSearch extends SpecialPage {
                $hasOtherResults = $textMatches &&
                        $textMatches->hasInterwikiResults( SearchResultSet::INLINE_RESULTS );
 
-               if ( $num === 0 ) {
-                       if ( $textStatus ) {
-                               $out->addHTML( '<div class="error">' .
-                                       $textStatus->getMessage( 'search-error' ) . '</div>' );
-                       } else {
+               // 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( "<p class=\"mw-search-nonefound\">\n$1</p>",
-                                       [ $hasOtherResults ? 'search-nonefound-thiswiki' : 'search-nonefound',
-                                                       wfEscapeWikiText( $term )
-                                       ] );
                        }
+                       $out->wrapWikiMsg( "<p class=\"mw-search-nonefound\">\n$1</p>", [
+                               $hasOtherResults ? 'search-nonefound-thiswiki' : 'search-nonefound',
+                               wfEscapeWikiText( $term )
+                       ] );
                }
 
                if ( $hasOtherResults ) {
@@ -469,25 +485,6 @@ class SpecialSearch extends SpecialPage {
                return "<p class=\"mw-search-interwiki-header mw-search-visualclear\">\n$wikiMsg</p>";
        }
 
-       /**
-        * Decide if the suggested query should be run, and it's results returned
-        * instead of the provided $textMatches
-        *
-        * @param SearchResultSet $textMatches The results of a users query
-        * @return bool
-        */
-       protected function shouldRunSuggestedQuery( SearchResultSet $textMatches ) {
-               if ( !$this->runSuggestion ||
-                       !$textMatches->hasSuggestion() ||
-                       $textMatches->numRows() > 0 ||
-                       $textMatches->searchContainedSyntax()
-               ) {
-                       return false;
-               }
-
-               return $this->getConfig()->get( 'SearchRunSuggestedQuery' );
-       }
-
        /**
         * Generates HTML shown to the user when we have a suggestion about a query
         * that might give more results than their current query.
@@ -502,9 +499,16 @@ class SpecialSearch extends SpecialPage {
                }
                $stParams = array_merge( $params, $this->powerSearchOptions() );
 
-               $suggest = Linker::linkKnown(
+               $linkRenderer = $this->getLinkRenderer();
+
+               $snippet = $textMatches->getSuggestionSnippet() ?: null;
+               if ( $snippet !== null ) {
+                       $snippet = new HtmlArmor( $snippet );
+               }
+
+               $suggest = $linkRenderer->makeKnownLink(
                        $this->getPageTitle(),
-                       $textMatches->getSuggestionSnippet() ?: null,
+                       $snippet,
                        [ 'id' => 'mw-search-DYM-suggestion' ],
                        $stParams
                );
@@ -538,18 +542,25 @@ class SpecialSearch extends SpecialPage {
                }
                $stParams = array_merge( $params, $this->powerSearchOptions() );
 
-               $rewritten = Linker::linkKnown(
+               $linkRenderer = $this->getLinkRenderer();
+
+               $snippet = $textMatches->getQueryAfterRewriteSnippet() ?: null;
+               if ( $snippet !== null ) {
+                       $snippet = new HtmlArmor( $snippet );
+               }
+
+               $rewritten = $linkRenderer->makeKnownLink(
                        $this->getPageTitle(),
-                       $textMatches->getQueryAfterRewriteSnippet() ?: null,
+                       $snippet,
                        [ 'id' => 'mw-search-DYM-rewritten' ],
                        $stParams
                );
 
                $stParams['search'] = $term;
                $stParams['runsuggestion'] = 0;
-               $original = Linker::linkKnown(
+               $original = $linkRenderer->makeKnownLink(
                        $this->getPageTitle(),
-                       htmlspecialchars( $term ),
+                       $term,
                        [ 'id' => 'mw-search-DYM-original' ],
                        $stParams
                );
@@ -769,7 +780,13 @@ class SpecialSearch extends SpecialPage {
                Hooks::run( 'ShowSearchHitTitle',
                        [ &$link_t, &$titleSnippet, $result, $terms, $this, &$query ] );
 
-               $link = Linker::linkKnown(
+               $linkRenderer = $this->getLinkRenderer();
+
+               if ( $titleSnippet !== null ) {
+                       $titleSnippet = new HtmlArmor( $titleSnippet );
+               }
+
+               $link = $linkRenderer->makeKnownLink(
                        $link_t,
                        $titleSnippet,
                        [ 'data-serp-pos' => $position ], // HTML attributes
@@ -803,9 +820,13 @@ class SpecialSearch extends SpecialPage {
                                $redirectText = null;
                        }
 
+                       if ( $redirectText !== null ) {
+                               $redirectText = new HtmlArmor( $redirectText );
+                       }
+
                        $redirect = "<span class='searchalttitle'>" .
                                $this->msg( 'search-redirect' )->rawParams(
-                                       Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
+                                       $linkRenderer->makeKnownLink( $redirectTitle, $redirectText ) )->text() .
                                "</span>";
                }
 
@@ -815,9 +836,13 @@ class SpecialSearch extends SpecialPage {
                                $sectionText = null;
                        }
 
+                       if ( $sectionText !== null ) {
+                               $sectionText = new HtmlArmor( $sectionText );
+                       }
+
                        $section = "<span class='searchalttitle'>" .
                                $this->msg( 'search-section' )->rawParams(
-                                       Linker::linkKnown( $sectionTitle, $sectionText ) )->text() .
+                                       $linkRenderer->makeKnownLink( $sectionTitle, $sectionText ) )->text() .
                                "</span>";
                }
 
@@ -974,6 +999,8 @@ class SpecialSearch extends SpecialPage {
                        return '';
                }
 
+               $linkRenderer = $this->getLinkRenderer();
+
                $title = $result->getTitle();
 
                $titleSnippet = $result->getTitleSnippet();
@@ -982,7 +1009,11 @@ class SpecialSearch extends SpecialPage {
                        $titleSnippet = null;
                }
 
-               $link = Linker::linkKnown(
+               if ( $titleSnippet !== null ) {
+                       $titleSnippet = new HtmlArmor( $titleSnippet );
+               }
+
+               $link = $linkRenderer->makeKnownLink(
                        $title,
                        $titleSnippet
                );
@@ -996,9 +1027,13 @@ class SpecialSearch extends SpecialPage {
                                $redirectText = null;
                        }
 
+                       if ( $redirectText !== null ) {
+                               $redirectText = new HtmlArmor( $redirectText );
+                       }
+
                        $redirect = "<span class='searchalttitle'>" .
                                $this->msg( 'search-redirect' )->rawParams(
-                                       Linker::linkKnown( $redirectTitle, $redirectText ) )->text() .
+                                       $linkRenderer->makeKnownLink( $redirectTitle, $redirectText ) )->text() .
                                "</span>";
                }
 
@@ -1016,7 +1051,7 @@ class SpecialSearch extends SpecialPage {
                        }
                        // "more results" link (special page stuff could be localized, but we might not know target lang)
                        $searchTitle = Title::newFromText( $title->getInterwiki() . ":Special:Search" );
-                       $searchLink = Linker::linkKnown(
+                       $searchLink = $linkRenderer->makeKnownLink(
                                $searchTitle,
                                $this->msg( 'search-interwiki-more' )->text(),
                                [],
@@ -1332,23 +1367,6 @@ class SpecialSearch extends SpecialPage {
                return false;
        }
 
-       /**
-        * Check if query starts with all: prefix
-        *
-        * @param string $term The string to check
-        * @return bool
-        */
-       protected function startsWithAll( $term ) {
-               $allkeyword = $this->msg( 'searchall' )->inContentLanguage()->text();
-
-               $parts = explode( ':', $term );
-               if ( count( $parts ) > 1 ) {
-                       return $parts[0] == $allkeyword;
-               }
-
-               return false;
-       }
-
        /**
         * @since 1.18
         *