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 namespace MediaWiki\Api
;
25 use MediaWiki\Cache\LinkBatchFactory
;
26 use MediaWiki\CommentFormatter\CommentFormatter
;
27 use MediaWiki\Content\TextContent
;
28 use MediaWiki\Feed\FeedItem
;
29 use MediaWiki\HookContainer\HookContainer
;
30 use MediaWiki\Linker\LinkRenderer
;
31 use MediaWiki\MainConfigNames
;
32 use MediaWiki\MediaWikiServices
;
33 use MediaWiki\Pager\ContribsPager
;
34 use MediaWiki\ParamValidator\TypeDef\UserDef
;
35 use MediaWiki\Revision\RevisionAccessException
;
36 use MediaWiki\Revision\RevisionRecord
;
37 use MediaWiki\Revision\RevisionStore
;
38 use MediaWiki\Revision\SlotRecord
;
39 use MediaWiki\SpecialPage\SpecialPage
;
40 use MediaWiki\Title\NamespaceInfo
;
41 use MediaWiki\Title\Title
;
42 use MediaWiki\User\ExternalUserNames
;
43 use MediaWiki\User\UserFactory
;
44 use MediaWiki\User\UserRigorOptions
;
45 use Wikimedia\ParamValidator\ParamValidator
;
46 use Wikimedia\Rdbms\IConnectionProvider
;
51 class ApiFeedContributions
extends ApiBase
{
53 private RevisionStore
$revisionStore;
54 private LinkRenderer
$linkRenderer;
55 private LinkBatchFactory
$linkBatchFactory;
56 private HookContainer
$hookContainer;
57 private IConnectionProvider
$dbProvider;
58 private NamespaceInfo
$namespaceInfo;
59 private UserFactory
$userFactory;
60 private CommentFormatter
$commentFormatter;
61 private ApiHookRunner
$hookRunner;
63 public function __construct(
66 RevisionStore
$revisionStore,
67 LinkRenderer
$linkRenderer,
68 LinkBatchFactory
$linkBatchFactory,
69 HookContainer
$hookContainer,
70 IConnectionProvider
$dbProvider,
71 NamespaceInfo
$namespaceInfo,
72 UserFactory
$userFactory,
73 CommentFormatter
$commentFormatter
75 parent
::__construct( $main, $action );
76 $this->revisionStore
= $revisionStore;
77 $this->linkRenderer
= $linkRenderer;
78 $this->linkBatchFactory
= $linkBatchFactory;
79 $this->hookContainer
= $hookContainer;
80 $this->dbProvider
= $dbProvider;
81 $this->namespaceInfo
= $namespaceInfo;
82 $this->userFactory
= $userFactory;
83 $this->commentFormatter
= $commentFormatter;
85 $this->hookRunner
= new ApiHookRunner( $hookContainer );
89 * This module uses a custom feed wrapper printer.
91 * @return ApiFormatFeedWrapper
93 public function getCustomPrinter() {
94 return new ApiFormatFeedWrapper( $this->getMain() );
97 public function execute() {
98 $params = $this->extractRequestParams();
100 $config = $this->getConfig();
101 if ( !$config->get( MainConfigNames
::Feed
) ) {
102 $this->dieWithError( 'feed-unavailable' );
105 $feedClasses = $config->get( MainConfigNames
::FeedClasses
);
106 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
107 $this->dieWithError( 'feed-invalid' );
110 if ( $params['showsizediff'] && $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
111 $this->dieWithError( 'apierror-sizediffdisabled' );
114 $msg = $this->msg( 'Contributions' )->inContentLanguage()->text();
115 $feedTitle = $config->get( MainConfigNames
::Sitename
) . ' - ' . $msg .
116 ' [' . $config->get( MainConfigNames
::LanguageCode
) . ']';
118 $target = $params['user'];
119 if ( ExternalUserNames
::isExternal( $target ) ) {
120 // Interwiki names make invalid titles, so put the target in the query instead.
121 $feedUrl = SpecialPage
::getTitleFor( 'Contributions' )->getFullURL( [ 'target' => $target ] );
123 $feedUrl = SpecialPage
::getTitleFor( 'Contributions', $target )->getFullURL();
126 $feed = new $feedClasses[$params['feedformat']] (
128 htmlspecialchars( $msg ),
132 // Convert year/month parameters to end parameter
133 $params['start'] = '';
135 $params = ContribsPager
::processDateFilter( $params );
137 $targetUser = $this->userFactory
->newFromName( $target, UserRigorOptions
::RIGOR_NONE
);
139 $pager = new ContribsPager(
140 $this->getContext(), [
142 'namespace' => $params['namespace'],
143 'start' => $params['start'],
144 'end' => $params['end'],
145 'tagFilter' => $params['tagfilter'],
146 'deletedOnly' => $params['deletedonly'],
147 'topOnly' => $params['toponly'],
148 'newOnly' => $params['newonly'],
149 'hideMinor' => $params['hideminor'],
150 'showSizeDiff' => $params['showsizediff'],
153 $this->linkBatchFactory
,
154 $this->hookContainer
,
156 $this->revisionStore
,
157 $this->namespaceInfo
,
159 $this->commentFormatter
162 $feedLimit = $this->getConfig()->get( MainConfigNames
::FeedLimit
);
163 if ( $pager->getLimit() > $feedLimit ) {
164 $pager->setLimit( $feedLimit );
168 if ( $pager->getNumRows() > 0 ) {
170 $limit = $pager->getLimit();
171 foreach ( $pager->mResult
as $row ) {
172 // ContribsPager selects one more row for navigation, skip that row
173 if ( ++
$count > $limit ) {
176 $item = $this->feedItem( $row );
177 if ( $item !== null ) {
178 $feedItems[] = $item;
183 ApiFormatFeedWrapper
::setResult( $this->getResult(), $feed, $feedItems );
186 protected function feedItem( $row ) {
187 // This hook is the api contributions equivalent to the
188 // ContributionsLineEnding hook. Hook implementers may cancel
189 // the hook to signal the user is not allowed to read this item.
191 $hookResult = $this->hookRunner
->onApiFeedContributions__feedItem(
192 $row, $this->getContext(), $feedItem );
193 // Hook returned a valid feed item
194 if ( $feedItem instanceof FeedItem
) {
196 // Hook was canceled and did not return a valid feed item
197 } elseif ( !$hookResult ) {
201 // Hook completed and did not return a valid feed item
202 $title = Title
::makeTitle( (int)$row->page_namespace
, $row->page_title
);
204 if ( $title && $this->getAuthority()->authorizeRead( 'read', $title ) ) {
205 $date = $row->rev_timestamp
;
206 $comments = $title->getTalkPage()->getFullURL();
207 $revision = $this->revisionStore
->newRevisionFromRow( $row, 0, $title );
210 $title->getPrefixedText(),
211 $this->feedItemDesc( $revision ),
212 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
214 $this->feedItemAuthor( $revision ),
223 * @since 1.32, takes a RevisionRecord instead of a Revision
224 * @param RevisionRecord $revision
227 protected function feedItemAuthor( RevisionRecord
$revision ) {
228 $user = $revision->getUser();
229 return $user ?
$user->getName() : '';
233 * @since 1.32, takes a RevisionRecord instead of a Revision
234 * @param RevisionRecord $revision
237 protected function feedItemDesc( RevisionRecord
$revision ) {
238 $msg = $this->msg( 'colon-separator' )->inContentLanguage()->text();
240 $content = $revision->getContent( SlotRecord
::MAIN
);
241 } catch ( RevisionAccessException
$e ) {
245 if ( $content instanceof TextContent
) {
246 // only textual content has a "source view".
247 $html = nl2br( htmlspecialchars( $content->getText(), ENT_COMPAT
) );
249 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
250 // contain JS magic and generally may not be suitable for inclusion in a feed.
251 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
252 // Compare also MediaWiki\Feed\FeedUtils::formatDiffRow.
256 $comment = $revision->getComment();
258 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
259 htmlspecialchars( FeedItem
::stripComment( $comment->text ??
'' ) ) .
260 "</p>\n<hr />\n<div>" . $html . '</div>';
263 public function getAllowedParams() {
264 $feedFormatNames = array_keys( $this->getConfig()->get( MainConfigNames
::FeedClasses
) );
268 ParamValidator
::PARAM_DEFAULT
=> 'rss',
269 ParamValidator
::PARAM_TYPE
=> $feedFormatNames
272 ParamValidator
::PARAM_TYPE
=> 'user',
273 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'cidr', 'id', 'interwiki' ],
274 ParamValidator
::PARAM_REQUIRED
=> true,
277 ParamValidator
::PARAM_TYPE
=> 'namespace'
280 ParamValidator
::PARAM_TYPE
=> 'integer'
283 ParamValidator
::PARAM_TYPE
=> 'integer'
286 ParamValidator
::PARAM_ISMULTI
=> true,
287 ParamValidator
::PARAM_TYPE
=> array_values( MediaWikiServices
::getInstance()
288 ->getChangeTagsStore()->listDefinedTags()
290 ParamValidator
::PARAM_DEFAULT
=> '',
292 'deletedonly' => false,
295 'hideminor' => false,
297 ParamValidator
::PARAM_DEFAULT
=> false,
301 if ( $this->getConfig()->get( MainConfigNames
::MiserMode
) ) {
302 $ret['showsizediff'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
308 protected function getExamplesMessages() {
310 'action=feedcontributions&user=Example'
311 => 'apihelp-feedcontributions-example-simple',
315 public function getHelpUrls() {
316 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedcontributions';
320 /** @deprecated class alias since 1.43 */
321 class_alias( ApiFeedContributions
::class, 'ApiFeedContributions' );