Merge "Allow API results to wrap long lines"
[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 /**
37 * This module uses a custom feed wrapper printer.
38 *
39 * @return ApiFormatFeedWrapper
40 */
41 public function getCustomPrinter() {
42 return new ApiFormatFeedWrapper( $this->getMain() );
43 }
44
45 private $linkToDiffs = false;
46
47 /**
48 * Make a nested call to the API to request watchlist items in the last $hours.
49 * Wrap the result as an RSS/Atom feed.
50 */
51 public function execute() {
52 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
53
54 try {
55 $params = $this->extractRequestParams();
56
57 if( !$wgFeed ) {
58 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
59 }
60
61 if( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
62 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
63 }
64 if ( !is_null( $params['wlexcludeuser'] ) ) {
65 $fauxReqArr['wlexcludeuser'] = $params['wlexcludeuser'];
66 }
67
68 // limit to the number of hours going from now back
69 $endTime = wfTimestamp( TS_MW, time() - intval( $params['hours'] * 60 * 60 ) );
70
71 // Prepare parameters for nested request
72 $fauxReqArr = array(
73 'action' => 'query',
74 'meta' => 'siteinfo',
75 'siprop' => 'general',
76 'list' => 'watchlist',
77 'wlprop' => 'title|user|comment|timestamp',
78 'wldir' => 'older', // reverse order - from newest to oldest
79 'wlend' => $endTime, // stop at this time
80 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
81 );
82
83 if ( !is_null( $params['wlowner'] ) ) {
84 $fauxReqArr['wlowner'] = $params['wlowner'];
85 }
86 if ( !is_null( $params['wltoken'] ) ) {
87 $fauxReqArr['wltoken'] = $params['wltoken'];
88 }
89
90 // Support linking to diffs instead of article
91 if ( $params['linktodiffs'] ) {
92 $this->linkToDiffs = true;
93 $fauxReqArr['wlprop'] .= '|ids';
94 }
95
96 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
97 if ( $params['allrev'] ) {
98 $fauxReqArr['wlallrev'] = '';
99 }
100
101 // Create the request
102 $fauxReq = new FauxRequest( $fauxReqArr );
103
104 // Execute
105 $module = new ApiMain( $fauxReq );
106 $module->execute();
107
108 // Get data array
109 $data = $module->getResultData();
110
111 $feedItems = array();
112 foreach ( (array)$data['query']['watchlist'] as $info ) {
113 $feedItems[] = $this->createFeedItem( $info );
114 }
115
116 $msg = wfMessage( 'watchlist' )->inContentLanguage()->text();
117
118 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
119 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
120
121 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( $msg ), $feedUrl );
122
123 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
124
125 } catch ( Exception $e ) {
126
127 // Error results should not be cached
128 $this->getMain()->setCacheMaxAge( 0 );
129
130 $feedTitle = $wgSitename . ' - Error - ' . wfMessage( 'watchlist' )->inContentLanguage()->text() . ' [' . $wgLanguageCode . ']';
131 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
132
133 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
134 $msg = wfMessage( 'watchlist' )->inContentLanguage()->escaped();
135 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, $msg, $feedUrl );
136
137 if ( $e instanceof UsageException ) {
138 $errorCode = $e->getCodeString();
139 } else {
140 // Something is seriously wrong
141 $errorCode = 'internal_api_error';
142 }
143
144 $errorText = $e->getMessage();
145 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
146 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
147 }
148 }
149
150 /**
151 * @param $info array
152 * @return FeedItem
153 */
154 private function createFeedItem( $info ) {
155 $titleStr = $info['title'];
156 $title = Title::newFromText( $titleStr );
157 if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
158 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
159 } else {
160 $titleUrl = $title->getFullURL();
161 }
162 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
163 $timestamp = $info['timestamp'];
164 $user = $info['user'];
165
166 $completeText = "$comment ($user)";
167
168 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
169 }
170
171 public function getAllowedParams() {
172 global $wgFeedClasses;
173 $feedFormatNames = array_keys( $wgFeedClasses );
174 return array (
175 'feedformat' => array(
176 ApiBase::PARAM_DFLT => 'rss',
177 ApiBase::PARAM_TYPE => $feedFormatNames
178 ),
179 'hours' => array(
180 ApiBase::PARAM_DFLT => 24,
181 ApiBase::PARAM_TYPE => 'integer',
182 ApiBase::PARAM_MIN => 1,
183 ApiBase::PARAM_MAX => 72,
184 ),
185 'allrev' => false,
186 'wlowner' => array(
187 ApiBase::PARAM_TYPE => 'user'
188 ),
189 'wltoken' => array(
190 ApiBase::PARAM_TYPE => 'string'
191 ),
192 'wlexcludeuser' => array(
193 ApiBase::PARAM_TYPE => 'user'
194 ),
195 'linktodiffs' => false,
196 );
197 }
198
199 public function getParamDescription() {
200 return array(
201 'feedformat' => 'The format of the feed',
202 'hours' => 'List pages modified within this many hours from now',
203 'allrev' => 'Include multiple revisions of the same page within given timeframe',
204 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}wltoken if it's not you)",
205 'wltoken' => 'Security token that requested user set in their preferences',
206 'wlexcludeuser' => 'A user whose edits should not be shown in the watchlist',
207 'linktodiffs' => 'Link to change differences instead of article pages',
208 );
209 }
210
211 public function getDescription() {
212 return 'Returns a watchlist feed';
213 }
214
215 public function getPossibleErrors() {
216 return array_merge( parent::getPossibleErrors(), array(
217 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
218 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
219 ) );
220 }
221
222 public function getExamples() {
223 return array(
224 'api.php?action=feedwatchlist',
225 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
226 );
227 }
228
229 public function getHelpUrls() {
230 return 'https://www.mediawiki.org/wiki/API:Watchlist_feed';
231 }
232 }