(minor) Make ApiFeedContributions respect $wgFeedLimit
[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 /**
33 * This module uses a custom feed wrapper printer.
34 *
35 * @return ApiFormatFeedWrapper
36 */
37 public function getCustomPrinter() {
38 return new ApiFormatFeedWrapper( $this->getMain() );
39 }
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43
44 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
45
46 if ( !$wgFeed ) {
47 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
48 }
49
50 if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
51 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
52 }
53
54 global $wgMiserMode;
55 if ( $params['showsizediff'] && $wgMiserMode ) {
56 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
57 }
58
59 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
60 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
61 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
62
63 $target = $params['user'] == 'newbies'
64 ? 'newbies'
65 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
66
67 $feed = new $wgFeedClasses[$params['feedformat']] (
68 $feedTitle,
69 htmlspecialchars( $msg ),
70 $feedUrl
71 );
72
73 $pager = new ContribsPager( $this->getContext(), array(
74 'target' => $target,
75 'namespace' => $params['namespace'],
76 'year' => $params['year'],
77 'month' => $params['month'],
78 'tagFilter' => $params['tagfilter'],
79 'deletedOnly' => $params['deletedonly'],
80 'topOnly' => $params['toponly'],
81 'showSizeDiff' => $params['showsizediff'],
82 ) );
83
84 if ( $pager->getLimit() > $wgFeedLimit ) {
85 $pager->setLimit( $wgFeedLimit );
86 }
87
88 $feedItems = array();
89 if ( $pager->getNumRows() > 0 ) {
90 foreach ( $pager->mResult as $row ) {
91 $feedItems[] = $this->feedItem( $row );
92 }
93 }
94
95 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
96 }
97
98 protected function feedItem( $row ) {
99 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
100 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
101 $date = $row->rev_timestamp;
102 $comments = $title->getTalkPage()->getFullURL();
103 $revision = Revision::newFromRow( $row );
104
105 return new FeedItem(
106 $title->getPrefixedText(),
107 $this->feedItemDesc( $revision ),
108 $title->getFullURL(),
109 $date,
110 $this->feedItemAuthor( $revision ),
111 $comments
112 );
113 }
114
115 return null;
116 }
117
118 /**
119 * @param $revision Revision
120 * @return string
121 */
122 protected function feedItemAuthor( $revision ) {
123 return $revision->getUserText();
124 }
125
126 /**
127 * @param $revision Revision
128 * @return string
129 */
130 protected function feedItemDesc( $revision ) {
131 if ( $revision ) {
132 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
133 $content = $revision->getContent();
134
135 if ( $content instanceof TextContent ) {
136 // only textual content has a "source view".
137 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
138 } else {
139 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
140 // contain JS magic and generally may not be suitable for inclusion in a feed.
141 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
142 //Compare also FeedUtils::formatDiffRow.
143 $html = '';
144 }
145
146 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
147 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
148 "</p>\n<hr />\n<div>" . $html . "</div>";
149 }
150
151 return '';
152 }
153
154 public function getAllowedParams() {
155 global $wgFeedClasses;
156 $feedFormatNames = array_keys( $wgFeedClasses );
157
158 return array(
159 'feedformat' => array(
160 ApiBase::PARAM_DFLT => 'rss',
161 ApiBase::PARAM_TYPE => $feedFormatNames
162 ),
163 'user' => array(
164 ApiBase::PARAM_TYPE => 'user',
165 ApiBase::PARAM_REQUIRED => true,
166 ),
167 'namespace' => array(
168 ApiBase::PARAM_TYPE => 'namespace'
169 ),
170 'year' => array(
171 ApiBase::PARAM_TYPE => 'integer'
172 ),
173 'month' => array(
174 ApiBase::PARAM_TYPE => 'integer'
175 ),
176 'tagfilter' => array(
177 ApiBase::PARAM_ISMULTI => true,
178 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
179 ApiBase::PARAM_DFLT => '',
180 ),
181 'deletedonly' => false,
182 'toponly' => false,
183 'showsizediff' => false,
184 );
185 }
186
187 public function getParamDescription() {
188 return array(
189 'feedformat' => 'The format of the feed',
190 'user' => 'What users to get the contributions for',
191 'namespace' => 'What namespace to filter the contributions by',
192 'year' => 'From year (and earlier)',
193 'month' => 'From month (and earlier)',
194 'tagfilter' => 'Filter contributions that have these tags',
195 'deletedonly' => 'Show only deleted contributions',
196 'toponly' => 'Only show edits that are latest revisions',
197 'showsizediff' => 'Show the size difference between revisions. Disabled in Miser Mode',
198 );
199 }
200
201 public function getDescription() {
202 return 'Returns a user contributions feed';
203 }
204
205 public function getPossibleErrors() {
206 return array_merge( parent::getPossibleErrors(), array(
207 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
208 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
209 array( 'code' => 'sizediffdisabled', 'info' => 'Size difference is disabled in Miser Mode' ),
210 ) );
211 }
212
213 public function getExamples() {
214 return array(
215 'api.php?action=feedcontributions&user=Reedy',
216 );
217 }
218 }