Matthias Mullie has uploaded a new change for review.

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

Change subject: Make search API output very similar to view-topiclist
......................................................................

Make search API output very similar to view-topiclist

Bug: T104582
Change-Id: Iae898b64e2da157549073741c1d89c61224c2f16
---
M autoload.php
M container.php
M includes/Api/ApiFlowSearch.php
D includes/Formatter/SearchQuery.php
M includes/Formatter/TopicListFormatter.php
5 files changed, 39 insertions(+), 99 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/18/223018/1

diff --git a/autoload.php b/autoload.php
index d330e1c..d8d410c 100644
--- a/autoload.php
+++ b/autoload.php
@@ -153,7 +153,6 @@
        'Flow\\Formatter\\RevisionUndoViewFormatter' => __DIR__ . 
'/includes/Formatter/RevisionUndoViewFormatter.php',
        'Flow\\Formatter\\RevisionViewFormatter' => __DIR__ . 
'/includes/Formatter/RevisionViewFormatter.php',
        'Flow\\Formatter\\RevisionViewQuery' => __DIR__ . 
'/includes/Formatter/RevisionViewQuery.php',
-       'Flow\\Formatter\\SearchQuery' => __DIR__ . 
'/includes/Formatter/SearchQuery.php',
        'Flow\\Formatter\\SinglePostQuery' => __DIR__ . 
'/includes/Formatter/SinglePostQuery.php',
        'Flow\\Formatter\\TocTopicListFormatter' => __DIR__ . 
'/includes/Formatter/TocTopicListFormatter.php',
        'Flow\\Formatter\\TopicFormatter' => __DIR__ . 
'/includes/Formatter/TopicFormatter.php',
diff --git a/container.php b/container.php
index ffb4d33..eb7f1b3 100644
--- a/container.php
+++ b/container.php
@@ -1005,12 +1005,6 @@
                $c['formatter.revision']
        );
 };
