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