Happy5214 has uploaded a new change for review.

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

Change subject: Expose post and topic history through the API
......................................................................

Expose post and topic history through the API

This commit adds two new API submodules for accessing post and topic
histories. The calls return the relevant revisions. A modified clone
of the existing method rendering histories was used for this change.

Bug: T103032
Bug: T103034
Change-Id: Ib9b6a0fe53b6317d4bedfce2ebec0df709f96a86
---
M includes/Api/ApiFlow.php
A includes/Api/ApiFlowViewPostHistory.php
A includes/Api/ApiFlowViewTopicHistory.php
M includes/Block/Topic.php
4 files changed, 199 insertions(+), 4 deletions(-)


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

diff --git a/includes/Api/ApiFlow.php b/includes/Api/ApiFlow.php
index 7065351..fadb336 100644
--- a/includes/Api/ApiFlow.php
+++ b/includes/Api/ApiFlow.php
@@ -14,7 +14,7 @@
        /** @var ApiModuleManager $moduleManager */
        private $moduleManager;
 
-       private static $modules = array(
+       private static $alwaysEnabledModules = array(
                // POST
                'new-topic' => 'Flow\Api\ApiFlowNewTopic',
                'edit-header' => 'Flow\Api\ApiFlowEditHeader',
@@ -35,16 +35,29 @@
                // topiclist - we'll want to know topic-/post- or 
topiclist-view ;)
                'view-topiclist' => 'Flow\Api\ApiFlowViewTopicList',
                'view-post' => 'Flow\Api\ApiFlowViewPost',
+               'view-post-history' => 'Flow\Api\ApiFlowViewPostHistory',
                'view-topic' => 'Flow\Api\ApiFlowViewTopic',
+               'view-topic-history' => 'Flow\Api\ApiFlowViewTopicHistory',
                'view-header' => 'Flow\Api\ApiFlowViewHeader',
                'view-topic-summary' => 'Flow\Api\ApiFlowViewTopicSummary',
-//             'search' => 'Flow\Api\ApiFlowSearch', // @todo: uncomment once 
we're ready to expose search
+       );
+
+       private static $searchModules = array(
+               'search' => 'Flow\Api\ApiFlowSearch',
        );
 
        public function __construct( $main, $action ) {
+               global $wgFlowSearchEnabled;
+
                parent::__construct( $main, $action );
                $this->moduleManager = new ApiModuleManager( $this );
-               $this->moduleManager->addModules( self::$modules, 'submodule' );
+
+               $enabledModules = self::$alwaysEnabledModules;
+               if ( $wgFlowSearchEnabled ) {
+                       $enabledModules += self::$searchModules;
+               }
+
+               $this->moduleManager->addModules( $enabledModules, 'submodule' 
);
        }
 
        public function getModuleManager() {
diff --git a/includes/Api/ApiFlowViewPostHistory.php 
b/includes/Api/ApiFlowViewPostHistory.php
new file mode 100644
index 0000000..632d957
--- /dev/null
+++ b/includes/Api/ApiFlowViewPostHistory.php
@@ -0,0 +1,74 @@
+<?php
+
+namespace Flow\Api;
+
+use ApiBase;
+
+class ApiFlowViewPostHistory extends ApiFlowBaseGet {
+       public function __construct( $api, $modName ) {
+               parent::__construct( $api, $modName, 'vph' );
+       }
+
+       /**
+        * Taken from ext.flow.base.js
+        *
+        * @return array
+        */
+       protected function getBlockParams() {
+               return array( 'topic' => $this->extractRequestParams() );
+       }
+
+       protected function getAction() {
+               return 'view-post-history';
+       }
+
+       public function getAllowedParams() {
+               global $wgFlowContentFormat;
+
+               return array(
+                       'postId' => array(
+                               ApiBase::PARAM_REQUIRED => true,
+                       ),
+                       'format' => array(
+                               ApiBase::PARAM_TYPE => array( 'html', 
'wikitext', 'fixed-html' ),
+                               ApiBase::PARAM_DFLT => $wgFlowContentFormat,
+                       ),
+               );
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getParamDescription() {
+               return array(
+                       'postId' => 'Id of the post for which to view revision 
history',
+                       'format' => 'Format to return the content in',
+               );
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getDescription() {
+               return 'View the revision history of a post';
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getExamples() {
+               return array(
+                       
'api.php?action=flow&submodule=view-post-history&page=Topic:S2tycnas4hcucw8w&vphpostId=???&vphformat=wikitext',
+               );
+       }
+
+       /**
+        * @see ApiBase::getExamplesMessages()
+        */
+       protected function getExamplesMessages() {
+               return array(
+                       
'action=flow&submodule=view-post-history&page=Topic:S2tycnas4hcucw8w&vphpostId=???&vphformat=wikitext'
+                               => 'apihelp-flow+view-post-history-example-1',
+               );
+       }
+}
diff --git a/includes/Api/ApiFlowViewTopicHistory.php 
b/includes/Api/ApiFlowViewTopicHistory.php
new file mode 100644
index 0000000..63d9ffe
--- /dev/null
+++ b/includes/Api/ApiFlowViewTopicHistory.php
@@ -0,0 +1,70 @@
+<?php
+
+namespace Flow\Api;
+
+use ApiBase;
+
+class ApiFlowViewTopicHistory extends ApiFlowBaseGet {
+       public function __construct( $api, $modName ) {
+               parent::__construct( $api, $modName, 'vth' );
+       }
+
+       /**
+        * Taken from ext.flow.base.js
+        *
+        * @return array
+        */
+       protected function getBlockParams() {
+               return array( 'topic' => $this->extractRequestParams() );
+       }
+
+       protected function getAction() {
+               return 'view-topic-history';
+       }
+
+       public function getAllowedParams() {
+               global $wgFlowContentFormat;
+
+               return array(
+                       'format' => array(
+                               ApiBase::PARAM_TYPE => array( 'html', 
'wikitext', 'fixed-html' ),
+                               ApiBase::PARAM_DFLT => $wgFlowContentFormat,
+                       ),
+               );
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getParamDescription() {
+               return array(
+                       'format' => 'Format to return the content in',
+               );
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getDescription() {
+               return 'View the revision history of a topic';
+       }
+
+       /**
+        * @deprecated since MediaWiki core 1.25
+        */
+       public function getExamples() {
+               return array(
+                       
'api.php?action=flow&submodule=view-topic-history&page=Topic:S2tycnas4hcucw8w&vthformat=wikitext',
+               );
+       }
+
+       /**
+        * @see ApiBase::getExamplesMessages()
+        */
+       protected function getExamplesMessages() {
+               return array(
+                       
'action=flow&submodule=view-topic-history&page=Topic:S2tycnas4hcucw8w&vthformat=wikitext'
+                               => 'apihelp-flow+view-topic-history-example-1',
+               );
+       }
+}
diff --git a/includes/Block/Topic.php b/includes/Block/Topic.php
index 1b02d8c..1255508 100644
--- a/includes/Block/Topic.php
+++ b/includes/Block/Topic.php
@@ -70,7 +70,7 @@
 
        protected $supportedGetActions = array(
                'reply', 'view', 'history', 'edit-post', 'edit-title', 
'compare-post-revisions', 'single-view',
-               'view-topic', 'view-post', 'undo-edit-post',
+               'view-topic', 'view-topic-history', 'view-post', 
'view-post-history', 'undo-edit-post',
                'moderate-topic', 'moderate-post', 'lock-topic',
        );
 
@@ -513,6 +513,16 @@
                                $output += $this->renderUndoApi( $options );
                                break;
 
+                       case 'view-post-history':
+                               // View entire history of single post
+                               $output += $this->renderHistoryApi( 
Container::get( 'query.post.history' ), UUID::create( $options['postId'] ), 
$options );
+                               break;
+
+                       case 'view-topic-history':
+                               // View entire histories of a topic's posts
+                               $output += $this->renderHistoryApi( 
Container::get( 'query.topic.history' ), $this->workflow->getId(), $options );
+                               break;
+
                        // Any actions require (re)rendering the whole topic
                        case 'edit-post':
                        case 'moderate-post':
@@ -747,6 +757,34 @@
        }
 
        /**
+        * Process the history result for either topic or post, in a format 
more suitable for API consumers
+        *
+        * @param TopicHistoryQuery|PostHistoryQuery $query
+        * @param UUID $uuid
+        * @param array $options
+        * @return array
+        */
+       protected function renderHistoryApi( /* 
TopicHistoryQuery|PostHistoryQuery */ $query, UUID $uuid, $options ) {
+               $format = isset( $options['format'] ) ? $options['format'] : 
'fixed-html';
+               $serializer = $this->getRevisionFormatter( $format );
+               $serializer->setIncludeHistoryProperties( true );
+
+               $history = $query->getResults( $uuid );
+               $revisions = array();
+               foreach ( $history as $row ) {
+                       $serialized = $serializer->formatApi( $row, 
$this->context, 'history' );
+                       // if the user is not allowed to see this row it will 
return empty
+                       if ( $serialized ) {
+                               $revisions[$serialized['revisionId']] = 
$serialized;
+                       }
+               }
+
+               return array(
+                       'revisions' => $revisions,
+               );
+       }
+
+       /**
         * @return PostRevision|null
         */
        public function loadRootPost() {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib9b6a0fe53b6317d4bedfce2ebec0df709f96a86
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: Happy5214 <happy5...@gmail.com>

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

Reply via email to