Mattflaschen has uploaded a new change for review.

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

Change subject: WIP: J2f - Allow fetching topic list without replies
......................................................................

WIP: J2f - Allow fetching topic list without replies

This will improve performance for the TOC use case.

Change-Id: I3615817edb1500bd63dc4cbd64820fa25c943e21
---
M autoload.php
M container.php
M includes/Block/TopicList.php
M includes/Formatter/TopicListFormatter.php
M includes/api/ApiFlowViewTopicList.php
5 files changed, 58 insertions(+), 20 deletions(-)


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

diff --git a/autoload.php b/autoload.php
index 891d85d..18da62c 100644
--- a/autoload.php
+++ b/autoload.php
@@ -159,6 +159,7 @@
 $wgAutoloadClasses['Flow\\Formatter\\TopicHistoryQuery'] = __DIR__ . 
'/includes/Formatter/TopicHistoryQuery.php';
 $wgAutoloadClasses['Flow\\Formatter\\TopicListFormatter'] = __DIR__ . 
'/includes/Formatter/TopicListFormatter.php';
 $wgAutoloadClasses['Flow\\Formatter\\TopicListQuery'] = __DIR__ . 
'/includes/Formatter/TopicListQuery.php';
+$wgAutoloadClasses['Flow\\Formatter\\TopicListQueryWithoutReplies'] = __DIR__ 
. '/includes/Formatter/TopicListQueryWithoutReplies.php';
 $wgAutoloadClasses['Flow\\Formatter\\TopicRow'] = __DIR__ . 
'/includes/Formatter/TopicRow.php';
 $wgAutoloadClasses['Flow\\LinksTableUpdater'] = __DIR__ . 
'/includes/LinksTableUpdater.php';
 $wgAutoloadClasses['Flow\\Log\\Formatter'] = __DIR__ . 
'/includes/Log/Formatter.php';
diff --git a/container.php b/container.php
index ed47695..2ef07bf 100644
--- a/container.php
+++ b/container.php
@@ -696,6 +696,14 @@
                $c['watched_items']
        );
 } );
