598e95de24ccd279f153ef87b1ca3489e2faa64f
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() {
45 $params = $this->extractRequestParams();
47 // limit to the number of hours going from now back
48 $endTime = wfTimestamp(TS_MW
, time() - intval($params['hours'] * 60 * 60));
50 // Prepare nested request
51 $fauxReq = new FauxRequest(array (
54 'siprop' => 'general',
55 'list' => 'watchlist',
56 'wlprop' => 'user|comment|timestamp',
57 'wldir' => 'older', // reverse order - from newest to oldest
58 'wlend' => $endTime, // stop at this time
63 $module = new ApiMain($fauxReq);
65 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
71 $data = $module->getResultData();
73 $feedItems = array ();
74 foreach ($data['query']['watchlist'] as $info) {
75 $feedItems[] = $this->createFeedItem($info);
78 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
79 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullUrl();
81 $feed = new $wgFeedClasses[$params['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 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
91 $feedUrl = SpecialPage
::getTitleFor( 'Watchlist' )->getFullUrl();
93 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
96 if ($e instanceof UsageException
) {
97 $errorCode = $e->getCodeString();
99 // Something is seriously wrong
100 $errorCode = 'internal_api_error';
103 $errorText = $e->getMessage();
104 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
105 ApiFormatFeedWrapper
:: setResult($this->getResult(), $feed, $feedItems);
109 private function createFeedItem($info) {
110 $titleStr = $info['title'];
111 $title = Title
:: newFromText($titleStr);
112 $titleUrl = $title->getFullUrl();
113 $comment = isset( $info['comment'] ) ?
$info['comment'] : null;
114 $timestamp = $info['timestamp'];
115 $user = $info['user'];
117 $completeText = "$comment ($user)";
119 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
122 protected function getAllowedParams() {
123 global $wgFeedClasses;
124 $feedFormatNames = array_keys($wgFeedClasses);
126 'feedformat' => array (
127 ApiBase
:: PARAM_DFLT
=> 'rss',
128 ApiBase
:: PARAM_TYPE
=> $feedFormatNames
131 ApiBase
:: PARAM_DFLT
=> 24,
132 ApiBase
:: PARAM_TYPE
=> 'integer',
133 ApiBase
:: PARAM_MIN
=> 1,
134 ApiBase
:: PARAM_MAX
=> 72,
139 protected function getParamDescription() {
141 'feedformat' => 'The format of the feed',
142 'hours' => 'List pages modified within this many hours from now'
146 protected function getDescription() {
147 return 'This module returns a watchlist feed';
150 protected function getExamples() {
152 'api.php?action=feedwatchlist'
156 public function getVersion() {
157 return __CLASS__
. ': $Id$';