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