follow up r62353 Make ApiBase::requireOnlyOneParameter() accept parameters that are...
[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 * @ingroup 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, $wgFeedLimit, $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 $dbr = wfGetDB( DB_SLAVE );
66 // Prepare parameters for nested request
67 $fauxReqArr = array (
68 'action' => 'query',
69 'meta' => 'siteinfo',
70 'siprop' => 'general',
71 'list' => 'watchlist',
72 'wlprop' => 'title|user|comment|timestamp',
73 'wldir' => 'older', // reverse order - from newest to oldest
74 'wlend' => $dbr->timestamp( $endTime ), // stop at this time
75 'wllimit' => ( 50 > $wgFeedLimit ) ? $wgFeedLimit : 50
76 );
77
78 if ( !is_null( $params['wlowner'] ) )
79 $fauxReqArr['wlowner'] = $params['wlowner'];
80 if ( !is_null( $params['wltoken'] ) )
81 $fauxReqArr['wltoken'] = $params['wltoken'];
82
83 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
84 if ( ! is_null ( $params['allrev'] ) ) $fauxReqArr['wlallrev'] = '';
85
86 // Create the request
87 $fauxReq = new FauxRequest ( $fauxReqArr );
88
89 // Execute
90 $module = new ApiMain( $fauxReq );
91 $module->execute();
92
93 // Get data array
94 $data = $module->getResultData();
95
96 $feedItems = array ();
97 foreach ( (array)$data['query']['watchlist'] as $info ) {
98 $feedItems[] = $this->createFeedItem( $info );
99 }
100
101 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
102 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
103
104 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
105
106 ApiFormatFeedWrapper :: setResult( $this->getResult(), $feed, $feedItems );
107
108 } catch ( Exception $e ) {
109
110 // Error results should not be cached
111 $this->getMain()->setCacheMaxAge( 0 );
112
113 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgContLanguageCode . ']';
114 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullUrl();
115
116 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
117 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
118
119 if ( $e instanceof UsageException ) {
120 $errorCode = $e->getCodeString();
121 } else {
122 // Something is seriously wrong
123 $errorCode = 'internal_api_error';
124 }
125
126 $errorText = $e->getMessage();
127 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, "", "", "" );
128 ApiFormatFeedWrapper :: setResult( $this->getResult(), $feed, $feedItems );
129 }
130 }
131
132 private function createFeedItem( $info ) {
133 $titleStr = $info['title'];
134 $title = Title :: newFromText( $titleStr );
135 $titleUrl = $title->getFullUrl();
136 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
137 $timestamp = $info['timestamp'];
138 $user = $info['user'];
139
140 $completeText = "$comment ($user)";
141
142 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
143 }
144
145 public function getAllowedParams() {
146 global $wgFeedClasses;
147 $feedFormatNames = array_keys( $wgFeedClasses );
148 return array (
149 'feedformat' => array (
150 ApiBase :: PARAM_DFLT => 'rss',
151 ApiBase :: PARAM_TYPE => $feedFormatNames
152 ),
153 'hours' => array (
154 ApiBase :: PARAM_DFLT => 24,
155 ApiBase :: PARAM_TYPE => 'integer',
156 ApiBase :: PARAM_MIN => 1,
157 ApiBase :: PARAM_MAX => 72,
158 ),
159 'allrev' => null,
160 'wlowner' => array (
161 ApiBase :: PARAM_TYPE => 'user'
162 ),
163 'wltoken' => array (
164 ApiBase :: PARAM_TYPE => 'string'
165 )
166 );
167 }
168
169 public function getParamDescription() {
170 return array (
171 'feedformat' => 'The format of the feed',
172 'hours' => 'List pages modified within this many hours from now',
173 'allrev' => 'Include multiple revisions of the same page within given timeframe.',
174 'wlowner' => "The user whose watchlist you want (must be accompanied by wltoken if it's not you)",
175 'wltoken' => 'Security token that requested user set in their preferences'
176 );
177 }
178
179 public function getDescription() {
180 return 'This module returns a watchlist feed';
181 }
182
183 protected function getExamples() {
184 return array (
185 'api.php?action=feedwatchlist'
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }