MediaWiki master
ApiFeedContributions.php
Go to the documentation of this file.
1<?php
45
50
51 private RevisionStore $revisionStore;
52 private LinkRenderer $linkRenderer;
53 private LinkBatchFactory $linkBatchFactory;
54 private HookContainer $hookContainer;
55 private IConnectionProvider $dbProvider;
56 private NamespaceInfo $namespaceInfo;
57 private UserFactory $userFactory;
58 private CommentFormatter $commentFormatter;
59 private ApiHookRunner $hookRunner;
60
73 public function __construct(
74 ApiMain $main,
75 $action,
76 RevisionStore $revisionStore,
77 LinkRenderer $linkRenderer,
78 LinkBatchFactory $linkBatchFactory,
79 HookContainer $hookContainer,
80 IConnectionProvider $dbProvider,
81 NamespaceInfo $namespaceInfo,
82 UserFactory $userFactory,
83 CommentFormatter $commentFormatter
84 ) {
85 parent::__construct( $main, $action );
86 $this->revisionStore = $revisionStore;
87 $this->linkRenderer = $linkRenderer;
88 $this->linkBatchFactory = $linkBatchFactory;
89 $this->hookContainer = $hookContainer;
90 $this->dbProvider = $dbProvider;
91 $this->namespaceInfo = $namespaceInfo;
92 $this->userFactory = $userFactory;
93 $this->commentFormatter = $commentFormatter;
94
95 $this->hookRunner = new ApiHookRunner( $hookContainer );
96 }
97
103 public function getCustomPrinter() {
104 return new ApiFormatFeedWrapper( $this->getMain() );
105 }
106
107 public function execute() {
108 $params = $this->extractRequestParams();
109
110 $config = $this->getConfig();
111 if ( !$config->get( MainConfigNames::Feed ) ) {
112 $this->dieWithError( 'feed-unavailable' );
113 }
114
115 $feedClasses = $config->get( MainConfigNames::FeedClasses );
116 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
117 $this->dieWithError( 'feed-invalid' );
118 }
119
120 if ( $params['showsizediff'] && $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
121 $this->dieWithError( 'apierror-sizediffdisabled' );
122 }
123
124 $msg = $this->msg( 'Contributions' )->inContentLanguage()->text();
125 $feedTitle = $config->get( MainConfigNames::Sitename ) . ' - ' . $msg .
126 ' [' . $config->get( MainConfigNames::LanguageCode ) . ']';
127
128 $target = $params['user'];
129 if ( ExternalUserNames::isExternal( $target ) ) {
130 // Interwiki names make invalid titles, so put the target in the query instead.
131 $feedUrl = SpecialPage::getTitleFor( 'Contributions' )->getFullURL( [ 'target' => $target ] );
132 } else {
133 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $target )->getFullURL();
134 }
135
136 $feed = new $feedClasses[$params['feedformat']] (
137 $feedTitle,
138 htmlspecialchars( $msg ),
139 $feedUrl
140 );
141
142 // Convert year/month parameters to end parameter
143 $params['start'] = '';
144 $params['end'] = '';
145 $params = ContribsPager::processDateFilter( $params );
146
147 $targetUser = $this->userFactory->newFromName( $target, UserRigorOptions::RIGOR_NONE );
148
149 $pager = new ContribsPager(
150 $this->getContext(), [
151 'target' => $target,
152 'namespace' => $params['namespace'],
153 'start' => $params['start'],
154 'end' => $params['end'],
155 'tagFilter' => $params['tagfilter'],
156 'deletedOnly' => $params['deletedonly'],
157 'topOnly' => $params['toponly'],
158 'newOnly' => $params['newonly'],
159 'hideMinor' => $params['hideminor'],
160 'showSizeDiff' => $params['showsizediff'],
161 ],
162 $this->linkRenderer,
163 $this->linkBatchFactory,
164 $this->hookContainer,
165 $this->dbProvider,
166 $this->revisionStore,
167 $this->namespaceInfo,
168 $targetUser,
169 $this->commentFormatter
170 );
171
172 $feedLimit = $this->getConfig()->get( MainConfigNames::FeedLimit );
173 if ( $pager->getLimit() > $feedLimit ) {
174 $pager->setLimit( $feedLimit );
175 }
176
177 $feedItems = [];
178 if ( $pager->getNumRows() > 0 ) {
179 $count = 0;
180 $limit = $pager->getLimit();
181 foreach ( $pager->mResult as $row ) {
182 // ContribsPager selects one more row for navigation, skip that row
183 if ( ++$count > $limit ) {
184 break;
185 }
186 $item = $this->feedItem( $row );
187 if ( $item !== null ) {
188 $feedItems[] = $item;
189 }
190 }
191 }
192
193 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
194 }
195
196 protected function feedItem( $row ) {
197 // This hook is the api contributions equivalent to the
198 // ContributionsLineEnding hook. Hook implementers may cancel
199 // the hook to signal the user is not allowed to read this item.
200 $feedItem = null;
201 $hookResult = $this->hookRunner->onApiFeedContributions__feedItem(
202 $row, $this->getContext(), $feedItem );
203 // Hook returned a valid feed item
204 if ( $feedItem instanceof FeedItem ) {
205 return $feedItem;
206 // Hook was canceled and did not return a valid feed item
207 } elseif ( !$hookResult ) {
208 return null;
209 }
210
211 // Hook completed and did not return a valid feed item
212 $title = Title::makeTitle( (int)$row->page_namespace, $row->page_title );
213
214 if ( $title && $this->getAuthority()->authorizeRead( 'read', $title ) ) {
215 $date = $row->rev_timestamp;
216 $comments = $title->getTalkPage()->getFullURL();
217 $revision = $this->revisionStore->newRevisionFromRow( $row, 0, $title );
218
219 return new FeedItem(
220 $title->getPrefixedText(),
221 $this->feedItemDesc( $revision ),
222 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
223 $date,
224 $this->feedItemAuthor( $revision ),
225 $comments
226 );
227 }
228
229 return null;
230 }
231
237 protected function feedItemAuthor( RevisionRecord $revision ) {
238 $user = $revision->getUser();
239 return $user ? $user->getName() : '';
240 }
241
247 protected function feedItemDesc( RevisionRecord $revision ) {
248 $msg = $this->msg( 'colon-separator' )->inContentLanguage()->text();
249 try {
250 $content = $revision->getContent( SlotRecord::MAIN );
251 } catch ( RevisionAccessException $e ) {
252 $content = null;
253 }
254
255 if ( $content instanceof TextContent ) {
256 // only textual content has a "source view".
257 $html = nl2br( htmlspecialchars( $content->getText(), ENT_COMPAT ) );
258 } else {
259 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
260 // contain JS magic and generally may not be suitable for inclusion in a feed.
261 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
262 // Compare also MediaWiki\Feed\FeedUtils::formatDiffRow.
263 $html = '';
264 }
265
266 $comment = $revision->getComment();
267
268 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
269 htmlspecialchars( FeedItem::stripComment( $comment->text ?? '' ) ) .
270 "</p>\n<hr />\n<div>" . $html . '</div>';
271 }
272
273 public function getAllowedParams() {
274 $feedFormatNames = array_keys( $this->getConfig()->get( MainConfigNames::FeedClasses ) );
275
276 $ret = [
277 'feedformat' => [
278 ParamValidator::PARAM_DEFAULT => 'rss',
279 ParamValidator::PARAM_TYPE => $feedFormatNames
280 ],
281 'user' => [
282 ParamValidator::PARAM_TYPE => 'user',
283 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'cidr', 'id', 'interwiki' ],
284 ParamValidator::PARAM_REQUIRED => true,
285 ],
286 'namespace' => [
287 ParamValidator::PARAM_TYPE => 'namespace'
288 ],
289 'year' => [
290 ParamValidator::PARAM_TYPE => 'integer'
291 ],
292 'month' => [
293 ParamValidator::PARAM_TYPE => 'integer'
294 ],
295 'tagfilter' => [
296 ParamValidator::PARAM_ISMULTI => true,
297 ParamValidator::PARAM_TYPE => array_values( ChangeTags::listDefinedTags() ),
298 ParamValidator::PARAM_DEFAULT => '',
299 ],
300 'deletedonly' => false,
301 'toponly' => false,
302 'newonly' => false,
303 'hideminor' => false,
304 'showsizediff' => [
305 ParamValidator::PARAM_DEFAULT => false,
306 ],
307 ];
308
309 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
310 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
311 }
312
313 return $ret;
314 }
315
316 protected function getExamplesMessages() {
317 return [
318 'action=feedcontributions&user=Example'
319 => 'apihelp-feedcontributions-example-simple',
320 ];
321 }
322
323 public function getHelpUrls() {
324 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Feedcontributions';
325 }
326}
array $params
The job parameters.
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:64
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1542
getMain()
Get the main module.
Definition ApiBase.php:559
getResult()
Get the result object.
Definition ApiBase.php:680
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:820
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:171
feedItemDesc(RevisionRecord $revision)
feedItemAuthor(RevisionRecord $revision)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getHelpUrls()
Return links to more detailed help pages about the module.
getCustomPrinter()
This module uses a custom feed wrapper printer.
__construct(ApiMain $main, $action, RevisionStore $revisionStore, LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory, HookContainer $hookContainer, IConnectionProvider $dbProvider, NamespaceInfo $namespaceInfo, UserFactory $userFactory, CommentFormatter $commentFormatter)
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getExamplesMessages()
Returns usage examples for this module.
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 is the main API class, used for both external and internal processing.
Definition ApiMain.php:66
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
This class provides an implementation of the hook interfaces used by the core Action API,...
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()
getContext()
Get the base IContextSource object.
A base class for outputting syndication feeds (e.g.
Definition FeedItem.php:40
Class that generates HTML for internal links.
A class containing constants representing the names of configuration variables.
Pager for Special:Contributions.
Type definition for user types.
Definition UserDef.php:27
Exception representing a failure to look up a revision.
Page revision base class.
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.
getContent( $role, $audience=self::FOR_PUBLIC, Authority $performer=null)
Returns the Content of the given slot of this revision.
Service for looking up page revisions.
Value object representing a content slot associated with a page revision.
Parent class for all special pages.
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:78
Class to parse and build external user names.
Creates 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.