Introduce ApiFeedContributions::feedItem hook
authorErik Bernhardson <ebernhardson@wikimedia.org>
Wed, 14 Jan 2015 21:52:50 +0000 (13:52 -0800)
committerErik Bernhardson <ebernhardson@wikimedia.org>
Fri, 23 Jan 2015 18:11:22 +0000 (10:11 -0800)
ContribsPager, which is used by ApiFeedContributions, can return more
than just revision rows.  This is handled in the html side within the
ContributionsLineEnding hook.  ApiFeedContributions had no special
handling so here I have added a simple hook the provides the data
from ContribsPager and allows subscribers to provide the appropriate
FeedItem instance.

Bug: T85229
Change-Id: I27c77cc682ba801c40361c76b67398108ca1a592

docs/hooks.txt
includes/api/ApiFeedContributions.php

index 2de78dd..4717c38 100644 (file)
@@ -372,6 +372,17 @@ $editPage : the EditPage object
 $text : the new text of the article (has yet to be saved)
 &$resultArr : data in this array will be added to the API result
 
 $text : the new text of the article (has yet to be saved)
 &$resultArr : data in this array will be added to the API result
 
+'ApiFeedContributions::feedItem': Called to convert the result of ContribsPager
+into a FeedItem instance that ApiFeedContributions can consume. Implementors of
+this hook may cancel the hook to signal that the item is not viewable in the
+provided context.
+$row: A row of data from ContribsPager.  The set of data returned by ContribsPager
+ can be adjusted by handling the ContribsPager::reallyDoQuery hook.
+$context: An IContextSource implementation.
+&$feedItem: Set this to a FeedItem instance if the callback can handle the provided
+ row. This is provided to the hook as a null, if it is non null then another callback
+ has already handled the hook.
+
 'ApiFormatHighlight': Use to syntax-highlight API pretty-printed output. When
 highlighting, add output to $context->getOutput() and return false.
 $context: An IContextSource.
 'ApiFormatHighlight': Use to syntax-highlight API pretty-printed output. When
 highlighting, add output to $context->getOutput() and return false.
 $context: An IContextSource.
index ced5f0c..edda672 100644 (file)
@@ -95,7 +95,10 @@ class ApiFeedContributions extends ApiBase {
                                if ( ++$count > $limit ) {
                                        break;
                                }
                                if ( ++$count > $limit ) {
                                        break;
                                }
-                               $feedItems[] = $this->feedItem( $row );
+                               $item = $this->feedItem( $row );
+                               if ( $item !== null ) {
+                                       $feedItems[] = $item;
+                               }
                        }
                }
 
                        }
                }
 
@@ -103,6 +106,23 @@ class ApiFeedContributions extends ApiBase {
        }
 
        protected function feedItem( $row ) {
        }
 
        protected function feedItem( $row ) {
+               // This hook is the api contributions equivalent to the
+               // ContributionsLineEnding hook. Hook implementers may cancel
+               // the hook to signal the user is not allowed to read this item.
+               $feedItem = null;
+               $hookResult = Hooks::run(
+                       'ApiFeedContributions::feedItem',
+                       array( $row, $this->getContext(), &$feedItem )
+               );
+               // Hook returned a valid feed item
+               if ( $feedItem instanceof FeedItem ) {
+                       return $feedItem;
+               // Hook was canceled and did not return a valid feed item
+               } elseif ( !$hookResult ) {
+                       return null;
+               }
+
+               // Hook completed and did not return a valid feed item
                $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
                if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
                        $date = $row->rev_timestamp;
                $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
                if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
                        $date = $row->rev_timestamp;