4 * Created on Oct 13, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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");
34 class ApiFeedWatchlist
extends ApiBase
{
36 public function __construct($main, $action) {
37 parent
:: __construct($main, $action);
40 public function getCustomPrinter() {
41 return new ApiFormatFeedWrapper($this->getMain());
44 public function execute() {
46 extract($this->extractRequestParams());
48 // limit to 1 day going from now back
49 $endTime = wfTimestamp(TS_MW
, time() - intval(1 * 24 * 60 * 60));
51 // Prepare nested request
52 $params = new FauxRequest(array (
55 'siprop' => 'general',
56 'list' => 'watchlist',
57 'wlprop' => 'user|comment|timestamp',
64 $module = new ApiMain($params);
70 $data = $module->getResultData();
72 $feedItems = array ();
73 foreach ($data['query']['watchlist'] as $info) {
74 $feedItems[] = $this->createFeedItem($info);
77 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
78 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
79 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullUrl();
81 $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
83 ApiFormatFeedWrapper
:: setResult($this->getResult(), $feed, $feedItems);
85 } catch (Exception
$e) {
87 // Error results should not be cached
88 $this->getMain()->setCacheMaxAge(0);
90 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
91 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
92 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullUrl();
94 $feed = new $wgFeedClasses[$feedformat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
97 if ($e instanceof UsageException
) {
98 $errorCode = $e->getCodeString();
100 // Something is seriously wrong
101 $errorCode = 'internal_api_error';
104 $errorText = $e->getMessage();
105 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
106 ApiFormatFeedWrapper
:: setResult($this->getResult(), $feed, $feedItems);
110 private function createFeedItem($info) {
111 $titleStr = $info['title'];
112 $title = Title
:: newFromText($titleStr);
113 $titleUrl = $title->getFullUrl();
114 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
115 $timestamp = $info['timestamp'];
116 $user = $info['user'];
118 $completeText = "$comment ($user)";
120 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
123 protected function getAllowedParams() {
124 global $wgFeedClasses;
125 $feedFormatNames = array_keys($wgFeedClasses);
127 'feedformat' => array (
128 ApiBase
:: PARAM_DFLT
=> 'rss',
129 ApiBase
:: PARAM_TYPE
=> $feedFormatNames
134 protected function getParamDescription() {
136 'feedformat' => 'The format of the feed'
140 protected function getDescription() {
141 return 'This module returns a watchlist feed';
144 protected function getExamples() {
146 'api.php?action=feedwatchlist'
150 public function getVersion() {
151 return __CLASS__
. ': $Id$';