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
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Revision\RevisionAccessException
;
25 use MediaWiki\Revision\RevisionRecord
;
26 use MediaWiki\Revision\RevisionStore
;
27 use MediaWiki\Revision\SlotRecord
;
32 class ApiFeedContributions
extends ApiBase
{
34 /** @var RevisionStore */
35 private $revisionStore;
37 /** @var TitleParser */
41 * This module uses a custom feed wrapper printer.
43 * @return ApiFormatFeedWrapper
45 public function getCustomPrinter() {
46 return new ApiFormatFeedWrapper( $this->getMain() );
49 public function execute() {
50 $this->revisionStore
= MediaWikiServices
::getInstance()->getRevisionStore();
51 $this->titleParser
= MediaWikiServices
::getInstance()->getTitleParser();
53 $params = $this->extractRequestParams();
55 $config = $this->getConfig();
56 if ( !$config->get( 'Feed' ) ) {
57 $this->dieWithError( 'feed-unavailable' );
60 $feedClasses = $config->get( 'FeedClasses' );
61 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
62 $this->dieWithError( 'feed-invalid' );
65 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
66 $this->dieWithError( 'apierror-sizediffdisabled' );
69 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
70 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
71 ' [' . $config->get( 'LanguageCode' ) . ']';
72 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
75 $target = $this->titleParser
76 ->parseTitle( $params['user'], NS_USER
)
78 } catch ( MalformedTitleException
$e ) {
80 [ 'apierror-baduser', 'user', wfEscapeWikiText( $params['user'] ) ],
81 'baduser_' . $this->encodeParamName( 'user' )
85 $feed = new $feedClasses[$params['feedformat']] (
87 htmlspecialchars( $msg ),
91 // Convert year/month parameters to end parameter
92 $params['start'] = '';
94 $params = ContribsPager
::processDateFilter( $params );
96 $pager = new ContribsPager( $this->getContext(), [
98 'namespace' => $params['namespace'],
99 'start' => $params['start'],
100 'end' => $params['end'],
101 'tagFilter' => $params['tagfilter'],
102 'deletedOnly' => $params['deletedonly'],
103 'topOnly' => $params['toponly'],
104 'newOnly' => $params['newonly'],
105 'hideMinor' => $params['hideminor'],
106 'showSizeDiff' => $params['showsizediff'],
109 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
110 if ( $pager->getLimit() > $feedLimit ) {
111 $pager->setLimit( $feedLimit );
115 if ( $pager->getNumRows() > 0 ) {
117 $limit = $pager->getLimit();
118 foreach ( $pager->mResult
as $row ) {
119 // ContribsPager selects one more row for navigation, skip that row
120 if ( ++
$count > $limit ) {
123 $item = $this->feedItem( $row );
124 if ( $item !== null ) {
125 $feedItems[] = $item;
130 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
133 protected function feedItem( $row ) {
134 // This hook is the api contributions equivalent to the
135 // ContributionsLineEnding hook. Hook implementers may cancel
136 // the hook to signal the user is not allowed to read this item.
138 $hookResult = Hooks
::run(
139 'ApiFeedContributions::feedItem',
140 [ $row, $this->getContext(), &$feedItem ]
142 // Hook returned a valid feed item
143 if ( $feedItem instanceof FeedItem
) {
145 // Hook was canceled and did not return a valid feed item
146 } elseif ( !$hookResult ) {
150 // Hook completed and did not return a valid feed item
151 $title = Title
::makeTitle( (int)$row->page_namespace
, $row->page_title
);
152 $user = $this->getUser();
154 if ( $title && $this->getPermissionManager()->userCan( 'read', $user, $title ) ) {
155 $date = $row->rev_timestamp
;
156 $comments = $title->getTalkPage()->getFullURL();
157 $revision = $this->revisionStore
->newRevisionFromRow( $row );
160 $title->getPrefixedText(),
161 $this->feedItemDesc( $revision ),
162 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
164 $this->feedItemAuthor( $revision ),
173 * @since 1.32, takes a RevisionRecord instead of a Revision
174 * @param RevisionRecord $revision
177 protected function feedItemAuthor( RevisionRecord
$revision ) {
178 $user = $revision->getUser();
179 return $user ?
$user->getName() : '';
183 * @since 1.32, takes a RevisionRecord instead of a Revision
184 * @param RevisionRecord $revision
187 protected function feedItemDesc( RevisionRecord
$revision ) {
188 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
190 $content = $revision->getContent( SlotRecord
::MAIN
);
191 } catch ( RevisionAccessException
$e ) {
195 if ( $content instanceof TextContent
) {
196 // only textual content has a "source view".
197 $html = nl2br( htmlspecialchars( $content->getText() ) );
199 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
200 // contain JS magic and generally may not be suitable for inclusion in a feed.
201 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
202 // Compare also FeedUtils::formatDiffRow.
206 $comment = $revision->getComment();
208 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
209 htmlspecialchars( FeedItem
::stripComment( $comment ?
$comment->text
: '' ) ) .
210 "</p>\n<hr />\n<div>" . $html . '</div>';
213 public function getAllowedParams() {
214 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
218 ApiBase
::PARAM_DFLT
=> 'rss',
219 ApiBase
::PARAM_TYPE
=> $feedFormatNames
222 ApiBase
::PARAM_TYPE
=> 'user',
223 ApiBase
::PARAM_REQUIRED
=> true,
226 ApiBase
::PARAM_TYPE
=> 'namespace'
229 ApiBase
::PARAM_TYPE
=> 'integer'
232 ApiBase
::PARAM_TYPE
=> 'integer'
235 ApiBase
::PARAM_ISMULTI
=> true,
236 ApiBase
::PARAM_TYPE
=> array_values( ChangeTags
::listDefinedTags() ),
237 ApiBase
::PARAM_DFLT
=> '',
239 'deletedonly' => false,
242 'hideminor' => false,
244 ApiBase
::PARAM_DFLT
=> false,
248 if ( $this->getConfig()->get( 'MiserMode' ) ) {
249 $ret['showsizediff'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
255 protected function getExamplesMessages() {
257 'action=feedcontributions&user=Example'
258 => 'apihelp-feedcontributions-example-simple',