+$c['query.topiclistwithoutreplies'] = $c->share( function( $c ) {
+       return new Flow\Formatter\TopicListQueryWithoutReplies(
+               $c['storage'],
+               $c['repository.tree'],
+               $c['permissions'],
+               $c['watched_items']
+       );
+} );
 $c['query.topic.history'] = $c->share( function( $c ) {
        return new Flow\Formatter\TopicHistoryQuery(
                $c['storage'],
diff --git a/includes/Block/TopicList.php b/includes/Block/TopicList.php
index f5bd2fc..1c3251e 100644
--- a/includes/Block/TopicList.php
+++ b/includes/Block/TopicList.php
@@ -184,14 +184,24 @@
                }
 
                $workflows = $this->storage->getMulti( 'Workflow', $workflowIds 
);
+
+               $includeReplies = !$options['omitreplies']; // See 
ApiFlowViewTopicList
+
                /** @var TopicListQuery $query */
-               $query = Container::get( 'query.topiclist' );
+               $query = null;
+
+               if ( $includeReplies ) {
+                       $query = Container::get( 'query.topiclist' );
+               } else {
+                       $query = Container::get( 
'query.topiclistwithoutreplies' );
+               }
+
                $found = $query->getResults( $page->getResults() );
                wfDebugLog( 'FlowDebug', 'Rendering topiclist for ids: ' . 
implode( ', ', array_map( function( UUID $id ) {
                        return $id->getAlphadecimal();
                }, $workflowIds ) ) );
 
-               return $response + $serializer->formatApi( $this->workflow, 
$workflows, $found, $page, $this->context );
+               return $response + $serializer->formatApi( $this->workflow, 
$workflows, $found, $page, $this->context, $includeReplies );
        }
 
        public function getName() {
diff --git a/includes/Formatter/TopicListFormatter.php 
b/includes/Formatter/TopicListFormatter.php
index 169c470..a2442b0 100644
--- a/includes/Formatter/TopicListFormatter.php
+++ b/includes/Formatter/TopicListFormatter.php
@@ -48,11 +48,12 @@
                array $workflows,
                array $found,
                PagerPage $page,
-               IContextSource $ctx
+               IContextSource $ctx,
+               $includeReplies
        ) {
                /** @noinspection PhpUnusedLocalVariableInspection */
                $section = new \ProfileSection( __METHOD__ );
-               $res = $this->buildResult( $listWorkflow, $workflows, $found, 
$ctx ) +
+               $res = $this->buildResult( $listWorkflow, $workflows, $found, 
$ctx, $includeReplies ) +
                        $this->buildEmptyResult( $listWorkflow );
                $pagingOption = $page->getPagingLinksOptions();
                $res['links']['pagination'] = $this->buildPaginationLinks(
@@ -94,9 +95,10 @@
         * @param Workflow[] $workflows
         * @param FormatterRow[] $found
         * @param IContextSource $ctx
+        * @param boolean $includeReplies True to include replies, false to 
limit to the topic entity itself
         * @return array
         */
-       protected function buildResult( Workflow $listWorkflow, array 
$workflows, array $found, IContextSource $ctx ) {
+       protected function buildResult( Workflow $listWorkflow, array 
$workflows, array $found, IContextSource $ctx, $includeReplies ) {
                $revisions = $posts = $replies = array();
                foreach( $found as $formatterRow ) {
                        $serialized = $this->serializer->formatApi( 
$formatterRow, $ctx );
@@ -108,9 +110,11 @@
                        $replies[$serialized['replyToId']][] = 
$serialized['postId'];
                }
 
-               foreach ( $revisions as $i => $serialized ) {
-                       $alpha = $serialized['postId'];
-                       $revisions[$i]['replies'] = isset( $replies[$alpha] ) ? 
$replies[$alpha] : array();
+               if ( $includeReplies ) {
+                       foreach ( $revisions as $i => $serialized ) {
+                               $alpha = $serialized['postId'];
+                               $revisions[$i]['replies'] = isset( 
$replies[$alpha] ) ? $replies[$alpha] : array();
+                       }
                }
 
                $list = array();
@@ -129,7 +133,7 @@
 
                        foreach ( $list as $alpha ) {
                                // Metadata that requires everything to be 
serialied first
-                               $metadata = $this->generateTopicMetadata( 
$posts, $revisions, $workflows, $alpha, $ctx );
+                               $metadata = $this->generateTopicMetadata( 
$posts, $revisions, $workflows, $alpha, $ctx, $includeReplies );
                                foreach ( $posts[$alpha] as $revId ) {
                                        $revisions[$revId] += $metadata;
                                }
@@ -163,7 +167,7 @@
                );
        }
 
-       protected function generateTopicMetadata( array $posts, array 
$revisions, array $workflows, $postAlphaId, IContextSource $ctx ) {
+       protected function generateTopicMetadata( array $posts, array 
$revisions, array $workflows, $postAlphaId, IContextSource $ctx, 
$includeReplies ) {
                $language = $ctx->getLanguage();
                $user = $ctx->getUser();
 
@@ -171,23 +175,31 @@
                $authors = array();
                $stack = new \SplStack;
                $stack->push( $revisions[$posts[$postAlphaId][0]] );
-               do {
-                       $data = $stack->pop();
-                       $replies++;
-                       $authors[] = $data['creator']['name'];
-                       foreach ( $data['replies'] as $postId ) {
-                               $stack->push( $revisions[$posts[$postId][0]] );
-                       }
-               } while( !$stack->isEmpty() );
+               if ( $includeReplies ) {
+                       do {
+                               $data = $stack->pop();
+                               $replies++;
+                               $authors[] = $data['creator']['name'];
+                               foreach ( $data['replies'] as $postId ) {
+                                       $stack->push( 
$revisions[$posts[$postId][0]] );
+                               }
+                       } while( !$stack->isEmpty() );
+               }
 
                /** @var Workflow|null $workflow */
                $workflow = isset( $workflows[$postAlphaId] ) ? 
$workflows[$postAlphaId] : null;
                $ts = $workflow ? 
$workflow->getLastModifiedObj()->getTimestamp() : 0;
-               return array(
-                       'reply_count' => $replies,
+
+               $metadata = array(
                        'last_updated_readable' => $language->userTimeAndDate( 
$ts, $user ),
                        // ms timestamp
                        'last_updated' => $ts * 1000,
                );
+
+               if ( $includeReplies ) {
+                       $metadata['reply_count'] = $replies;
+               }
+
+               return $metadata;
        }
 }
diff --git a/includes/api/ApiFlowViewTopicList.php 
b/includes/api/ApiFlowViewTopicList.php
index 06e9cd8..e839664 100644
--- a/includes/api/ApiFlowViewTopicList.php
+++ b/includes/api/ApiFlowViewTopicList.php
@@ -56,6 +56,12 @@
                                ApiBase::PARAM_TYPE => 'boolean',
                                ApiBase::PARAM_DFLT => false,
                        ),
+                       // I don't like double-negatives, but boolean 
parameters can't
+                       // default to true for some reason.
+                       'omitreplies' => array(
+                               ApiBase::PARAM_TYPE => 'boolean',
+                               ApiBase::PARAM_DFLT => false,
+                       ),
                );
        }
 
@@ -68,6 +74,7 @@
                        'offset' => 'Offset value to start fetching topics at',
                        'limit' => 'Amount of topics to fetch',
                        'render' => 'Renders (in HTML) the topics, if set',
+                       'omitreplies' => 'Whether to omit replies (posts); if 
true, only each topic itself will be included',
                );
        }
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3615817edb1500bd63dc4cbd64820fa25c943e21
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>

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

Reply via email to