Smalyshev has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/241129

Change subject: [WIP] Displaying search results for multiple wikis
......................................................................

[WIP] Displaying search results for multiple wikis

Requires Ic4bdd9462f844febea2e402e4b89d9dcc4c836b6

Bug: T112349
Change-Id: Ib6524bc79e1648ccf6adc5745f0dbc4c26dcb0d2
---
M includes/search/SearchResultSet.php
M includes/specials/SpecialSearch.php
M languages/i18n/en.json
M languages/i18n/qqq.json
4 files changed, 85 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/29/241129/1

diff --git a/includes/search/SearchResultSet.php 
b/includes/search/SearchResultSet.php
index 8d18b0e..c7b0f32 100644
--- a/includes/search/SearchResultSet.php
+++ b/includes/search/SearchResultSet.php
@@ -25,6 +25,21 @@
  * @ingroup Search
  */
 class SearchResultSet {
+
+       /**
+        * Types of interwiki results
+        */
+       /**
+        * Results that are displayed only together with existing main wiki 
results
+        * @var int
+        */
+       const SECONDARY_RESULTS = 0;
+       /**
+        * Results that can displayed even if no existing main wiki results 
exist
+        * @var int
+        */
+       const INLINE_RESULTS = 1;
+
        protected $containedSyntax = false;
 
        public function __construct( $containedSyntax = false ) {
@@ -116,7 +131,7 @@
         *
         * @return SearchResultSet
         */
-       function getInterwikiResults() {
+       function getInterwikiResults( $type = self::SECONDARY_RESULTS ) {
                return null;
        }
 
@@ -125,8 +140,8 @@
         *
         * @return bool
         */
-       function hasInterwikiResults() {
-               return $this->getInterwikiResults() != null;
+       function hasInterwikiResults( $type = self::SECONDARY_RESULTS ) {
+               return false;
        }
 
        /**
diff --git a/includes/specials/SpecialSearch.php 
b/includes/specials/SpecialSearch.php
index 06cad42..ecee0df 100644
--- a/includes/specials/SpecialSearch.php
+++ b/includes/specials/SpecialSearch.php
@@ -73,6 +73,12 @@
         */
        protected $runSuggestion = true;
 
+       /**
+        * Names of the wikis
+        * @var array
+        */
+       protected $customCaptions;
+
        const NAMESPACES_CURRENT = 'sense';
 
        public function __construct() {
@@ -370,25 +376,41 @@
                        if ( $numTextMatches > 0 ) {
                                $out->addHTML( $this->showMatches( $textMatches 
) );
                        }
-                       // show interwiki results if any
-                       if ( $textMatches->hasInterwikiResults() ) {
-                               $out->addHTML( $this->showInterwiki( 
$textMatches->getInterwikiResults(), $term ) );
+
+                       // show secondary interwiki results if any
+                       if ( $textMatches->hasInterwikiResults( 
SearchResultSet::SECONDARY_RESULTS ) ) {
+                               $out->addHTML( $this->showInterwiki( 
$textMatches->getInterwikiResults( SearchResultSet::SECONDARY_RESULTS ), $term 
) );
                        }
 
                        $textMatches->free();
                }
+
+               $hasOtherResults = $textMatches->hasInterwikiResults( 
SearchResultSet::INLINE_RESULTS );
+
                if ( $num === 0 ) {
                        if ( $textStatus ) {
                                $out->addHTML( '<div class="error">' .
                                        $textStatus->getMessage( 'search-error' 
) . '</div>' );
                        } else {
-                               $out->wrapWikiMsg( "<p 
class=\"mw-search-nonefound\">\n$1</p>",
-                                       array( 'search-nonefound', 
wfEscapeWikiText( $term ) ) );
                                $this->showCreateLink( $title, $num, 
$titleMatches, $textMatches );
+                               $out->wrapWikiMsg( "<p 
class=\"mw-search-nonefound\">\n$1</p>",
+                                       array( $hasOtherResults ? 
'search-nonefound-thiswiki' : 'search-nonefound', wfEscapeWikiText( $term ) ) );
+                       }
+               }
+
+               if( $hasOtherResults ) {
+                       foreach( $textMatches->getInterwikiResults( 
SearchResultSet::INLINE_RESULTS ) as $interwiki => $interwikiResult ) {
+                               if( $interwikiResult instanceof Status || 
$interwikiResult->numRows() == 0 ) {
+                                       // ignore bad interwikis for now
+                                       continue;
+                               }
+                               // TODO: wiki header
+                               $out->addHTML( $this->showMatches( 
$interwikiResult, $interwiki ) );
                        }
                }
 
                $out->addHTML( '<div class="visualClear"></div>' );
+
                if ( $prevnext ) {
                        $out->addHTML( "<p 
class='mw-search-pager-bottom'>{$prevnext}</p>\n" );
                }
@@ -397,6 +419,16 @@
 
                Hooks::run( 'SpecialSearchResultsAppend', array( $this, $out ) 
);
 
+       }
+
+       /**
+        * Produce wiki header for interwiki results
+        * @param string $interwiki
+        */
+       protected function wikiHeader( $interwiki, $interwikiResult ) {
+               // TODO: we need to figure out how to name wikis correctly
+               $wikiMsg = $this->msg( 'search-interwiki-results-' . $interwiki 
)->parse();
+               return "<p class=\"mw-search-nonefound\">\n$wikiMsg</p>";
        }
 
        /**
@@ -641,13 +673,18 @@
         *
         * @return string
         */
-       protected function showMatches( &$matches ) {
+       protected function showMatches( &$matches, $interwiki = null ) {
                global $wgContLang;
 
                $terms = $wgContLang->convertForSearchResult( 
$matches->termMatches() );
-
-               $out = "<ul class='mw-search-results'>\n";
+               $out = '';
                $result = $matches->next();
+
+               if( $result && $interwiki ) {
+                       $out .= $this->wikiHeader( $interwiki, $result );
+               }
+
+               $out .= "<ul class='mw-search-results'>\n";
                while ( $result ) {
                        $out .= $this->showHit( $result, $terms );
                        $result = $matches->next();
@@ -818,6 +855,25 @@
        }
 
        /**
+        * Extract custom captions from search-interwiki-custom message
+        * @return array
+        */
+       protected function getCustomCaptions() {
+               if( is_null($this->customCaptions) ) {
+                       $this->customCaptions = array();
+                       // format per line <iwprefix>:<caption>
+                       $customLines = explode( "\n", $this->msg( 
'search-interwiki-custom' )->text() );
+                       foreach ( $customLines as $line ) {
+                               $parts = explode( ":", $line, 2 );
+                               if ( count( $parts ) == 2 ) { // validate line
+                                       $this->customCaptions[$parts[0]] = 
$parts[1];
+                               }
+                       }
+               }
+               return $this->customCaptions;
+       }
+
+       /**
         * Show results from other wikis
         *
         * @param SearchResultSet|array $matches
@@ -833,15 +889,7 @@
                $out .= "<ul class='mw-search-iwresults'>\n";
 
                // work out custom project captions
-               $customCaptions = array();
-               // format per line <iwprefix>:<caption>
-               $customLines = explode( "\n", $this->msg( 
'search-interwiki-custom' )->text() );
-               foreach ( $customLines as $line ) {
-                       $parts = explode( ":", $line, 2 );
-                       if ( count( $parts ) == 2 ) { // validate line
-                               $customCaptions[$parts[0]] = $parts[1];
-                       }
-               }
+               $customCaptions = $this->getCustomCaptions();
 
                if ( !is_array( $matches ) ) {
                        $matches = array( $matches );
diff --git a/languages/i18n/en.json b/languages/i18n/en.json
index 3fc297c..0db3992 100644
--- a/languages/i18n/en.json
+++ b/languages/i18n/en.json
@@ -929,6 +929,7 @@
        "showingresultsinrange": "Showing below up to 
{{PLURAL:$1|<strong>1</strong> result|<strong>$1</strong> results}} in range 
#<strong>$2</strong> to #<strong>$3</strong>.",
        "search-showingresults": "{{PLURAL:$4|Result <strong>$1</strong> of 
<strong>$3</strong>|Results <strong>$1 - $2</strong> of <strong>$3</strong>}}",
        "search-nonefound": "There were no results matching the query.",
+       "search-nonefound-thiswiki": "There were no results matching the query 
in this wiki.",
        "powersearch-legend": "Advanced search",
        "powersearch-ns": "Search in namespaces:",
        "powersearch-togglelabel": "Check:",
diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json
index 79d8a69..40a7c86 100644
--- a/languages/i18n/qqq.json
+++ b/languages/i18n/qqq.json
@@ -1102,6 +1102,7 @@
        "showingresultsinrange": "Used in pagination of 
[[Special:MostLinkedCategories]]. Parameters:\n* $1 - the total number of 
results in the batch shown\n* $2 - the number of the first item listed\n* $3 - 
the number of last item in the batch shown\n\nSee also 
{{msg-mw|Showingresults}}",
        "search-showingresults": "Used in search results of [[Special:Search]]. 
Parameters:\n* $1 - minimum offset\n* $2 - maximum offset\n* $3 - total number 
of results\n* $4 - number of results",
        "search-nonefound": "Message shown when a search returned no results 
(when using the default MediaWiki search engine).",
+       "search-nonefound-thiswiki": "Message shown when a search in current 
wiki returned no results (when using the default MediaWiki search engine) but 
search in other wikis did return results.",
        "powersearch-legend": "Advanced search\n\n{{Identical|Advanced 
search}}",
        "powersearch-ns": "Used in the extended search form at 
[[Special:Search]]",
        "powersearch-togglelabel": "Used in 
[{{canonicalurl:Special:Search|advanced=1}} Advanced search]. Synonym: 
\"Select\" as verb.\n{{Identical|Check}}",

-- 
To view, visit https://gerrit.wikimedia.org/r/241129
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib6524bc79e1648ccf6adc5745f0dbc4c26dcb0d2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to