Merge "Make sure images that don't have an explicit alignment get aligned right"
[mediawiki.git] / includes / api / ApiFeedContributions.php
blobafd5a1320c4610db8a743bbb2b39d830d0ba4203
1 <?php
2 /**
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiFeedContributions extends ApiBase {
32 /**
33 * This module uses a custom feed wrapper printer.
35 * @return ApiFormatFeedWrapper
37 public function getCustomPrinter() {
38 return new ApiFormatFeedWrapper( $this->getMain() );
41 public function execute() {
42 $params = $this->extractRequestParams();
44 global $wgFeed, $wgFeedClasses, $wgFeedLimit, $wgSitename, $wgLanguageCode;
46 if ( !$wgFeed ) {
47 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
50 if ( !isset( $wgFeedClasses[$params['feedformat']] ) ) {
51 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
54 global $wgMiserMode;
55 if ( $params['showsizediff'] && $wgMiserMode ) {
56 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
59 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
60 $feedTitle = $wgSitename . ' - ' . $msg . ' [' . $wgLanguageCode . ']';
61 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
63 $target = $params['user'] == 'newbies'
64 ? 'newbies'
65 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
67 $feed = new $wgFeedClasses[$params['feedformat']] (
68 $feedTitle,
69 htmlspecialchars( $msg ),
70 $feedUrl
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 'newOnly' => $params['newonly'],
82 'showSizeDiff' => $params['showsizediff'],
83 ) );
85 if ( $pager->getLimit() > $wgFeedLimit ) {
86 $pager->setLimit( $wgFeedLimit );
89 $feedItems = array();
90 if ( $pager->getNumRows() > 0 ) {
91 $count = 0;
92 $limit = $pager->getLimit();
93 foreach ( $pager->mResult as $row ) {
94 // ContribsPager selects one more row for navigation, skip that row
95 if ( ++$count > $limit ) {
96 break;
98 $feedItems[] = $this->feedItem( $row );
102 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
105 protected function feedItem( $row ) {
106 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
107 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
108 $date = $row->rev_timestamp;
109 $comments = $title->getTalkPage()->getFullURL();
110 $revision = Revision::newFromRow( $row );
112 return new FeedItem(
113 $title->getPrefixedText(),
114 $this->feedItemDesc( $revision ),
115 $title->getFullURL( array( 'diff' => $revision->getId() ) ),
116 $date,
117 $this->feedItemAuthor( $revision ),
118 $comments
122 return null;
126 * @param Revision $revision
127 * @return string
129 protected function feedItemAuthor( $revision ) {
130 return $revision->getUserText();
134 * @param Revision $revision
135 * @return string
137 protected function feedItemDesc( $revision ) {
138 if ( $revision ) {
139 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
140 $content = $revision->getContent();
142 if ( $content instanceof TextContent ) {
143 // only textual content has a "source view".
144 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
145 } else {
146 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
147 // contain JS magic and generally may not be suitable for inclusion in a feed.
148 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
149 //Compare also FeedUtils::formatDiffRow.
150 $html = '';
153 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
154 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
155 "</p>\n<hr />\n<div>" . $html . "</div>";
158 return '';
161 public function getAllowedParams() {
162 global $wgFeedClasses;
163 $feedFormatNames = array_keys( $wgFeedClasses );
165 return array(
166 'feedformat' => array(
167 ApiBase::PARAM_DFLT => 'rss',
168 ApiBase::PARAM_TYPE => $feedFormatNames
170 'user' => array(
171 ApiBase::PARAM_TYPE => 'user',
172 ApiBase::PARAM_REQUIRED => true,
174 'namespace' => array(
175 ApiBase::PARAM_TYPE => 'namespace'
177 'year' => array(
178 ApiBase::PARAM_TYPE => 'integer'
180 'month' => array(
181 ApiBase::PARAM_TYPE => 'integer'
183 'tagfilter' => array(
184 ApiBase::PARAM_ISMULTI => true,
185 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
186 ApiBase::PARAM_DFLT => '',
188 'deletedonly' => false,
189 'toponly' => false,
190 'newonly' => false,
191 'showsizediff' => false,
195 public function getParamDescription() {
196 return array(
197 'feedformat' => 'The format of the feed',
198 'user' => 'What users to get the contributions for',
199 'namespace' => 'What namespace to filter the contributions by',
200 'year' => 'From year (and earlier)',
201 'month' => 'From month (and earlier)',
202 'tagfilter' => 'Filter contributions that have these tags',
203 'deletedonly' => 'Show only deleted contributions',
204 'toponly' => 'Only show edits that are latest revisions',
205 'newonly' => 'Only show edits that are page creations',
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' ),
219 ) );
222 public function getExamples() {
223 return array(
224 'api.php?action=feedcontributions&user=Reedy',