Smalyshev has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/344738 )

Change subject: Move request switches handling into Searcher
......................................................................

Move request switches handling into Searcher

This allows other Searcher clients besides main CirrusSearch class
to use the same facilities and same variables in the centralized manner.

This will allow developing alternative search modes easier as they automatically
can access the debugging/explain facilities as soon as thery use Searcher.

Change-Id: Idf5ea876da299c7aaac3b3e6cdce6424c75c81bf
---
M includes/CirrusSearch.php
M includes/Search/SearchContext.php
M includes/Searcher.php
3 files changed, 68 insertions(+), 51 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CirrusSearch 
refs/changes/38/344738/1

diff --git a/includes/CirrusSearch.php b/includes/CirrusSearch.php
index 200f323..915cdc8 100644
--- a/includes/CirrusSearch.php
+++ b/includes/CirrusSearch.php
@@ -349,12 +349,7 @@
                        }
                }
 
-               $dumpQuery = $this->request && $this->request->getVal( 
'cirrusDumpQuery' ) !== null;
-               $searcher->setReturnQuery( $dumpQuery );
-               $dumpResult = $this->request && $this->request->getVal( 
'cirrusDumpResult' ) !== null;
-               $searcher->setDumpResult( $dumpResult );
-               $returnExplain = $this->request && $this->request->getVal( 
'cirrusExplain' ) !== null;
-               $searcher->setReturnExplain( $returnExplain );
+               $searcher->setOptionsFromRequest( $this->request );
 
                if ( $this->lastNamespacePrefix ) {
                        $searcher->addSuggestPrefix( $this->lastNamespacePrefix 
);
@@ -407,9 +402,7 @@
                ) {
 
                        $iwSearch = new InterwikiSearcher( $this->connection, 
$config, $this->namespaces, null, $highlightingConfig );
-                       $iwSearch->setReturnQuery( $dumpQuery );
-                       $iwSearch->setDumpResult( $dumpResult );
-                       $iwSearch->setReturnExplain( $returnExplain );
+                       $iwSearch->setOptionsFromRequest( $this->request );
                        $interwikiResults = $iwSearch->getInterwikiResults( 
$term );
 
                        if ( $interwikiResults !== null ) {
@@ -443,32 +436,8 @@
                }
 
                if ( $searcher->isReturnRaw() ) {
-                       $header = null;
-                       if ( $this->request && $this->request->getVal( 
'cirrusExplain' ) === 'pretty' ) {
-                               $header = 'Content-type: text/html; 
charset=UTF-8';
-                               $printer = new CirrusSearch\ExplainPrinter();
-                               $result = $printer->format( $result );
-                       } else {
-                               $header = 'Content-type: application/json; 
charset=UTF-8';
-                               if ( $result === null ) {
-                                       $result = '{}';
-                               } else {
-                                       $result = json_encode( $result, 
JSON_PRETTY_PRINT );
-                               }
-                       }
-
-                       if ( $this->dumpAndDie ) {
-                               // When dumping the query we skip _everything_ 
but echoing the query.
-                               
RequestContext::getMain()->getOutput()->disable();
-                               if ( $header !== null ) {
-                                       $this->request->response()->header( 
$header );
-                               }
-                               echo $result;
-                               exit();
-                       } else {
-                               // This breaks the return types and is only 
used in the fixtures unit tests
-                               $status->setResult( true, $result );
-                       }
+                       $status->setResult( true,
+                               $searcher->processRawReturn( $result, 
$this->request, $this->dumpAndDie ) );
                }
 
                return $status;
diff --git a/includes/Search/SearchContext.php 
b/includes/Search/SearchContext.php
index 495cde8..953f5bd 100644
--- a/includes/Search/SearchContext.php
+++ b/includes/Search/SearchContext.php
@@ -3,7 +3,6 @@
 namespace CirrusSearch\Search;
 
 use CirrusSearch\SearchConfig;
-use GeoData\Coord;
 use Elastica\Query\AbstractQuery;
 
 /**
@@ -747,4 +746,5 @@
        public function getWarnings() {
                return $this->warnings;
        }
+
 }
diff --git a/includes/Searcher.php b/includes/Searcher.php
index 231f128..ab1dcce 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -14,11 +14,13 @@
 use MediaWiki\Logger\LoggerFactory;
 use MediaWiki\MediaWikiServices;
 use ObjectCache;
+use RequestContext;
 use SearchResultSet;
 use Status;
 use ApiUsageException;
 use UsageException;
 use User;
+use WebRequest;
 
 
 /**
@@ -96,21 +98,6 @@
        protected $indexBaseName;
 
        /**
-        * @var boolean just return the array that makes up the query instead 
of searching
-        */
-       private $returnQuery = false;
-
-       /**
-        * @var boolean return raw Elasticsearch result instead of processing it
-        */
-       private $returnResult = false;
-
-       /**
-        * @var boolean return explanation with results
-        */
-       private $returnExplain = false;
-
-       /**
         * Search environment configuration
         * @var SearchConfig
         */
@@ -121,6 +108,20 @@
         */
        protected $searchContext;
 
+       /**
+        * @var boolean just return the array that makes up the query instead 
of searching
+        */
+       protected $returnQuery = false;
+
+       /**
+        * @var boolean return raw Elasticsearch result instead of processing it
+        */
+       protected $returnResult = false;
+
+       /**
+        * @var boolean return explanation with results
+        */
+       protected $returnExplain = false;
        /**
         * Constructor
         * @param Connection $conn
@@ -1020,4 +1021,51 @@
                        $extra
                );
        }
+
+       /**
+        * Set search options from request params
+        * @param WebRequest $request
+        */
+       public function setOptionsFromRequest( WebRequest $request ) {
+               if ( !$request ) {
+                       return;
+               }
+               $this->returnQuery = $request->getVal( 'cirrusDumpQuery' ) !== 
null;
+               $this->returnResult = $request->getVal( 'cirrusDumpResult' ) 
!== null;
+               $this->returnExplain = $request->getVal( 'cirrusExplain' );
+       }
+
+       /**
+        * If we're supposed to create raw result, create and return it,
+        * or output it and finish.
+        * @param array $result
+        * @param WebRequest $request
+        * @param bool $dumpAndDie
+        * @return string The new raw result.
+        */
+       public function processRawReturn( array $result, WebRequest $request, 
$dumpAndDie = true ) {
+               $header = null;
+               if ( $this->returnExplain === 'pretty' ) {
+                       $header = 'Content-type: text/html; charset=UTF-8';
+                       $printer = new ExplainPrinter();
+                       $result = $printer->format( $result );
+               } else {
+                       $header = 'Content-type: application/json; 
charset=UTF-8';
+                       if ( $result === null ) {
+                               $result = '{}';
+                       } else {
+                               $result = json_encode( $result, 
JSON_PRETTY_PRINT );
+                       }
+               }
+
+               if ( $dumpAndDie ) {
+                       // When dumping the query we skip _everything_ but 
echoing the query.
+                       RequestContext::getMain()->getOutput()->disable();
+                       $request->response()->header( $header );
+                       echo $result;
+                       exit();
+               }
+
+               return $result;
+       }
 }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idf5ea876da299c7aaac3b3e6cdce6424c75c81bf
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CirrusSearch
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