MediaWiki REL1_33
ApiFeedContributions.php
Go to the documentation of this file.
1<?php
28
33
36
42 public function getCustomPrinter() {
43 return new ApiFormatFeedWrapper( $this->getMain() );
44 }
45
46 public function execute() {
47 $this->revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
48
50
51 $config = $this->getConfig();
52 if ( !$config->get( 'Feed' ) ) {
53 $this->dieWithError( 'feed-unavailable' );
54 }
55
56 $feedClasses = $config->get( 'FeedClasses' );
57 if ( !isset( $feedClasses[$params['feedformat']] ) ) {
58 $this->dieWithError( 'feed-invalid' );
59 }
60
61 if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
62 $this->dieWithError( 'apierror-sizediffdisabled' );
63 }
64
65 $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
66 $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
67 ' [' . $config->get( 'LanguageCode' ) . ']';
68 $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
69
70 $target = $params['user'] == 'newbies'
71 ? 'newbies'
72 : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
73
74 $feed = new $feedClasses[$params['feedformat']] (
76 htmlspecialchars( $msg ),
78 );
79
80 // Convert year/month parameters to end parameter
81 $params['start'] = '';
82 $params['end'] = '';
84
85 $pager = new ContribsPager( $this->getContext(), [
86 'target' => $target,
87 'namespace' => $params['namespace'],
88 'start' => $params['start'],
89 'end' => $params['end'],
90 'tagFilter' => $params['tagfilter'],
91 'deletedOnly' => $params['deletedonly'],
92 'topOnly' => $params['toponly'],
93 'newOnly' => $params['newonly'],
94 'hideMinor' => $params['hideminor'],
95 'showSizeDiff' => $params['showsizediff'],
96 ] );
97
98 $feedLimit = $this->getConfig()->get( 'FeedLimit' );
99 if ( $pager->getLimit() > $feedLimit ) {
100 $pager->setLimit( $feedLimit );
101 }
102
103 $feedItems = [];
104 if ( $pager->getNumRows() > 0 ) {
105 $count = 0;
106 $limit = $pager->getLimit();
107 foreach ( $pager->mResult as $row ) {
108 // ContribsPager selects one more row for navigation, skip that row
109 if ( ++$count > $limit ) {
110 break;
111 }
112 $item = $this->feedItem( $row );
113 if ( $item !== null ) {
114 $feedItems[] = $item;
115 }
116 }
117 }
118
119 ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
120 }
121
122 protected function feedItem( $row ) {
123 // This hook is the api contributions equivalent to the
124 // ContributionsLineEnding hook. Hook implementers may cancel
125 // the hook to signal the user is not allowed to read this item.
126 $feedItem = null;
127 $hookResult = Hooks::run(
128 'ApiFeedContributions::feedItem',
129 [ $row, $this->getContext(), &$feedItem ]
130 );
131 // Hook returned a valid feed item
132 if ( $feedItem instanceof FeedItem ) {
133 return $feedItem;
134 // Hook was canceled and did not return a valid feed item
135 } elseif ( !$hookResult ) {
136 return null;
137 }
138
139 // Hook completed and did not return a valid feed item
140 $title = Title::makeTitle( (int)$row->page_namespace, $row->page_title );
141 if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
142 $date = $row->rev_timestamp;
143 $comments = $title->getTalkPage()->getFullURL();
144 $revision = $this->revisionStore->newRevisionFromRow( $row );
145
146 return new FeedItem(
147 $title->getPrefixedText(),
148 $this->feedItemDesc( $revision ),
149 $title->getFullURL( [ 'diff' => $revision->getId() ] ),
150 $date,
151 $this->feedItemAuthor( $revision ),
152 $comments
153 );
154 }
155
156 return null;
157 }
158
164 protected function feedItemAuthor( RevisionRecord $revision ) {
165 $user = $revision->getUser();
166 return $user ? $user->getName() : '';
167 }
168
174 protected function feedItemDesc( RevisionRecord $revision ) {
175 $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
176 try {
177 $content = $revision->getContent( SlotRecord::MAIN );
178 } catch ( RevisionAccessException $e ) {
179 $content = null;
180 }
181
182 if ( $content instanceof TextContent ) {
183 // only textual content has a "source view".
184 $html = nl2br( htmlspecialchars( $content->getText() ) );
185 } else {
186 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
187 // contain JS magic and generally may not be suitable for inclusion in a feed.
188 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
189 // Compare also FeedUtils::formatDiffRow.
190 $html = '';
191 }
192
193 $comment = $revision->getComment();
194
195 return '<p>' . htmlspecialchars( $this->feedItemAuthor( $revision ) ) . $msg .
196 htmlspecialchars( FeedItem::stripComment( $comment ? $comment->text : '' ) ) .
197 "</p>\n<hr />\n<div>" . $html . '</div>';
198 }
199
200 public function getAllowedParams() {
201 $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
202
203 $ret = [
204 'feedformat' => [
205 ApiBase::PARAM_DFLT => 'rss',
207 ],
208 'user' => [
209 ApiBase::PARAM_TYPE => 'user',
211 ],
212 'namespace' => [
213 ApiBase::PARAM_TYPE => 'namespace'
214 ],
215 'year' => [
216 ApiBase::PARAM_TYPE => 'integer'
217 ],
218 'month' => [
219 ApiBase::PARAM_TYPE => 'integer'
220 ],
221 'tagfilter' => [
225 ],
226 'deletedonly' => false,
227 'toponly' => false,
228 'newonly' => false,
229 'hideminor' => false,
230 'showsizediff' => [
231 ApiBase::PARAM_DFLT => false,
232 ],
233 ];
234
235 if ( $this->getConfig()->get( 'MiserMode' ) ) {
236 $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
237 }
238
239 return $ret;
240 }
241
242 protected function getExamplesMessages() {
243 return [
244 'action=feedcontributions&user=Example'
245 => 'apihelp-feedcontributions-example-simple',
246 ];
247 }
248}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:37
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:111
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:1990
getMain()
Get the main module.
Definition ApiBase.php:528
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
getResult()
Get the result object.
Definition ApiBase.php:632
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:743
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:124
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:51
feedItemDesc(RevisionRecord $revision)
feedItemAuthor(RevisionRecord $revision)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getCustomPrinter()
This module uses a custom feed wrapper printer.
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.
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
getContext()
Get the base IContextSource object.
static processDateFilter(array $opts)
Set up date filter options, given request data.
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition Feed.php:38
static stripComment( $text)
Quickie hack... strip out wikilinks to more legible form from the comment.
Definition Feed.php:223
MediaWikiServices is the service locator for the application scope of MediaWiki.
Exception representing a failure to look up a revision.
Page revision base class.
getComment( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision comment, if it's available to the specified audience.
getContent( $role, $audience=self::FOR_PUBLIC, User $user=null)
Returns the Content of the given slot of this revision.
getUser( $audience=self::FOR_PUBLIC, User $user=null)
Fetch revision's author's user identity, 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.
Content object implementation for representing flat text.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2003
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
returning false will NOT prevent logging $e
Definition hooks.txt:2175
$content
$params