-$c['query.search'] = function( $c ) {
-       return new Flow\Formatter\SearchQuery(
-               $c['storage'],
-               $c['repository.tree']
-       );
-};
 $c['searchindex.updaters'] = function( $c ) {
        // permissions for anon user
        $anonPermissions = new Flow\RevisionActionPermissions( 
$c['flow_actions'], new User );
diff --git a/includes/Api/ApiFlowSearch.php b/includes/Api/ApiFlowSearch.php
index a8c569e..01a84d8 100644
--- a/includes/Api/ApiFlowSearch.php
+++ b/includes/Api/ApiFlowSearch.php
@@ -5,8 +5,9 @@
 use ApiBase;
 use Flow\Container;
 use Flow\Exception\InvalidDataException;
-use Flow\Formatter\RevisionFormatter;
-use Flow\Formatter\SearchQuery;
+use Flow\Formatter\TopicListFormatter;
+use Flow\Formatter\TopicListQuery;
+use Flow\Model\UUID;
 use Flow\Search\Connection;
 use Flow\Search\SearchEngine;
 use Flow\TalkpageManager;
@@ -15,18 +16,12 @@
 
 class ApiFlowSearch extends ApiFlowBaseGet {
        /**
-        * @var SearchQuery
-        */
-       protected $query;
-
-       /**
         * @var SearchEngine
         */
        protected $searchEngine;
 
        public function __construct( $api, $modName ) {
                parent::__construct( $api, $modName, 'q' );
-               $this->query = Container::get( 'query.search' );
                $this->searchEngine = new SearchEngine();
        }
 
@@ -65,22 +60,43 @@
                // result can be null, if nothing was found
                $results = $result === null ? array() : $result->getResults();
 
-               $rows = $this->query->getResults( $results );
-               $results = array(
+               $topicIds = array();
+               foreach ( $results as $topic ) {
+                       $topicIds[] = UUID::create( $topic->getId() );
+               }
+
+               // output similar to view-topiclist
+               $results = $this->formatApi( $topicIds );
+               // search-specific output
+               $results += array(
                        'total' => $result->getTotalHits(),
                        'rows' => array(),
                );
 
-               /** @var RevisionFormatter $formatter */
-               $formatter = Container::get( 'formatter.revision' );
-               foreach ( $rows as $row ) {
-                       $results['rows'][] = $formatter->formatApi( $row, 
$this->getContext() );
-               }
-
                $this->getResult()->addValue( null, $this->getModuleName(), 
$results );
        }
 
        /**
+        * Given an array of topic UUIDs, we'll use TopicListQuery & 
TopicListFormatter
+        * to return API output very similar to ApiFlowViewTopicList.
+        *
+        * @param array $topicIds
+        * @return array
+        */
+       protected function formatApi( array $topicIds ) {
+               /** @var TopicListQuery $query */
+               $query = Container::get( 'query.topiclist' );
+               $found = $query->getResults( $topicIds );
+
+               $storage = Container::get( 'storage' );
+               $workflows = $storage->getMulti( 'Workflow', $topicIds );
+
+               /** @var TopicListFormatter $serializer */
+               $serializer = Container::get( 'formatter.topiclist' );
+               return $serializer->buildResult( $workflows, $found, 
$this->getContext() );
+       }
+
+       /**
         * @param array $params
         * @return int[]
         */
diff --git a/includes/Formatter/SearchQuery.php 
b/includes/Formatter/SearchQuery.php
deleted file mode 100644
index 2748931..0000000
--- a/includes/Formatter/SearchQuery.php
+++ /dev/null
@@ -1,73 +0,0 @@
-<?php
-
-namespace Flow\Formatter;
-
-use Elastica\Result;
-use Flow\Collection\AbstractCollection;
-use Flow\Exception\FlowException;
-use Flow\Exception\InvalidDataException;
-use Flow\Model\UUID;
-use Flow\Search\Connection;
-use MWExceptionHandler;
-
-class SearchQuery extends AbstractQuery {
-       /**
-        * @param Result[] $results
-        * @return FormatterRow[]
-        */
-       public function getResults( array $results ) {
-               // loop all results & fetch matched collections
-               /** @var AbstractCollection[] $collections */
-               $collections = array();
-               foreach ( $results as $result ) {
-                       try {
-                               $id = UUID::create( $result->getId() );
-                               $collections[$id->getAlphadecimal()] = 
$this->getCollection( $id, $result->getType() );
-                       } catch ( FlowException $e ) {
-                               wfWarn( __METHOD__ . ': ' . $e->getMessage() );
-                               MWExceptionHandler::logException( $e );
-                       }
-               }
-
-               // load all data for found results
-               // @todo: do more efficient batchloading instead of piecemeal 
Collection objects
-               $results = array();
-               foreach ( $collections as $id => $collection ) {
-                       try {
-                               $revision = $collection->getLastRevision();
-                       } catch ( \Exception $e ) {
-                               wfWarn( __METHOD__ . ': Couldn\'t find last 
revision for ' . $id . ': ' . $e->getMessage() );
-                               MWExceptionHandler::logException( $e );
-                               continue;
-                       }
-
-                       $results[] = $this->buildResult( $revision, '' /* 
@todo: $indexField */ );
-               }
-
-               return $results;
-       }
-
-       /**
-        * Returns the collection object for the given id, based on the 
collection
-        * type, which can be determined based on the index type.
-        *
-        * @param UUID $id
-        * @param string $type
-        * @return AbstractCollection
-        * @throws InvalidDataException
-        */
-       protected function getCollection( UUID $id, $type ) {
-               // @todo: there's got to be more elegant ways to do this
-
-               $map = array(
-                       Connection::TOPIC_TYPE_NAME => 
'Flow\\Collection\\PostCollection',
-                       Connection::HEADER_TYPE_NAME => 
'Flow\\Collection\\HeaderCollection',
-               );
-
-               if ( !isset( $map[$type] ) ) {
-                       throw new InvalidDataException( "Unknown index type: 
$type", 'fail-load-data' );
-               }
-
-               return $map[$type]::newFromId( $id );
-       }
-}
diff --git a/includes/Formatter/TopicListFormatter.php 
b/includes/Formatter/TopicListFormatter.php
index dd2d761..7f8ed1e 100644
--- a/includes/Formatter/TopicListFormatter.php
+++ b/includes/Formatter/TopicListFormatter.php
@@ -3,6 +3,7 @@
 namespace Flow\Formatter;
 
 use Flow\Data\Pager\PagerPage;
+use Flow\Model\UUID;
 use Flow\Model\Workflow;
 use Flow\UrlGenerator;
 use IContextSource;
@@ -30,6 +31,7 @@
        public function buildEmptyResult( Workflow $workflow ) {
                $title = $workflow->getArticleTitle();
                return array(
+                       'workflowId' => $workflow->getId()->getAlphadecimal(),
                        'title' => $title->getPrefixedText(),
                        'actions' => $this->buildApiActions( $workflow ),
                ) + parent::buildEmptyResult( $workflow );
@@ -42,7 +44,7 @@
                PagerPage $page,
                IContextSource $ctx
        ) {
-               $res = $this->buildResult( $listWorkflow, $workflows, $found, 
$ctx ) +
+               $res = $this->buildResult( $workflows, $found, $ctx ) +
                        $this->buildEmptyResult( $listWorkflow );
                $pagingOption = $page->getPagingLinksOptions();
                $res['links']['pagination'] = $this->buildPaginationLinks(
@@ -61,13 +63,16 @@
        }
 
        /**
+        * Method is called from static::formatApi & ApiFlowSearch::formatApi, 
to ensure
+        * both have similar output.
+        *
         * @param Workflow $listWorkflow
         * @param Workflow[] $workflows
         * @param FormatterRow[] $found
         * @param IContextSource $ctx
         * @return array
         */
-       protected function buildResult( Workflow $listWorkflow, array 
$workflows, array $found, IContextSource $ctx ) {
+       public function buildResult( array $workflows, array $found, 
IContextSource $ctx ) {
                $revisions = $posts = $replies = array();
                foreach( $found as $formatterRow ) {
                        $serialized = $this->serializer->formatApi( 
$formatterRow, $ctx );
@@ -108,7 +113,6 @@
                }
 
                return array(
-                       'workflowId' => 
$listWorkflow->getId()->getAlphadecimal(),
                        // array_values must be used to ensure 0-indexed array
                        'roots' => $list,
                        'posts' => $posts,

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iae898b64e2da157549073741c1d89c61224c2f16
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Matthias Mullie <mmul...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to