merged master
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
index 34c6650..eee8fa1 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on Oct 13, 2006
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on Oct 13, 2006
  *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( "ApiBase.php" );
-}
-
 /**
  * This action allows users to get their watchlist items in RSS/Atom formats.
  * When executed, it performs a nested call to the API to get the needed data,
@@ -43,25 +39,39 @@ class ApiFeedWatchlist extends ApiBase {
 
        /**
         * This module uses a custom feed wrapper printer.
+        *
+        * @return ApiFormatFeedWrapper
         */
        public function getCustomPrinter() {
                return new ApiFormatFeedWrapper( $this->getMain() );
        }
 
+       private $linkToDiffs = false;
+
        /**
         * Make a nested call to the API to request watchlist items in the last $hours.
         * Wrap the result as an RSS/Atom feed.
         */
        public function execute() {
-               global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgContLanguageCode;
+               global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
 
                try {
                        $params = $this->extractRequestParams();
 
+                       if( !$wgFeed ) {
+                               $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
+                       }
+
+                       if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
+                               $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
+                       }
+                       if ( !is_null( $params['wlexcludeuser'] ) ) {
+                               $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
+                       }
+
                        // limit to the number of hours going from now back
                        $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
 
-                       $dbr = wfGetDB( DB_SLAVE );
                        // Prepare parameters for nested request
                        $fauxReqArr = array(
                                'action' => 'query',
@@ -70,7 +80,7 @@ class ApiFeedWatchlist extends ApiBase {
                                'list' => 'watchlist',
                                'wlprop' => 'title|user|comment|timestamp',
                                'wldir' => 'older', // reverse order - from newest to oldest
-                               'wlend' => $dbr->timestamp( $endTime ), // stop at this time
+                               'wlend' => $endTime, // stop at this time
                                'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
                        );
 
@@ -81,8 +91,14 @@ class ApiFeedWatchlist extends ApiBase {
                                $fauxReqArr['wltoken'] = $params['wltoken'];
                        }
 
+                       // Support linking to diffs instead of article
+                       if ( $params['linktodiffs'] ) {
+                               $this->linkToDiffs = true;
+                               $fauxReqArr['wlprop'] .= '|ids';
+                       }
+
                        // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
-                       if ( !is_null( $params['allrev'] ) ) {
+                       if ( $params['allrev'] ) {
                                $fauxReqArr['wlallrev'] = '';
                        }
 
@@ -101,10 +117,12 @@ class ApiFeedWatchlist extends ApiBase {
                                $feedItems[] = $this->createFeedItem( $info );
                        }
 
-                       $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
+                       $msg = wfMsgForContent( 'watchlist' );
+
+                       $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
                        $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
 
-                       $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
+                       $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
 
                        ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
 
@@ -113,7 +131,7 @@ class ApiFeedWatchlist extends ApiBase {
                        // Error results should not be cached
                        $this->getMain()->setCacheMaxAge( 0 );
 
-                       $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
+                       $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
                        $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
 
                        $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
@@ -132,10 +150,18 @@ class ApiFeedWatchlist extends ApiBase {
                }
        }
 
+       /**
+        * @param $info array
+        * @return FeedItem
+        */
        private function createFeedItem( $info ) {
                $titleStr = $info['title'];
                $title = Title::newFromText( $titleStr );
-               $titleUrl = $title->getFullURL();
+               if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
+                       $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
+               } else {
+                       $titleUrl = $title->getFullURL();
+               }
                $comment = isset( $info['comment'] ) ? $info['comment'] : null;
                $timestamp = $info['timestamp'];
                $user = $info['user'];
@@ -159,13 +185,17 @@ class ApiFeedWatchlist extends ApiBase {
                                ApiBase::PARAM_MIN => 1,
                                ApiBase::PARAM_MAX => 72,
                        ),
-                       'allrev' => null,
+                       'allrev' => false,
                        'wlowner' => array(
                                ApiBase::PARAM_TYPE => 'user'
                        ),
                        'wltoken' => array(
                                ApiBase::PARAM_TYPE => 'string'
-                       )
+                       ),
+                       'wlexcludeuser' => array(
+                               ApiBase::PARAM_TYPE => 'user'
+                       ),
+                       'linktodiffs' => false,
                );
        }
 
@@ -174,21 +204,35 @@ class ApiFeedWatchlist extends ApiBase {
                        'feedformat' => 'The format of the feed',
                        'hours'      => 'List pages modified within this many hours from now',
                        'allrev'     => 'Include multiple revisions of the same page within given timeframe',
-                       'wlowner'     => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
-                       'wltoken'    => 'Security token that requested user set in their preferences'
+                       'wlowner'    => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}wltoken if it's not you)",
+                       'wltoken'    => 'Security token that requested user set in their preferences',
+                       'wlexcludeuser' => 'A user whose edits should not be shown in the watchlist',
+                       'linktodiffs' => 'Link to change differences instead of article pages',
                );
        }
 
        public function getDescription() {
-               return 'This module returns a watchlist feed';
+               return 'Returns a watchlist feed';
+       }
+
+       public function getPossibleErrors() {
+               return array_merge( parent::getPossibleErrors(), array(
+                       array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
+                       array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
+               ) );
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
-                       'api.php?action=feedwatchlist'
+                       'api.php?action=feedwatchlist',
+                       'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
                );
        }
 
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }