Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiFeedContributions.php
blobedda67231f5adaab45579104e2887d151fdc3f94
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 $config = $this->getConfig();
45 if ( !$config->get( 'Feed' ) ) {
46 $this->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
49 $feedClasses = $config->get( 'FeedClasses' );
50 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
51 $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
54 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
55 $this->dieUsage( 'Size difference is disabled in Miser Mode', 'sizediffdisabled' );
58 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
59 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg . ' [' . $config->get( 'LanguageCode' ) . ']';
60 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
62 $target = $params['user'] == 'newbies'
63 ? 'newbies'
64 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
66 $feed = new $feedClasses[$params['feedformat']] (
67 $feedTitle,
68 htmlspecialchars( $msg ),
69 $feedUrl
72 $pager = new ContribsPager( $this->getContext(), array(
73 'target' => $target,
74 'namespace' => $params['namespace'],
75 'year' => $params['year'],
76 'month' => $params['month'],
77 'tagFilter' => $params['tagfilter'],
78 'deletedOnly' => $params['deletedonly'],
79 'topOnly' => $params['toponly'],
80 'newOnly' => $params['newonly'],
81 'showSizeDiff' => $params['showsizediff'],
82 ) );
84 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
85 if ( $pager->getLimit() > $feedLimit ) {
86 $pager->setLimit( $feedLimit );
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 $item = $this->feedItem( $row );
99 if ( $item !== null ) {
100 $feedItems[] = $item;
105 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
108 protected function feedItem( $row ) {
109 // This hook is the api contributions equivalent to the
110 // ContributionsLineEnding hook. Hook implementers may cancel
111 // the hook to signal the user is not allowed to read this item.
112 $feedItem = null;
113 $hookResult = Hooks::run(
114 'ApiFeedContributions::feedItem',
115 array( $row, $this->getContext(), &$feedItem )
117 // Hook returned a valid feed item
118 if ( $feedItem instanceof FeedItem ) {
119 return $feedItem;
120 // Hook was canceled and did not return a valid feed item
121 } elseif ( !$hookResult ) {
122 return null;
125 // Hook completed and did not return a valid feed item
126 $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
127 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
128 $date = $row->rev_timestamp;
129 $comments = $title->getTalkPage()->getFullURL();
130 $revision = Revision::newFromRow( $row );
132 return new FeedItem(
133 $title->getPrefixedText(),
134 $this->feedItemDesc( $revision ),
135 $title->getFullURL( array( 'diff' => $revision->getId() ) ),
136 $date,
137 $this->feedItemAuthor( $revision ),
138 $comments
142 return null;
146 * @param Revision $revision
147 * @return string
149 protected function feedItemAuthor( $revision ) {
150 return $revision->getUserText();
154 * @param Revision $revision
155 * @return string
157 protected function feedItemDesc( $revision ) {
158 if ( $revision ) {
159 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
160 $content = $revision->getContent();
162 if ( $content instanceof TextContent ) {
163 // only textual content has a "source view".
164 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
165 } else {
166 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
167 // contain JS magic and generally may not be suitable for inclusion in a feed.
168 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
169 //Compare also FeedUtils::formatDiffRow.
170 $html = '';
173 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
174 htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
175 "</p>\n<hr />\n<div>" . $html . "</div>";
178 return '';
181 public function getAllowedParams() {
182 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
184 $ret = array(
185 'feedformat' => array(
186 ApiBase::PARAM_DFLT => 'rss',
187 ApiBase::PARAM_TYPE => $feedFormatNames
189 'user' => array(
190 ApiBase::PARAM_TYPE => 'user',
191 ApiBase::PARAM_REQUIRED => true,
193 'namespace' => array(
194 ApiBase::PARAM_TYPE => 'namespace'
196 'year' => array(
197 ApiBase::PARAM_TYPE => 'integer'
199 'month' => array(
200 ApiBase::PARAM_TYPE => 'integer'
202 'tagfilter' => array(
203 ApiBase::PARAM_ISMULTI => true,
204 ApiBase::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
205 ApiBase::PARAM_DFLT => '',
207 'deletedonly' => false,
208 'toponly' => false,
209 'newonly' => false,
210 'showsizediff' => array(
211 ApiBase::PARAM_DFLT => false,
215 if ( $this->getConfig()->get( 'MiserMode' ) ) {
216 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
219 return $ret;
222 protected function getExamplesMessages() {
223 return array(
224 'action=feedcontributions&user=Example'
225 => 'apihelp-feedcontributions-example-simple',