APIEDIT BRANCH MERGE:
[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 <Firstname><Lastname>@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 /**
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.
54 */
55 public function execute() {
56
57 global $wgFeedClasses, $wgSitename, $wgContLanguageCode;
58
59 try {
60 $params = $this->extractRequestParams();
61
62 // limit to the number of hours going from now back
63 $endTime = wfTimestamp(TS_MW, time() - intval($params['hours'] * 60 * 60));
64
65 // Prepare parameters for nested request
66 $fauxReqArr = array (
67 'action' => 'query',
68 'meta' => 'siteinfo',
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
74 'wllimit' => 50
75 );
76
77 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
78 if ( ! is_null ( $params['allrev'] ) ) $fauxReqArr['wlallrev'] = '';
79
80 // Create the request
81 $fauxReq = new FauxRequest ( $fauxReqArr );
82
83 // Execute
84 $module = new ApiMain($fauxReq);
85 $module->execute();
86
87 // Get data array
88 $data = $module->getResultData();
89
90 $feedItems = array ();
91 foreach ($data['query']['watchlist'] as $info) {
92 $feedItems[] = $this->createFeedItem($info);
93 }
94
95 $feedTitle = $wgSitename . ' - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
96 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
97
98 $feed = new $wgFeedClasses[$params['feedformat']] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
99
100 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
101
102 } catch (Exception $e) {
103
104 // Error results should not be cached
105 $this->getMain()->setCacheMaxAge(0);
106
107 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent('watchlist') . ' [' . $wgContLanguageCode . ']';
108 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
109
110 $feedFormat = isset($params['feedformat']) ? $params['feedformat'] : 'rss';
111 $feed = new $wgFeedClasses[$feedFormat] ($feedTitle, htmlspecialchars(wfMsgForContent('watchlist')), $feedUrl);
112
113
114 if ($e instanceof UsageException) {
115 $errorCode = $e->getCodeString();
116 } else {
117 // Something is seriously wrong
118 $errorCode = 'internal_api_error';
119 }
120
121 $errorText = $e->getMessage();
122 $feedItems[] = new FeedItem("Error ($errorCode)", $errorText, "", "", "");
123 ApiFormatFeedWrapper :: setResult($this->getResult(), $feed, $feedItems);
124 }
125 }
126
127 private function createFeedItem($info) {
128 $titleStr = $info['title'];
129 $title = Title :: newFromText($titleStr);
130 $titleUrl = $title->getFullUrl();
131 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
132 $timestamp = $info['timestamp'];
133 $user = $info['user'];
134
135 $completeText = "$comment ($user)";
136
137 return new FeedItem($titleStr, $completeText, $titleUrl, $timestamp, $user);
138 }
139
140 protected function getAllowedParams() {
141 global $wgFeedClasses;
142 $feedFormatNames = array_keys($wgFeedClasses);
143 return array (
144 'feedformat' => array (
145 ApiBase :: PARAM_DFLT => 'rss',
146 ApiBase :: PARAM_TYPE => $feedFormatNames
147 ),
148 'hours' => array (
149 ApiBase :: PARAM_DFLT => 24,
150 ApiBase :: PARAM_TYPE => 'integer',
151 ApiBase :: PARAM_MIN => 1,
152 ApiBase :: PARAM_MAX => 72,
153 ),
154 'allrev' => null
155 );
156 }
157
158 protected function getParamDescription() {
159 return array (
160 'feedformat' => 'The format of the feed',
161 'hours' => 'List pages modified within this many hours from now',
162 'allrev' => 'Include multiple revisions of the same page within given timeframe.'
163 );
164 }
165
166 protected function getDescription() {
167 return 'This module returns a watchlist feed';
168 }
169
170 protected function getExamples() {
171 return array (
172 'api.php?action=feedwatchlist'
173 );
174 }
175
176 public function getVersion() {
177 return __CLASS__ . ': $Id$';
178 }
179 }
180