Merge "Fix separated login link so that create account and login are always next...
[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 $params = $this->extractRequestParams();
47
48 global $wgFeed, $wgFeedClasses, $wgSitename, $wgLanguageCode;
49
50 if( !$wgFeed ) {
51 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
52 }
53
54 if( !isset( $wgFeedClasses[ $params['feedformat'] ] ) ) {
55 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
56 }
57
58 global $wgMiserMode;
59 if ( $params['showsizediff'] && $wgMiserMode ) {
60 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
61 }
62
63 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
64 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
65 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
66
67 $target = $params['user'] == 'newbies'
68 ? 'newbies'
69 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
70
71 $feed = new $wgFeedClasses[$params['feedformat']] (
72 $feedTitle,
73 htmlspecialchars( $msg ),
74 $feedUrl
75 );
76
77 $pager = new ContribsPager( $this->getContext(), array(
78 'target' => $target,
79 'namespace' => $params['namespace'],
80 'year' => $params['year'],
81 'month' => $params['month'],
82 'tagFilter' => $params['tagfilter'],
83 'deletedOnly' => $params['deletedonly'],
84 'topOnly' => $params['toponly'],
85 'showSizeDiff' => $params['showsizediff'],
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 ) {
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 } else {
114 return null;
115 }
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 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
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 ),
156 'year' => array(
157 ApiBase::PARAM_TYPE => 'integer'
158 ),
159 'month' => array(
160 ApiBase::PARAM_TYPE => 'integer'
161 ),
162 'tagfilter' => array(
163 ApiBase::PARAM_ISMULTI => true,
164 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
165 ApiBase::PARAM_DFLT => '',
166 ),
167 'deletedonly' => false,
168 'toponly' => false,
169 'showsizediff' => false,
170 );
171 }
172
173 public function getParamDescription() {
174 return array(
175 'feedformat' => 'The format of the feed',
176 'user' => 'What users to get the contributions for',
177 'namespace' => 'What namespace to filter the contributions by',
178 'year' => 'From year (and earlier)',
179 'month' => 'From month (and earlier)',
180 'tagfilter' => 'Filter contributions that have these tags',
181 'deletedonly' => 'Show only deleted contributions',
182 'toponly' => 'Only show edits that are latest revisions',
183 'showsizediff' => 'Show the size difference between revisions. Disabled in Miser Mode',
184 );
185 }
186
187 public function getDescription() {
188 return 'Returns a user contributions feed';
189 }
190
191 public function getPossibleErrors() {
192 return array_merge( parent::getPossibleErrors(), array(
193 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
194 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
195 array( 'code' => 'sizediffdisabled', 'info' => 'Size difference is disabled in Miser Mode' ),
196 ) );
197 }
198
199 public function getExamples() {
200 return array(
201 'api.php?action=feedcontributions&user=Reedy',
202 );
203 }
204
205 public function getVersion() {
206 return __CLASS__ . ': $Id$';
207 }
208 }