3 * Copyright © 2011 Sam Reed
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class ApiFeedContributions
extends ApiBase
{
29 * This module uses a custom feed wrapper printer.
31 * @return ApiFormatFeedWrapper
33 public function getCustomPrinter() {
34 return new ApiFormatFeedWrapper( $this->getMain() );
37 public function execute() {
38 $params = $this->extractRequestParams();
40 $config = $this->getConfig();
41 if ( !$config->get( 'Feed' ) ) {
42 $this->dieWithError( 'feed-unavailable' );
45 $feedClasses = $config->get( 'FeedClasses' );
46 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
47 $this->dieWithError( 'feed-invalid' );
50 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
51 $this->dieWithError( 'apierror-sizediffdisabled' );
54 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
55 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
56 ' [' . $config->get( 'LanguageCode' ) . ']';
57 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
59 $target = $params['user'] == 'newbies'
61 : Title
::makeTitleSafe( NS_USER
, $params['user'] )->getText();
63 $feed = new $feedClasses[$params['feedformat']] (
65 htmlspecialchars( $msg ),
69 // Convert year/month parameters to end parameter
70 $params['start'] = '';
72 $params = ContribsPager
::processDateFilter( $params );
74 $pager = new ContribsPager( $this->getContext(), [
76 'namespace' => $params['namespace'],
77 'start' => $params['start'],
78 'end' => $params['end'],
79 'tagFilter' => $params['tagfilter'],
80 'deletedOnly' => $params['deletedonly'],
81 'topOnly' => $params['toponly'],
82 'newOnly' => $params['newonly'],
83 'hideMinor' => $params['hideminor'],
84 'showSizeDiff' => $params['showsizediff'],
87 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
88 if ( $pager->getLimit() > $feedLimit ) {
89 $pager->setLimit( $feedLimit );
93 if ( $pager->getNumRows() > 0 ) {
95 $limit = $pager->getLimit();
96 foreach ( $pager->mResult
as $row ) {
97 // ContribsPager selects one more row for navigation, skip that row
98 if ( ++
$count > $limit ) {
101 $item = $this->feedItem( $row );
102 if ( $item !== null ) {
103 $feedItems[] = $item;
108 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
111 protected function feedItem( $row ) {
112 // This hook is the api contributions equivalent to the
113 // ContributionsLineEnding hook. Hook implementers may cancel
114 // the hook to signal the user is not allowed to read this item.
116 $hookResult = Hooks
::run(
117 'ApiFeedContributions::feedItem',
118 [ $row, $this->getContext(), &$feedItem ]
120 // Hook returned a valid feed item
121 if ( $feedItem instanceof FeedItem
) {
123 // Hook was canceled and did not return a valid feed item
124 } elseif ( !$hookResult ) {
128 // Hook completed and did not return a valid feed item
129 $title = Title
::makeTitle( intval( $row->page_namespace
), $row->page_title
);
130 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
131 $date = $row->rev_timestamp
;
132 $comments = $title->getTalkPage()->getFullURL();
133 $revision = Revision
::newFromRow( $row );
136 $title->getPrefixedText(),
137 $this->feedItemDesc( $revision ),
138 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
140 $this->feedItemAuthor( $revision ),
149 * @param Revision $revision
152 protected function feedItemAuthor( $revision ) {
153 return $revision->getUserText();
157 * @param Revision $revision
160 protected function feedItemDesc( $revision ) {
162 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
163 $content = $revision->getContent();
165 if ( $content instanceof TextContent
) {
166 // only textual content has a "source view".
167 $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
169 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
170 // contain JS magic and generally may not be suitable for inclusion in a feed.
171 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
172 // Compare also FeedUtils::formatDiffRow.
176 return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
177 htmlspecialchars( FeedItem
::stripComment( $revision->getComment() ) ) .
178 "</p>\n<hr />\n<div>" . $html . '</div>';
184 public function getAllowedParams() {
185 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
189 ApiBase
::PARAM_DFLT
=> 'rss',
190 ApiBase
::PARAM_TYPE
=> $feedFormatNames
193 ApiBase
::PARAM_TYPE
=> 'user',
194 ApiBase
::PARAM_REQUIRED
=> true,
197 ApiBase
::PARAM_TYPE
=> 'namespace'
200 ApiBase
::PARAM_TYPE
=> 'integer'
203 ApiBase
::PARAM_TYPE
=> 'integer'
206 ApiBase
::PARAM_ISMULTI
=> true,
207 ApiBase
::PARAM_TYPE
=> array_values( ChangeTags
::listDefinedTags() ),
208 ApiBase
::PARAM_DFLT
=> '',
210 'deletedonly' => false,
213 'hideminor' => false,
215 ApiBase
::PARAM_DFLT
=> false,
219 if ( $this->getConfig()->get( 'MiserMode' ) ) {
220 $ret['showsizediff'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
226 protected function getExamplesMessages() {
228 'action=feedcontributions&user=Example'
229 => 'apihelp-feedcontributions-example-simple',