Remove $wgContLanguageCode usage in core.
[lhc/web/wiklou.git] / includes / api / ApiFeedWatchlist.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
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 /**
53 * Make a nested call to the API to request watchlist items in the last $hours.
54 * Wrap the result as an RSS/Atom feed.
55 */
56 public function execute() {
57 global $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
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 }
81 if ( !is_null( $params['wltoken'] ) ) {
82 $fauxReqArr['wltoken'] = $params['wltoken'];
83 }
84
85 // Check for 'allrev' parameter, and if found, show all revisions to each page on wl.
86 if ( !is_null( $params['allrev'] ) ) {
87 $fauxReqArr['wlallrev'] = '';
88 }
89
90 // Create the request
91 $fauxReq = new FauxRequest( $fauxReqArr );
92
93 // Execute
94 $module = new ApiMain( $fauxReq );
95 $module->execute();
96
97 // Get data array
98 $data = $module->getResultData();
99
100 $feedItems = array();
101 foreach ( (array)$data['query']['watchlist'] as $info ) {
102 $feedItems[] = $this->createFeedItem( $info );
103 }
104
105 $feedTitle = $wgSitename . ' - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
106 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
107
108 $feed = new $wgFeedClasses[$params['feedformat']] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
109
110 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
111
112 } catch ( Exception $e ) {
113
114 // Error results should not be cached
115 $this->getMain()->setCacheMaxAge( 0 );
116
117 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'watchlist' ) . ' [' . $wgLanguageCode . ']';
118 $feedUrl = SpecialPage::getTitleFor( 'Watchlist' )->getFullURL();
119
120 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
121 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'watchlist' ) ), $feedUrl );
122
123 if ( $e instanceof UsageException ) {
124 $errorCode = $e->getCodeString();
125 } else {
126 // Something is seriously wrong
127 $errorCode = 'internal_api_error';
128 }
129
130 $errorText = $e->getMessage();
131 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
132 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
133 }
134 }
135
136 private function createFeedItem( $info ) {
137 $titleStr = $info['title'];
138 $title = Title::newFromText( $titleStr );
139 $titleUrl = $title->getFullURL();
140 $comment = isset( $info['comment'] ) ? $info['comment'] : null;
141 $timestamp = $info['timestamp'];
142 $user = $info['user'];
143
144 $completeText = "$comment ($user)";
145
146 return new FeedItem( $titleStr, $completeText, $titleUrl, $timestamp, $user );
147 }
148
149 public function getAllowedParams() {
150 global $wgFeedClasses;
151 $feedFormatNames = array_keys( $wgFeedClasses );
152 return array (
153 'feedformat' => array(
154 ApiBase::PARAM_DFLT => 'rss',
155 ApiBase::PARAM_TYPE => $feedFormatNames
156 ),
157 'hours' => array(
158 ApiBase::PARAM_DFLT => 24,
159 ApiBase::PARAM_TYPE => 'integer',
160 ApiBase::PARAM_MIN => 1,
161 ApiBase::PARAM_MAX => 72,
162 ),
163 'allrev' => null,
164 'wlowner' => array(
165 ApiBase::PARAM_TYPE => 'user'
166 ),
167 'wltoken' => array(
168 ApiBase::PARAM_TYPE => 'string'
169 )
170 );
171 }
172
173 public function getParamDescription() {
174 return array(
175 'feedformat' => 'The format of the feed',
176 'hours' => 'List pages modified within this many hours from now',
177 'allrev' => 'Include multiple revisions of the same page within given timeframe',
178 'wlowner' => "The user whose watchlist you want (must be accompanied by {$this->getModulePrefix()}token if it's not you)",
179 'wltoken' => 'Security token that requested user set in their preferences'
180 );
181 }
182
183 public function getDescription() {
184 return 'This module returns a watchlist feed';
185 }
186
187 protected function getExamples() {
188 return array(
189 'api.php?action=feedwatchlist'
190 );
191 }
192
193 public function getVersion() {
194 return __CLASS__ . ': $Id$';
195 }
196 }