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