fix for bug33214 - catch all exceptions in api execute and provides necessary paramet...
[lhc/web/wiklou.git] / includes / api / ApiFeedContributions.php
1 <?php
2 /**
3 *
4 *
5 * Created on June 06, 2011
6 *
7 * Copyright © 2011 Sam Reed
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 * @ingroup API
29 */
30 class ApiFeedContributions extends ApiBase {
31
32 public function __construct( $main, $action ) {
33 parent::__construct( $main, $action );
34 }
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 public function execute() {
46
47 global $wgFeed, $wgFeedClasses, $wgSitename, $wgLanguageCode;
48
49 try {
50 $params = $this->extractRequestParams();
51
52 if( !$wgFeed ) {
53 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
54 }
55
56 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
57 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
58 }
59
60 global $wgMiserMode;
61 if ( $params['showsizediff'] && $wgMiserMode ) {
62 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
63 }
64
65 $msg = wfMsgForContent( 'Contributions' );
66 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
67 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
68
69 $target = $params['user'] == 'newbies'
70 ? 'newbies'
71 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
72
73 $feed = new $wgFeedClasses[$params['feedformat']] (
74 $feedTitle,
75 htmlspecialchars( $msg ),
76 $feedUrl
77 );
78
79 $pager = new ContribsPager( $this->getContext(), array(
80 'target' => $target,
81 'namespace' => $params['namespace'],
82 'year' => $params['year'],
83 'month' => $params['month'],
84 'tagFilter' => $params['tagfilter'],
85 'deletedOnly' => $params['deletedonly'],
86 'topOnly' => $params['toponly'],
87 'showSizeDiff' => $params['showsizediff'],
88 ) );
89
90 $feedItems = array();
91 if( $pager->getNumRows() > 0 ) {
92 foreach ( $pager->mResult as $row ) {
93 $feedItems[] = $this->feedItem( $row );
94 }
95 }
96
97 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
98
99 } catch ( Exception $e ) {
100 // Error results should not be cached
101 $this->getMain()->setCacheMaxAge( 0 );
102
103 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'contributions' ) . ' [' . $wgLanguageCode . ']';
104 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
105
106 $feedFormat = isset( $params['feedformat'] ) ? $params['feedformat'] : 'rss';
107 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'contributions' ) ), $feedUrl );
108
109 if ( $e instanceof UsageException ) {
110 $errorCode = $e->getCodeString();
111 } else {
112 // Something is seriously wrong
113 $errorCode = 'internal_api_error';
114 }
115
116 $errorText = $e->getMessage();
117 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
118 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
119 }
120 }
121
122 protected function feedItem( $row ) {
123 $title = Title::MakeTitle( intval( $row->page_namespace ), $row->page_title );
124 if( $title ) {
125 $date = $row->rev_timestamp;
126 $comments = $title->getTalkPage()->getFullURL();
127 $revision = Revision::newFromRow( $row );
128
129 return new FeedItem(
130 $title->getPrefixedText(),
131 $this->feedItemDesc( $revision ),
132 $title->getFullURL(),
133 $date,
134 $this->feedItemAuthor( $revision ),
135 $comments
136 );
137 } else {
138 return null;
139 }
140 }
141
142 /**
143 * @param $revision Revision
144 * @return string
145 */
146 protected function feedItemAuthor( $revision ) {
147 return $revision->getUserText();
148 }
149
150 /**
151 * @param $revision Revision
152 * @return string
153 */
154 protected function feedItemDesc( $revision ) {
155 if( $revision ) {
156 return '<p>' . htmlspecialchars( $revision->getUserText() ) . wfMsgForContent( 'colon-separator' ) .
157 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
158 "</p>\n<hr />\n<div>" .
159 nl2br( htmlspecialchars( $revision->getText() ) ) . "</div>";
160 }
161 return '';
162 }
163
164 public function getAllowedParams() {
165 global $wgFeedClasses;
166 $feedFormatNames = array_keys( $wgFeedClasses );
167 return array (
168 'feedformat' => array(
169 ApiBase::PARAM_DFLT => 'rss',
170 ApiBase::PARAM_TYPE => $feedFormatNames
171 ),
172 'user' => array(
173 ApiBase::PARAM_TYPE => 'user',
174 ApiBase::PARAM_REQUIRED => true,
175 ),
176 'namespace' => array(
177 ApiBase::PARAM_TYPE => 'namespace'
178 ),
179 'year' => array(
180 ApiBase::PARAM_TYPE => 'integer'
181 ),
182 'month' => array(
183 ApiBase::PARAM_TYPE => 'integer'
184 ),
185 'tagfilter' => array(
186 ApiBase::PARAM_ISMULTI => true,
187 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
188 ApiBase::PARAM_DFLT => '',
189 ),
190 'deletedonly' => false,
191 'toponly' => false,
192 'showsizediff' => false,
193 );
194 }
195
196 public function getParamDescription() {
197 return array(
198 'feedformat' => 'The format of the feed',
199 'user' => 'What users to get the contributions for',
200 'namespace' => 'What namespace to filter the contributions by',
201 'year' => 'From year (and earlier)',
202 'month' => 'From month (and earlier)',
203 'tagfilter' => 'Filter contributions that have these tags',
204 'deletedonly' => 'Show only deleted contributions',
205 'toponly' => 'Only show edits that are latest revisions',
206 'showsizediff' => 'Show the size difference between revisions. Disabled in Miser Mode',
207 );
208 }
209
210 public function getDescription() {
211 return 'Returns a user contributions feed';
212 }
213
214 public function getPossibleErrors() {
215 return array_merge( parent::getPossibleErrors(), array(
216 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
217 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
218 array( 'code' => 'sizediffdisabled', 'info' => 'Size difference is disabled in Miser Mode' ),
219 ) );
220 }
221
222 public function getExamples() {
223 return array(
224 'api.php?action=feedcontributions&user=Reedy',
225 );
226 }
227
228 public function getVersion() {
229 return __CLASS__ . ': $Id$';
230 }
231 }