Merge "jquery.tablesorter: Reset unaffected columns' sort counts when sorting"
[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, $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 $feedItems = array();
85 if ( $pager->getNumRows() > 0 ) {
86 foreach ( $pager->mResult as $row ) {
87 $feedItems[] = $this->feedItem( $row );
88 }
89 }
90
91 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
92 }
93
94 protected function feedItem( $row ) {
95 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
96 if ( $title && $title->userCan( 'read' ) ) {
97 $date = $row->rev_timestamp;
98 $comments = $title->getTalkPage()->getFullURL();
99 $revision = Revision::newFromRow( $row );
100
101 return new FeedItem(
102 $title->getPrefixedText(),
103 $this->feedItemDesc( $revision ),
104 $title->getFullURL(),
105 $date,
106 $this->feedItemAuthor( $revision ),
107 $comments
108 );
109 }
110 return null;
111 }
112
113 /**
114 * @param $revision Revision
115 * @return string
116 */
117 protected function feedItemAuthor( $revision ) {
118 return $revision->getUserText();
119 }
120
121 /**
122 * @param $revision Revision
123 * @return string
124 */
125 protected function feedItemDesc( $revision ) {
126 if ( $revision ) {
127 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
128 $content = $revision->getContent();
129
130 if ( $content instanceof TextContent ) {
131 // only textual content has a "source view".
132 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
133 } else {
134 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
135 // contain JS magic and generally may not be suitable for inclusion in a feed.
136 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
137 //Compare also FeedUtils::formatDiffRow.
138 $html = '';
139 }
140
141 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
142 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
143 "</p>\n<hr />\n<div>" . $html . "</div>";
144 }
145 return '';
146 }
147
148 public function getAllowedParams() {
149 global $wgFeedClasses;
150 $feedFormatNames = array_keys( $wgFeedClasses );
151 return array(
152 'feedformat' => array(
153 ApiBase::PARAM_DFLT => 'rss',
154 ApiBase::PARAM_TYPE => $feedFormatNames
155 ),
156 'user' => array(
157 ApiBase::PARAM_TYPE => 'user',
158 ApiBase::PARAM_REQUIRED => true,
159 ),
160 'namespace' => array(
161 ApiBase::PARAM_TYPE => 'namespace'
162 ),
163 'year' => array(
164 ApiBase::PARAM_TYPE => 'integer'
165 ),
166 'month' => array(
167 ApiBase::PARAM_TYPE => 'integer'
168 ),
169 'tagfilter' => array(
170 ApiBase::PARAM_ISMULTI => true,
171 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
172 ApiBase::PARAM_DFLT => '',
173 ),
174 'deletedonly' => false,
175 'toponly' => false,
176 'showsizediff' => false,
177 );
178 }
179
180 public function getParamDescription() {
181 return array(
182 'feedformat' => 'The format of the feed',
183 'user' => 'What users to get the contributions for',
184 'namespace' => 'What namespace to filter the contributions by',
185 'year' => 'From year (and earlier)',
186 'month' => 'From month (and earlier)',
187 'tagfilter' => 'Filter contributions that have these tags',
188 'deletedonly' => 'Show only deleted contributions',
189 'toponly' => 'Only show edits that are latest revisions',
190 'showsizediff' => 'Show the size difference between revisions. Disabled in Miser Mode',
191 );
192 }
193
194 public function getDescription() {
195 return 'Returns a user contributions feed';
196 }
197
198 public function getPossibleErrors() {
199 return array_merge( parent::getPossibleErrors(), array(
200 array( 'code' => 'feed-unavailable', 'info' => 'Syndication feeds are not available' ),
201 array( 'code' => 'feed-invalid', 'info' => 'Invalid subscription feed type' ),
202 array( 'code' => 'sizediffdisabled', 'info' => 'Size difference is disabled in Miser Mode' ),
203 ) );
204 }
205
206 public function getExamples() {
207 return array(
208 'api.php?action=feedcontributions&user=Reedy',
209 );
210 }
211 }