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
{
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->dieWithError( 'feed-unavailable' );
49 $feedClasses = $config->get( 'FeedClasses' );
50 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
51 $this->dieWithError( 'feed-invalid' );
54 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
55 $this->dieWithError( 'apierror-sizediffdisabled' );
58 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
59 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
60 ' [' . $config->get( 'LanguageCode' ) . ']';
61 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
63 $target = $params['user'] == 'newbies'
65 : Title
::makeTitleSafe( NS_USER
, $params['user'] )->getText();
67 $feed = new $feedClasses[$params['feedformat']] (
69 htmlspecialchars( $msg ),
73 // Convert year/month parameters to end parameter
74 $params['start'] = '';
76 $params = ContribsPager
::processDateFilter( $params );
78 $pager = new ContribsPager( $this->getContext(), [
80 'namespace' => $params['namespace'],
81 'start' => $params['start'],
82 'end' => $params['end'],
83 'tagFilter' => $params['tagfilter'],
84 'deletedOnly' => $params['deletedonly'],
85 'topOnly' => $params['toponly'],
86 'newOnly' => $params['newonly'],
87 'hideMinor' => $params['hideminor'],
88 'showSizeDiff' => $params['showsizediff'],
91 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
92 if ( $pager->getLimit() > $feedLimit ) {
93 $pager->setLimit( $feedLimit );
97 if ( $pager->getNumRows() > 0 ) {
99 $limit = $pager->getLimit();
100 foreach ( $pager->mResult
as $row ) {
101 // ContribsPager selects one more row for navigation, skip that row
102 if ( ++
$count > $limit ) {
105 $item = $this->feedItem( $row );
106 if ( $item !== null ) {
107 $feedItems[] = $item;
112 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
115 protected function feedItem( $row ) {
116 // This hook is the api contributions equivalent to the
117 // ContributionsLineEnding hook. Hook implementers may cancel
118 // the hook to signal the user is not allowed to read this item.
120 $hookResult = Hooks
::run(
121 'ApiFeedContributions::feedItem',
122 [ $row, $this->getContext(), &$feedItem ]
124 // Hook returned a valid feed item
125 if ( $feedItem instanceof FeedItem
) {
127 // Hook was canceled and did not return a valid feed item
128 } elseif ( !$hookResult ) {
132 // Hook completed and did not return a valid feed item
133 $title = Title
::makeTitle( intval( $row->page_namespace
), $row->page_title
);
134 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
135 $date = $row->rev_timestamp
;
136 $comments = $title->getTalkPage()->getFullURL();
137 $revision = Revision
::newFromRow( $row );
140 $title->getPrefixedText(),
141 $this->feedItemDesc( $revision ),
142 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
144 $this->feedItemAuthor( $revision ),
153 * @param Revision $revision
156 protected function feedItemAuthor( $revision ) {
157 return $revision->getUserText();
161 * @param Revision $revision
164 protected function feedItemDesc( $revision ) {
166 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
167 $content = $revision->getContent();
169 if ( $content instanceof TextContent
) {
170 // only textual content has a "source view".
171 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
173 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
174 // contain JS magic and generally may not be suitable for inclusion in a feed.
175 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
176 // Compare also FeedUtils::formatDiffRow.
180 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
181 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
182 "</p>\n<hr />\n<div>" . $html . '</div>';
188 public function getAllowedParams() {
189 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
193 ApiBase
::PARAM_DFLT
=> 'rss',
194 ApiBase
::PARAM_TYPE
=> $feedFormatNames
197 ApiBase
::PARAM_TYPE
=> 'user',
198 ApiBase
::PARAM_REQUIRED
=> true,
201 ApiBase
::PARAM_TYPE
=> 'namespace'
204 ApiBase
::PARAM_TYPE
=> 'integer'
207 ApiBase
::PARAM_TYPE
=> 'integer'
210 ApiBase
::PARAM_ISMULTI
=> true,
211 ApiBase
::PARAM_TYPE
=> array_values( ChangeTags
::listDefinedTags() ),
212 ApiBase
::PARAM_DFLT
=> '',
214 'deletedonly' => false,
217 'hideminor' => false,
219 ApiBase
::PARAM_DFLT
=> false,
223 if ( $this->getConfig()->get( 'MiserMode' ) ) {
224 $ret['showsizediff'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
230 protected function getExamplesMessages() {
232 'action=feedcontributions&user=Example'
233 => 'apihelp-feedcontributions-example-simple',