EBernhardson has uploaded a new change for review.

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

Change subject: Handle flow entries in ApiFeedContributions
......................................................................

Handle flow entries in ApiFeedContributions

There is a related patch to core, I27c77cc6, which adds this hook.
This is fairly straightforward, reusing our existing formatting and
putting it into the format the FeedItem object expects. This api
can be queried via:

    action=feedcontributions&user=Admin&feedformat=atom

Bug: T85299
Change-Id: I3fdb89da8c9b9e164f25d756d9c5f76719d84ae2
---
M Flow.php
M Hooks.php
M autoload.php
M container.php
A includes/Formatter/FeedItemFormatter.php
5 files changed, 117 insertions(+), 0 deletions(-)


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

diff --git a/Flow.php b/Flow.php
index 8c25410..d08d969 100644
--- a/Flow.php
+++ b/Flow.php
@@ -70,6 +70,7 @@
 $wgHooks['ResourceLoaderGetConfigVars'][] = 
'FlowHooks::onResourceLoaderGetConfigVars';
 $wgHooks['ContribsPager::reallyDoQuery'][] = 'FlowHooks::onContributionsQuery';
 $wgHooks['ContributionsLineEnding'][] = 'FlowHooks::onContributionsLineEnding';
+$wgHooks['ApiFeedContributions::feedItem'][] = 
'FlowHooks::onContributionsFeedItem';
 $wgHooks['AbuseFilter-computeVariable'][] = 
'FlowHooks::onAbuseFilterComputeVariable';
 $wgHooks['AbortEmailNotification'][] = 'FlowHooks::onAbortEmailNotification';
 $wgHooks['InfoAction'][] = 'FlowHooks::onInfoAction';
