4 * Created on Oct 13, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiBase.php");
32 * This action allows users to get their watchlist items in RSS/Atom formats.
33 * When executed, it performs a nested call to the API to get the needed data,
34 * and formats it in a proper format.
38 class ApiFeedWatchlist
extends ApiBase
{
40 public function __construct($main, $action) {
41 parent
:: __construct($main, $action);
45 * This module uses a custom feed wrapper printer.
47 public function getCustomPrinter() {
48 return new ApiFormatFeedWrapper($this->getMain());
52 * Make a nested call to the API to request watchlist items in the last $hours.
53 * Wrap the result as an RSS/Atom feed.
55 public function execute() {
57 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
60 $params = $this->extractRequestParams();
62 // limit to the number of hours going from now back
63 $endTime = wfTimestamp(TS_MW
, time() - intval($params['hours'] * 60 * 60));
65 // Prepare nested request
66 $fauxReq = new FauxRequest(array (
69 'siprop' => 'general',
70 'list' => 'watchlist',
71 'wlprop' => 'title|user|comment|timestamp',
72 'wldir' => 'older', // reverse order - from newest to oldest
73 'wlend' => $endTime, // stop at this time
78 $module = new ApiMain($fauxReq);
82 $data = $module->getResultData();
84 $feedItems = array ();
85 foreach ($data['query']['watchlist'] as $info) {
86 $feedItems[] = $this->createFeedItem($info);
89 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
90 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullUrl();
92 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
94 ApiFormatFeedWrapper
:: setResult($this->getResult(), $feed, $feedItems);
96 } catch (Exception
$e) {
98 // Error results should not be cached
99 $this->getMain()->setCacheMaxAge(0);
101 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
102 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullUrl();
104 $feedFormat = isset($params['feedformat']) ?
$params['feedformat'] : 'rss';
105 $feed = new $wgFeedClasses[$feedFormat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
108 if ($e instanceof UsageException
) {
109 $errorCode = $e->getCodeString();
111 // Something is seriously wrong
112 $errorCode = 'internal_api_error';
115 $errorText = $e->getMessage();
116 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
117 ApiFormatFeedWrapper
:: setResult($this->getResult(), $feed, $feedItems);
121 private function createFeedItem($info) {
122 $titleStr = $info['title'];
123 $title = Title
:: newFromText($titleStr);
124 $titleUrl = $title->getFullUrl();
125 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
126 $timestamp = $info['timestamp'];
127 $user = $info['user'];
129 $completeText = "$comment ($user)";
131 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
134 protected function getAllowedParams() {
135 global $wgFeedClasses;
136 $feedFormatNames = array_keys($wgFeedClasses);
138 'feedformat' => array (
139 ApiBase
:: PARAM_DFLT
=> 'rss',
140 ApiBase
:: PARAM_TYPE
=> $feedFormatNames
143 ApiBase
:: PARAM_DFLT
=> 24,
144 ApiBase
:: PARAM_TYPE
=> 'integer',
145 ApiBase
:: PARAM_MIN
=> 1,
146 ApiBase
:: PARAM_MAX
=> 72,
151 protected function getParamDescription() {
153 'feedformat' => 'The format of the feed',
154 'hours' => 'List pages modified within this many hours from now'
158 protected function getDescription() {
159 return 'This module returns a watchlist feed';
162 protected function getExamples() {
164 'api.php?action=feedwatchlist'
168 public function getVersion() {
169 return __CLASS__
. ': $Id$';