MediaWiki master
ApiFeedContributions.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
20use MediaWiki\Pager\ContribsPager;
32use stdClass;
35
40
41 private ApiHookRunner $hookRunner;
42
43 public function __construct(
44 ApiMain $main,
45 string $action,
46 private readonly RevisionStore $revisionStore,
47 private readonly LinkRenderer $linkRenderer,
48 private readonly LinkBatchFactory $linkBatchFactory,
49 private readonly HookContainer $hookContainer,
50 private readonly IConnectionProvider $dbProvider,
51 private readonly NamespaceInfo $namespaceInfo,
52 private readonly UserFactory $userFactory,
53 private readonly CommentFormatter $commentFormatter,
54 ) {
55 parent::__construct( $main, $action );
56
57 $this->hookRunner = new ApiHookRunner( $hookContainer );
58 }
59
65 public function getCustomPrinter() {
66 return new ApiFormatFeedWrapper( $this->getMain() );
67 }
68
69 public function execute() {
70 $params = $this->extractRequestParams();
71
72 $config = $this->getConfig();
73 if ( !$config->get( MainConfigNames::Feed ) ) {
74 $this->dieWithError( 'feed-unavailable' );
75 }
76
77 $feedClasses = $config->get( MainConfigNames::FeedClasses );
78 '@phan-var array<string,class-string<ChannelFeed>> $feedClasses';
79 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
80 $this->dieWithError( 'feed-invalid' );
81 }
82
83 if ( $params['showsizediff'] && $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
84 $this->dieWithError( 'apierror-sizediffdisabled' );
85 }
86
87 $msg = $this->msg( 'Contributions' )->inContentLanguage()->escaped();
88 $feedTitle = $config->get( MainConfigNames::Sitename ) . ' - ' . $msg .
89 ' [' . $config->get( MainConfigNames::LanguageCode ) . ']';
90
91 $target = $params['user'];
92 if ( ExternalUserNames::isExternal( $target ) ) {
93 // Interwiki names make invalid titles, so put the target in the query instead.
94 $feedUrl = SpecialPage::getTitleFor( 'Contributions' )->getFullURL( [ 'target' => $target ] );
95 } else {
96 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $target )->getFullURL();
97 }
98
99 $feed = new $feedClasses[$params['feedformat']](
100 $feedTitle,
101 $msg,
102 $feedUrl
103 );
104
105 // Convert year/month parameters to end parameter
106 $params['start'] = '';
107 $params['end'] = '';
108 $params = ContribsPager::processDateFilter( $params );
109
110 $targetUser = $this->userFactory->newFromName( $target, UserRigorOptions::RIGOR_NONE );
111
112 $pager = new ContribsPager(
113 $this->getContext(), [
114 'target' => $target,
115 'namespace' => $params['namespace'],
116 'start' => $params['start'],
117 'end' => $params['end'],
118 'tagFilter' => $params['tagfilter'],
119 'deletedOnly' => $params['deletedonly'],
120 'topOnly' => $params['toponly'],
121 'newOnly' => $params['newonly'],
122 'hideMinor' => $params['hideminor'],
123 'showSizeDiff' => $params['showsizediff'],
124 ],
125 $this->linkRenderer,
126 $this->linkBatchFactory,
127 $this->hookContainer,
128 $this->dbProvider,
129 $this->revisionStore,
130 $this->namespaceInfo,
131 $targetUser,
132 $this->commentFormatter
133 );
134
135 $feedLimit = $this->getConfig()->get( MainConfigNames::FeedLimit );
136 if ( $pager->getLimit() > $feedLimit ) {
137 $pager->setLimit( $feedLimit );
138 }
139
140 $feedItems = [];
141 if ( $pager->getNumRows() > 0 ) {
142 $count = 0;
143 $limit = $pager->getLimit();
144 foreach ( $pager->mResult as $row ) {
145 // ContribsPager selects one more row for navigation, skip that row
146 if ( ++$count > $limit ) {
147 break;
148 }
149 $item = $this->feedItem( $row );
150 if ( $item !== null ) {
151 $feedItems[] = $item;
152 }
153 }
154 }
155
156 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
157 }
158
163 protected function feedItem( $row ): ?FeedItem {
164 // This hook is the api contributions equivalent to the
165 // ContributionsLineEnding hook. Hook implementers may cancel
166 // the hook to signal the user is not allowed to read this item.
167 $feedItem = null;
168 $hookResult = $this->hookRunner->onApiFeedContributions__feedItem(
169 $row, $this->getContext(), $feedItem );
170 // Hook returned a valid feed item
171 if ( $feedItem instanceof FeedItem ) {
172 return $feedItem;
173 // Hook was canceled and did not return a valid feed item
174 } elseif ( !$hookResult ) {
175 return null;
176 }
177
178 // Hook completed and did not return a valid feed item
179 $title = Title::makeTitle( (int)$row->page_namespace, $row->page_title );
180
181 if ( $this->getAuthority()->authorizeRead( 'read', $title ) ) {
182 $date = $row->rev_timestamp;
183 $comments = $title->getTalkPage()->getFullURL();
184 $revision = $this->revisionStore->newRevisionFromRow( $row, 0, $title );
185
186 return new FeedItem(
187 $title->getPrefixedText(),
188 $this->feedItemDesc( $revision ),
189 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
190 $date,
191 $this->feedItemAuthor( $revision ),
192 $comments
193 );
194 }
195
196 return null;
197 }
198
204 protected function feedItemAuthor( RevisionRecord $revision ) {
205 $user = $revision->getUser();
206 return $user ? $user->getName() : '';
207 }
208
214 protected function feedItemDesc( RevisionRecord $revision ) {
215 $msg = $this->msg( 'colon-separator' )->inContentLanguage()->escaped();
216 try {
217 $content = $revision->getContent( SlotRecord::MAIN );
218 } catch ( RevisionAccessException ) {
219 $content = null;
220 }
221
222 if ( $content instanceof TextContent ) {
223 // only textual content has a "source view".
224 $html = nl2br( htmlspecialchars( $content->getText(), ENT_COMPAT ) );
225 } else {
226 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
227 // contain JS magic and generally may not be suitable for inclusion in a feed.
228 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
229 // Compare also MediaWiki\Feed\FeedUtils::formatDiffRow.
230 $html = '';
231 }
232
233 $comment = $revision->getComment();
234
235 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
236 htmlspecialchars( FeedItem::stripComment( $comment->text ?? '' ) ) .
237 "</p>\n<hr />\n<div>" . $html . '</div>';
238 }
239
241 public function getAllowedParams() {
242 $feedFormatNames = array_keys( $this->getConfig()->get( MainConfigNames::FeedClasses ) );
243
244 $ret = [
245 'feedformat' => [
246 ParamValidator::PARAM_DEFAULT => 'rss',
247 ParamValidator::PARAM_TYPE => $feedFormatNames
248 ],
249 'user' => [
250 ParamValidator::PARAM_TYPE => 'user',
251 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr', 'id', 'interwiki' ],
252 ParamValidator::PARAM_REQUIRED => true,
253 ],
254 'namespace' => [
255 ParamValidator::PARAM_TYPE => 'namespace'
256 ],
257 'year' => [
258 ParamValidator::PARAM_TYPE => 'integer'
259 ],
260 'month' => [
261 ParamValidator::PARAM_TYPE => 'integer'
262 ],
263 'tagfilter' => [
264 ParamValidator::PARAM_ISMULTI => true,
265 ParamValidator::PARAM_TYPE => array_values( MediaWikiServices::getInstance()
266 ->getChangeTagsStore()->listDefinedTags()
267 ),
268 ParamValidator::PARAM_DEFAULT => '',
269 ],
270 'deletedonly' => false,
271 'toponly' => false,
272 'newonly' => false,
273 'hideminor' => false,
274 'showsizediff' => [
275 ParamValidator::PARAM_DEFAULT => false,
276 ],
277 ];
278
279 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
280 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
281 }
282
283 return $ret;
284 }
285
287 protected function getExamplesMessages() {
288 return [
289 'action=feedcontributions&user=Example'
290 => 'apihelp-feedcontributions-example-simple',
291 ];
292 }
293
295 public function getHelpUrls() {
296 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedcontributions';
297 }
298}
299
301class_alias( ApiFeedContributions::class, 'ApiFeedContributions' );
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1506
getMain()
Get the main module.
Definition ApiBase.php:560
getResult()
Get the result object.
Definition ApiBase.php:681
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:822
feedItem( $row)
TODO: use stdClass type hint without T398925.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getCustomPrinter()
This module uses a custom feed wrapper printer.
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
__construct(ApiMain $main, string $action, private readonly RevisionStore $revisionStore, private readonly LinkRenderer $linkRenderer, private readonly LinkBatchFactory $linkBatchFactory, private readonly HookContainer $hookContainer, private readonly IConnectionProvider $dbProvider, private readonly NamespaceInfo $namespaceInfo, private readonly UserFactory $userFactory, private readonly CommentFormatter $commentFormatter,)
This printer is used to wrap an instance of the Feed class.
static setResult( $result, $feed, $feedItems)
Call this method to initialize output data.
This class provides an implementation of the hook interfaces used by the core Action API,...
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
This is the main service interface for converting single-line comments from various DB comment fields...
Content object implementation for representing flat text.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Class to support the outputting of syndication feeds in Atom and RSS format.
A base class for outputting syndication feeds (e.g.
Definition FeedItem.php:26
Class that generates HTML for internal links.
A class containing constants representing the names of configuration variables.
const FeedClasses
Name constant for the FeedClasses setting, for use with Config::get()
const Feed
Name constant for the Feed setting, for use with Config::get()
const Sitename
Name constant for the Sitename setting, for use with Config::get()
const LanguageCode
Name constant for the LanguageCode setting, for use with Config::get()
const MiserMode
Name constant for the MiserMode setting, for use with Config::get()
const FeedLimit
Name constant for the FeedLimit setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Factory for LinkBatch objects to batch query page metadata.
Type definition for user types.
Definition UserDef.php:27
Exception representing a failure to look up a revision.
Page revision base class.
getContent( $role, $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Returns the Content of the given slot of this revision.
getUser( $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Fetch revision's author's user identity, if it's available to the specified audience.
getComment( $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Fetch revision comment, if it's available to the specified audience.
Service for looking up page revisions.
Value object representing a content slot associated with a page revision.
Parent class for all special pages.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Represents a title within MediaWiki.
Definition Title.php:69
Class to parse and build external user names.
Create User objects.
Service for formatting and validating API parameters.
Shared interface for rigor levels when dealing with User methods.
Provide primary and replica IDatabase connections.