Follow-up I0b781c11 (2a55449): use User::getAutomaticGroups().
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 13, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * This action allows users to get their watchlist items in RSS/Atom formats.
29 * When executed, it performs a nested call to the API to get the needed data,
30 * and formats it in a proper format.
31 *
32 * @ingroup API
33 */
34 class ApiFeedWatchlist extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 /**
41 * This module uses a custom feed wrapper printer.
42 *
43 * @return ApiFormatFeedWrapper
44 */
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
47 }
48
49 private $linkToDiffs = false;
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 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
57
58 try {
59 $params = $this->extractRequestParams();
60
61 if( !$wgFeed ) {
62 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
63 }
64
65 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
66 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
67 }
68 if ( !is_null( $params['wlexcludeuser'] ) ) {
69 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
70 }
71
72 // limit to the number of hours going from now back
73 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
74
75 // Prepare parameters for nested request
76 $fauxReqArr = array(
77 'action' => 'query',
78 'meta' => 'siteinfo',
79 'siprop' => 'general',
80 'list' => 'watchlist',
81 'wlprop' => 'title|user|comment|timestamp',
82 'wldir' => 'older', // reverse order - from newest to oldest
83 'wlend' => $endTime, // stop at this time
84 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
85 );
86
87 if ( !is_null( $params['wlowner'] ) ) {
88 $fauxReqArr['wlowner'] = $params['wlowner'];
89 }
90 if ( !is_null( $params['wltoken'] ) ) {
91 $fauxReqArr['wltoken'] = $params['wltoken'];
92 }
93
94 // Support linking to diffs instead of article
95 if ( $params['linktodiffs'] ) {
96 $this->linkToDiffs = true;
97 $fauxReqArr['wlprop'] .= '|ids';
98 }
99
100 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
101 if ( $params['allrev'] ) {
102 $fauxReqArr['wlallrev'] = '';
103 }
104
105 // Create the request
106 $fauxReq = new FauxRequest( $fauxReqArr );
107
108 // Execute
109 $module = new ApiMain( $fauxReq );
110 $module->execute();
111
112 // Get data array
113 $data = $module->getResultData();
114
115 $feedItems = array();
116 foreach ( (array)$data['query']['watchlist'] as $info ) {
117 $feedItems[] = $this->createFeedItem( $info );
118 }
119
120 $msg = wfMsgForContent( 'watchlist' );
121
122 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
123 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
124
125 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
126
127 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
128
129 } catch ( Exception $e ) {
130
131 // Error results should not be cached
132 $this->getMain()->setCacheMaxAge( 0 );
133
134 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
135 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
136
137 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
138 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
139
140 if ( $e instanceof UsageException ) {
141 $errorCode = $e->getCodeString();
142 } else {
143 // Something is seriously wrong
144 $errorCode = 'internal_api_error';
145 }
146
147 $errorText = $e->getMessage();
148 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
149 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
150 }
151 }
152
153 /**
154 * @param $info array
155 * @return FeedItem
156 */
157 private function createFeedItem( $info ) {
158 $titleStr = $info['title'];
159 $title = Title::newFromText( $titleStr );
160 if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
161 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
162 } else {
163 $titleUrl = $title->getFullURL();
164 }
165 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
166 $timestamp = $info['timestamp'];
167 $user = $info['user'];
168
169 $completeText = "$comment ($user)";
170
171 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
172 }
173
174 public function getAllowedParams() {
175 global $wgFeedClasses;
176 $feedFormatNames = array_keys( $wgFeedClasses );
177 return array (
178 'feedformat' => array(
179 ApiBase::PARAM_DFLT => 'rss',
180 ApiBase::PARAM_TYPE => $feedFormatNames
181 ),
182 'hours' => array(
183 ApiBase::PARAM_DFLT => 24,
184 ApiBase::PARAM_TYPE => 'integer',
185 ApiBase::PARAM_MIN => 1,
186 ApiBase::PARAM_MAX => 72,
187 ),
188 'allrev' => false,
189 'wlowner' => array(
190 ApiBase::PARAM_TYPE => 'user'
191 ),
192 'wltoken' => array(
193 ApiBase::PARAM_TYPE => 'string'
194 ),
195 'wlexcludeuser' => array(
196 ApiBase::PARAM_TYPE => 'user'
197 ),
198 'linktodiffs' => false,
199 );
200 }
201
202 public function getParamDescription() {
203 return array(
204 'feedformat' => 'The format of the feed',
205 'hours' => 'List pages modified within this many hours from now',
206 'allrev' => 'Include multiple revisions of the same page within given timeframe',
207 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}wltoken if it's not you)",
208 'wltoken' => 'Security token that requested user set in their preferences',
209 'wlexcludeuser' => 'A user whose edits should not be shown in the watchlist',
210 'linktodiffs' => 'Link to change differences instead of article pages',
211 );
212 }
213
214 public function getDescription() {
215 return 'Returns a watchlist feed';
216 }
217
218 public function getPossibleErrors() {
219 return array_merge( parent::getPossibleErrors(), array(
220 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
221 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
222 ) );
223 }
224
225 public function getExamples() {
226 return array(
227 'api.php?action=feedwatchlist',
228 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
229 );
230 }
231
232 public function getHelpUrls() {
233 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
234 }
235
236 public function getVersion() {
237 return __CLASS__ . ': $Id$';
238 }
239 }