diff --git a/Hooks.php b/Hooks.php
index a608d50..2ef6340 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -587,6 +587,40 @@
        }
 
        /**
+        * Convert flow contributions entries into FeedItem instances
+        * for ApiFeedContributions
+        *
+        * @param object $row Single row of data from ContribsPager
+        * @param FeedItem &$feedItem Return value holder for created feed item.
+        * @return bool
+        */
+       public static function onContributionsFeedItem( $row, IContextSource 
$ctx, FeedItem &$feedItem = null ) {
+               if ( !$row instanceof Flow\Formatter\FormatterRow ) {
+                       return true;
+               }
+
+               set_error_handler( new Flow\RecoverableErrorHandler, -1 );
+               try {
+                       /** @var Flow\Formatter\Contributions $formatter */
+                       $formatter = Container::get( 
'formatter.contributions.feeditem' );
+                       $result = $formatter->format( $row, $ctx, 'feeditem' );
+               } catch ( Exception $e ) {
+                       MWExceptionHandler::logException( $e );
+                       $result = false;
+               }
+               restore_error_handler();
+
+               if ( $result instanceof FeedItem ) {
+                       $feedItem = $result;
+                       return true;
+               } else {
+                       // If we failed to render a flow row, cancel it. This 
could be
+                       // either permissions or bugs.
+                       return false;
+               }
+       }
+
+       /**
         * Adds Flow contributions to the Contributions special page
         *
         * @param $data array an array of results of all contribs queries, to be
diff --git a/autoload.php b/autoload.php
index 57a96e6..13b10d7 100644
--- a/autoload.php
+++ b/autoload.php
@@ -143,6 +143,7 @@
        'Flow\\Formatter\\Contributions' => __DIR__ . 
'/includes/Formatter/Contributions.php',
        'Flow\\Formatter\\ContributionsQuery' => __DIR__ . 
'/includes/Formatter/ContributionsQuery.php',
        'Flow\\Formatter\\ContributionsRow' => __DIR__ . 
'/includes/Formatter/ContributionsQuery.php',
+       'Flow\\Formatter\\FeedItemFormatter' => __DIR__ . 
'/includes/Formatter/FeedItemFormatter.php',
        'Flow\\Formatter\\FormatterRow' => __DIR__ . 
'/includes/Formatter/AbstractQuery.php',
        'Flow\\Formatter\\HeaderViewQuery' => __DIR__ . 
'/includes/Formatter/RevisionViewQuery.php',
        'Flow\\Formatter\\IRCLineUrlFormatter' => __DIR__ . 
'/includes/Formatter/IRCLineUrlFormatter.php',
diff --git a/container.php b/container.php
index abb0e1a..7df1b50 100644
--- a/container.php
+++ b/container.php
@@ -918,6 +918,12 @@
                $c['formatter.revision']
        );
 } );
+$c['formatter.contributions.feeditem'] = $c->share( function( $c ) {
+       return new Flow\Formatter\FeedItemFormatter(
+               $c['permissions'],
+               $c['formatter.revision']
+       );
+} );
 $c['query.board-history'] = $c->share( function( $c ) {
        return new Flow\Formatter\BoardHistoryQuery(
                $c['storage'],
diff --git a/includes/Formatter/FeedItemFormatter.php 
b/includes/Formatter/FeedItemFormatter.php
new file mode 100644
index 0000000..bae0a29
--- /dev/null
+++ b/includes/Formatter/FeedItemFormatter.php
@@ -0,0 +1,75 @@
+<?php
+
+namespace Flow\Formatter;
+
+use FeedItem;
+use Flow\Exception\FlowException;
+use Flow\Model\PostRevision;
+use IContextSource;
+
+class FeedItemFormatter extends AbstractFormatter {
+       protected function getHistoryType() {
+               return 'feeditem';
+       }
+
+       /**
+        * @param FormatterRow $row With properties workflow, revision, 
previous_revision
+        * @param IContextSource $ctx
+        * @return FeedItem|false The requested format, or false on failure
+        */
+       public function format( FormatterRow $row, IContextSource $ctx ) {
+               try {
+                       if ( !$this->permissions->isAllowed( $row->revision, 
'history' ) ) {
+                               return false;
+                       }
+                       if ( $row->revision instanceof PostRevision &&
+                               !$this->permissions->isAllowed( $row->rootPost, 
'history' ) ) {
+                               return false;
+                       }
+
+                       return $this->createFeedItem( $row, $ctx );
+               } catch ( FlowException $e ) {
+                       \MWExceptionHandler::logException( $e );
+                       return false;
+               }
+       }
+
+       protected function createFeedItem( FormatterRow $row, IContextSource 
$ctx ) {
+               $this->serializer->setIncludeHistoryProperties( true );
+               $data = $this->serializer->formatApi( $row, $ctx );
+               if ( !$data ) {
+                       throw new FlowException( 'Could not format data for row 
' . $row->revision->getRevisionId()->getAlphadecimal() );
+               }
+
+               $preferredLinks = array(
+                       'header-revision',
+                       'post-revision', 'post',
+                       'topic-revision', 'topic',
+                       'board'
+               );
+               $url = '';
+               foreach ( $preferredLinks as $link ) {
+                       if ( isset( $data['links'][$link] ) ) {
+                               $url = $data['links'][$link]->getFullURL();
+                               break;
+                       }
+               }
+               // If we didn't choose anything just take the first.
+               // @todo perhaps just a convention that the first link
+               // is always the most specific, we use a similar pattern
+               // to above in TemplateHelper::historyTimestamp too.
+               if ( $url === '' && $data['links'] ) {
+                       $link = reset( array_keys( $data['links'] ) );
+                       $url = $data['links'][$link]->getFullURL();
+               }
+
+               return new FeedItem(
+                       $row->workflow->getArticleTitle()->getPrefixedText(),
+                       $this->formatDescription( $data, $ctx ),
+                       $url,
+                       $data['timestamp'],
+                       $data['author']['name'],
+                       $row->workflow->getOwnerTitle()->getFullURL()
+               );
+       }
+}

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

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

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

Reply via email to