API:
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2
3 /*
4 * Created on Oct 13, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ("ApiBase.php");
29 }
30
31 /**
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.
35 *
36 * @addtogroup API
37 */
38 class ApiFeedWatchlist extends ApiBase {
39
40 public function __construct($main, $action) {
41 parent :: __construct($main, $action);
42 }
43
44 /**
45 * This module uses a custom feed wrapper printer.
46 */
47 public function getCustomPrinter() {
48 return new ApiFormatFeedWrapper($this->getMain());
49 }
50
51 public function execute() {
52
53 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
54
55 try {
56 $params = $this->extractRequestParams();
57
58 // limit to the number of hours going from now back
59 $endTime = wfTimestamp(TS_MW, time() - intval($params['hours'] * 60 * 60));
60
61 // Prepare nested request
62 $fauxReq = new FauxRequest(array (
63 'action' => 'query',
64 'meta' => 'siteinfo',
65 'siprop' => 'general',
66 'list' => 'watchlist',
67 'wlprop' => 'user|comment|timestamp',
68 'wldir' => 'older', // reverse order - from newest to oldest
69 'wlend' => $endTime, // stop at this time
70 'wllimit' => 50
71 ));
72
73 // Execute
74 $module = new ApiMain($fauxReq);
75 $module->execute();
76
77 // Get data array
78 $data = $module->getResultData();
79
80 $feedItems = array ();
81 foreach ($data['query']['watchlist'] as $info) {
82 $feedItems[] = $this->createFeedItem($info);
83 }
84
85 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
86 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
87
88 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
89
90 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
91
92 } catch (Exception $e) {
93
94 // Error results should not be cached
95 $this->getMain()->setCacheMaxAge(0);
96
97 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
98 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
99
100 $feedFormat = isset($params['feedformat']) ? $params['feedformat'] : 'rss';
101 $feed = new $wgFeedClasses[$feedFormat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
102
103
104 if ($e instanceof UsageException) {
105 $errorCode = $e->getCodeString();
106 } else {
107 // Something is seriously wrong
108 $errorCode = 'internal_api_error';
109 }
110
111 $errorText = $e->getMessage();
112 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
113 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
114 }
115 }
116
117 private function createFeedItem($info) {
118 $titleStr = $info['title'];
119 $title = Title :: newFromText($titleStr);
120 $titleUrl = $title->getFullUrl();
121 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
122 $timestamp = $info['timestamp'];
123 $user = $info['user'];
124
125 $completeText = "$comment ($user)";
126
127 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
128 }
129
130 protected function getAllowedParams() {
131 global $wgFeedClasses;
132 $feedFormatNames = array_keys($wgFeedClasses);
133 return array (
134 'feedformat' => array (
135 ApiBase :: PARAM_DFLT => 'rss',
136 ApiBase :: PARAM_TYPE => $feedFormatNames
137 ),
138 'hours' => array (
139 ApiBase :: PARAM_DFLT => 24,
140 ApiBase :: PARAM_TYPE => 'integer',
141 ApiBase :: PARAM_MIN => 1,
142 ApiBase :: PARAM_MAX => 72,
143 )
144 );
145 }
146
147 protected function getParamDescription() {
148 return array (
149 'feedformat' => 'The format of the feed',
150 'hours' => 'List pages modified within this many hours from now'
151 );
152 }
153
154 protected function getDescription() {
155 return 'This module returns a watchlist feed';
156 }
157
158 protected function getExamples() {
159 return array (
160 'api.php?action=feedwatchlist'
161 );
162 }
163
164 public function getVersion() {
165 return __CLASS__ . ': $Id$';
166 }
167 }
168 ?>