Based on diff to wikia, set more functions consistently public rather than protected
[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 public function getCustomPrinter() {
49 return new ApiFormatFeedWrapper( $this->getMain() );
50 }
51
52 private $linkToDiffs = false;
53
54 /**
55 * Make a nested call to the API to request watchlist items in the last $hours.
56 * Wrap the result as an RSS/Atom feed.
57 */
58 public function execute() {
59 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
60
61 try {
62 $params = $this->extractRequestParams();
63
64 if( !$wgFeed ) {
65 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
66 }
67
68 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
69 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
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 ( !is_null( $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 private function createFeedItem( $info ) {
154 $titleStr = $info['title'];
155 $title = Title::newFromText( $titleStr );
156 if ( $this->linkToDiffs && isset( $info['revid'] ) ) {
157 $titleUrl = $title->getFullURL( array( 'diff' => $info['revid'] ) );
158 } else {
159 $titleUrl = $title->getFullURL();
160 }
161 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
162 $timestamp = $info['timestamp'];
163 $user = $info['user'];
164
165 $completeText = "$comment ($user)";
166
167 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
168 }
169
170 public function getAllowedParams() {
171 global $wgFeedClasses;
172 $feedFormatNames = array_keys( $wgFeedClasses );
173 return array (
174 'feedformat' => array(
175 ApiBase::PARAM_DFLT => 'rss',
176 ApiBase::PARAM_TYPE => $feedFormatNames
177 ),
178 'hours' => array(
179 ApiBase::PARAM_DFLT => 24,
180 ApiBase::PARAM_TYPE => 'integer',
181 ApiBase::PARAM_MIN => 1,
182 ApiBase::PARAM_MAX => 72,
183 ),
184 'allrev' => false,
185 'wlowner' => array(
186 ApiBase::PARAM_TYPE => 'user'
187 ),
188 'wltoken' => array(
189 ApiBase::PARAM_TYPE => 'string'
190 ),
191 'linktodiffs' => false,
192 );
193 }
194
195 public function getParamDescription() {
196 return array(
197 'feedformat' => 'The format of the feed',
198 'hours' => 'List pages modified within this many hours from now',
199 'allrev' => 'Include multiple revisions of the same page within given timeframe',
200 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
201 'wltoken' => 'Security token that requested user set in their preferences',
202 'linktodiffs' => 'Link to change differences instead of article pages'
203 );
204 }
205
206 public function getDescription() {
207 return 'Returns a watchlist feed';
208 }
209
210 public function getPossibleErrors() {
211 return array_merge( parent::getPossibleErrors(), array(
212 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
213 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
214 ) );
215 }
216
217 public function getExamples() {
218 return array(
219 'api.php?action=feedwatchlist',
220 'api.php?action=feedwatchlist&allrev=&linktodiffs=&hours=6'
221 );
222 }
223
224 public function getHelpUrls() {
225 return 'http://www.mediawiki.org/wiki/API:Watchlist_feed';
226 }
227
228 public function getVersion() {
229 return __CLASS__ . ': $Id$';
230 }
231 }