5 * Created on June 06, 2011
7 * Copyright © 2011 Sam Reed
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.
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.
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
30 class ApiFeedContributions
extends ApiBase
{
32 public function __construct( $main, $action ) {
33 parent
::__construct( $main, $action );
37 * This module uses a custom feed wrapper printer.
39 * @return ApiFormatFeedWrapper
41 public function getCustomPrinter() {
42 return new ApiFormatFeedWrapper( $this->getMain() );
45 public function execute() {
47 global $wgFeed, $wgFeedClasses, $wgSitename, $wgLanguageCode;
50 $params = $this->extractRequestParams();
53 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
56 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
57 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
61 if ( $params['showsizediff'] && $wgMiserMode ) {
62 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
65 $msg = wfMsgForContent( 'Contributions' );
66 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
67 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
69 $target = $params['user'] == 'newbies'
71 : Title
::makeTitleSafe( NS_USER
, $params['user'] )->getText();
73 $feed = new $wgFeedClasses[$params['feedformat']] (
75 htmlspecialchars( $msg ),
79 $pager = new ContribsPager( $this->getContext(), array(
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'],
91 if( $pager->getNumRows() > 0 ) {
92 foreach ( $pager->mResult
as $row ) {
93 $feedItems[] = $this->feedItem( $row );
97 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
99 } catch ( Exception
$e ) {
100 // Error results should not be cached
101 $this->getMain()->setCacheMaxAge( 0 );
103 $feedTitle = $wgSitename . ' - Error - ' . wfMsgForContent( 'contributions' ) . ' [' . $wgLanguageCode . ']';
104 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
106 $feedFormat = isset( $params['feedformat'] ) ?
$params['feedformat'] : 'rss';
107 $feed = new $wgFeedClasses[$feedFormat] ( $feedTitle, htmlspecialchars( wfMsgForContent( 'contributions' ) ), $feedUrl );
109 if ( $e instanceof UsageException
) {
110 $errorCode = $e->getCodeString();
112 // Something is seriously wrong
113 $errorCode = 'internal_api_error';
116 $errorText = $e->getMessage();
117 $feedItems[] = new FeedItem( "Error ($errorCode)", $errorText, '', '', '' );
118 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
122 protected function feedItem( $row ) {
123 $title = Title
::MakeTitle( intval( $row->page_namespace
), $row->page_title
);
125 $date = $row->rev_timestamp
;
126 $comments = $title->getTalkPage()->getFullURL();
127 $revision = Revision
::newFromRow( $row );
130 $title->getPrefixedText(),
131 $this->feedItemDesc( $revision ),
132 $title->getFullURL(),
134 $this->feedItemAuthor( $revision ),
143 * @param $revision Revision
146 protected function feedItemAuthor( $revision ) {
147 return $revision->getUserText();
151 * @param $revision Revision
154 protected function feedItemDesc( $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>";
164 public function getAllowedParams() {
165 global $wgFeedClasses;
166 $feedFormatNames = array_keys( $wgFeedClasses );
168 'feedformat' => array(
169 ApiBase
::PARAM_DFLT
=> 'rss',
170 ApiBase
::PARAM_TYPE
=> $feedFormatNames
173 ApiBase
::PARAM_TYPE
=> 'user',
174 ApiBase
::PARAM_REQUIRED
=> true,
176 'namespace' => array(
177 ApiBase
::PARAM_TYPE
=> 'namespace'
180 ApiBase
::PARAM_TYPE
=> 'integer'
183 ApiBase
::PARAM_TYPE
=> 'integer'
185 'tagfilter' => array(
186 ApiBase
::PARAM_ISMULTI
=> true,
187 ApiBase
::PARAM_TYPE
=> array_values( ChangeTags
::listDefinedTags() ),
188 ApiBase
::PARAM_DFLT
=> '',
190 'deletedonly' => false,
192 'showsizediff' => false,
196 public function getParamDescription() {
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',
210 public function getDescription() {
211 return 'Returns a user contributions feed';
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' ),
222 public function getExamples() {
224 'api.php?action=feedcontributions&user=Reedy',
228 public function getVersion() {
229 return __CLASS__
. ': $Id$';