MediaWiki  1.30.0
ApiFeedContributions.php
Go to the documentation of this file.
1 <?php
31 
37  public function getCustomPrinter() {
38  return new ApiFormatFeedWrapper( $this->getMain() );
39  }
40 
41  public function execute() {
42  $params = $this->extractRequestParams();
43 
44  $config = $this->getConfig();
45  if ( !$config->get( 'Feed' ) ) {
46  $this->dieWithError( 'feed-unavailable' );
47  }
48 
49  $feedClasses = $config->get( 'FeedClasses' );
50  if ( !isset( $feedClasses[$params['feedformat']] ) ) {
51  $this->dieWithError( 'feed-invalid' );
52  }
53 
54  if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
55  $this->dieWithError( 'apierror-sizediffdisabled' );
56  }
57 
58  $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
59  $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
60  ' [' . $config->get( 'LanguageCode' ) . ']';
61  $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
62 
63  $target = $params['user'] == 'newbies'
64  ? 'newbies'
65  : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
66 
67  $feed = new $feedClasses[$params['feedformat']] (
68  $feedTitle,
69  htmlspecialchars( $msg ),
70  $feedUrl
71  );
72 
73  // Convert year/month parameters to end parameter
74  $params['start'] = '';
75  $params['end'] = '';
77 
78  $pager = new ContribsPager( $this->getContext(), [
79  'target' => $target,
80  'namespace' => $params['namespace'],
81  'start' => $params['start'],
82  'end' => $params['end'],
83  'tagFilter' => $params['tagfilter'],
84  'deletedOnly' => $params['deletedonly'],
85  'topOnly' => $params['toponly'],
86  'newOnly' => $params['newonly'],
87  'hideMinor' => $params['hideminor'],
88  'showSizeDiff' => $params['showsizediff'],
89  ] );
90 
91  $feedLimit = $this->getConfig()->get( 'FeedLimit' );
92  if ( $pager->getLimit() > $feedLimit ) {
93  $pager->setLimit( $feedLimit );
94  }
95 
96  $feedItems = [];
97  if ( $pager->getNumRows() > 0 ) {
98  $count = 0;
99  $limit = $pager->getLimit();
100  foreach ( $pager->mResult as $row ) {
101  // ContribsPager selects one more row for navigation, skip that row
102  if ( ++$count > $limit ) {
103  break;
104  }
105  $item = $this->feedItem( $row );
106  if ( $item !== null ) {
107  $feedItems[] = $item;
108  }
109  }
110  }
111 
112  ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
113  }
114 
115  protected function feedItem( $row ) {
116  // This hook is the api contributions equivalent to the
117  // ContributionsLineEnding hook. Hook implementers may cancel
118  // the hook to signal the user is not allowed to read this item.
119  $feedItem = null;
120  $hookResult = Hooks::run(
121  'ApiFeedContributions::feedItem',
122  [ $row, $this->getContext(), &$feedItem ]
123  );
124  // Hook returned a valid feed item
125  if ( $feedItem instanceof FeedItem ) {
126  return $feedItem;
127  // Hook was canceled and did not return a valid feed item
128  } elseif ( !$hookResult ) {
129  return null;
130  }
131 
132  // Hook completed and did not return a valid feed item
133  $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
134  if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
135  $date = $row->rev_timestamp;
136  $comments = $title->getTalkPage()->getFullURL();
137  $revision = Revision::newFromRow( $row );
138 
139  return new FeedItem(
140  $title->getPrefixedText(),
141  $this->feedItemDesc( $revision ),
142  $title->getFullURL( [ 'diff' => $revision->getId() ] ),
143  $date,
144  $this->feedItemAuthor( $revision ),
145  $comments
146  );
147  }
148 
149  return null;
150  }
151 
156  protected function feedItemAuthor( $revision ) {
157  return $revision->getUserText();
158  }
159 
164  protected function feedItemDesc( $revision ) {
165  if ( $revision ) {
166  $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
167  $content = $revision->getContent();
168 
169  if ( $content instanceof TextContent ) {
170  // only textual content has a "source view".
171  $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
172  } else {
173  // XXX: we could get an HTML representation of the content via getParserOutput, but that may
174  // contain JS magic and generally may not be suitable for inclusion in a feed.
175  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
176  // Compare also FeedUtils::formatDiffRow.
177  $html = '';
178  }
179 
180  return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
181  htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
182  "</p>\n<hr />\n<div>" . $html . '</div>';
183  }
184 
185  return '';
186  }
187 
188  public function getAllowedParams() {
189  $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
190 
191  $ret = [
192  'feedformat' => [
193  ApiBase::PARAM_DFLT => 'rss',
194  ApiBase::PARAM_TYPE => $feedFormatNames
195  ],
196  'user' => [
197  ApiBase::PARAM_TYPE => 'user',
198  ApiBase::PARAM_REQUIRED => true,
199  ],
200  'namespace' => [
201  ApiBase::PARAM_TYPE => 'namespace'
202  ],
203  'year' => [
204  ApiBase::PARAM_TYPE => 'integer'
205  ],
206  'month' => [
207  ApiBase::PARAM_TYPE => 'integer'
208  ],
209  'tagfilter' => [
210  ApiBase::PARAM_ISMULTI => true,
212  ApiBase::PARAM_DFLT => '',
213  ],
214  'deletedonly' => false,
215  'toponly' => false,
216  'newonly' => false,
217  'hideminor' => false,
218  'showsizediff' => [
219  ApiBase::PARAM_DFLT => false,
220  ],
221  ];
222 
223  if ( $this->getConfig()->get( 'MiserMode' ) ) {
224  $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
225  }
226 
227  return $ret;
228  }
229 
230  protected function getExamplesMessages() {
231  return [
232  'action=feedcontributions&user=Example'
233  => 'apihelp-feedcontributions-example-simple',
234  ];
235  }
236 }
ContextSource\getConfig
getConfig()
Get the Config object.
Definition: ContextSource.php:68
FeedItem
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:41
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:115
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1855
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:128
ApiFeedContributions\getCustomPrinter
getCustomPrinter()
This module uses a custom feed wrapper printer.
Definition: ApiFeedContributions.php:37
ApiFeedContributions\feedItemAuthor
feedItemAuthor( $revision)
Definition: ApiFeedContributions.php:156
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:91
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:632
ApiFeedContributions\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiFeedContributions.php:41
FeedItem\stripComment
static stripComment( $text)
Quickie hack...
Definition: Feed.php:178
$params
$params
Definition: styleTest.css.php:40
SpecialPage\getTitleFor
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,...
Definition: SpecialPage.php:82
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:41
ApiFeedContributions\feedItemDesc
feedItemDesc( $revision)
Definition: ApiFeedContributions.php:164
$html
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:1965
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
ApiFormatFeedWrapper
This printer is used to wrap an instance of the Feed class.
Definition: ApiFormatFeedWrapper.php:31
ApiFeedContributions
Definition: ApiFeedContributions.php:30
ChangeTags\listDefinedTags
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
Definition: ChangeTags.php:1253
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
ContribsPager
Definition: ContribsPager.php:31
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:557
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:740
Revision\newFromRow
static newFromRow( $row)
Definition: Revision.php:238
$ret
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:1965
ContribsPager\processDateFilter
static processDateFilter( $opts)
Set up date filter options, given request data.
Definition: ContribsPager.php:661
TextContent
Content object implementation for representing flat text.
Definition: TextContent.php:35
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:52
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:55
NS_USER
const NS_USER
Definition: Defines.php:67
ApiFeedContributions\feedItem
feedItem( $row)
Definition: ApiFeedContributions.php:115
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:528
ApiFeedContributions\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiFeedContributions.php:188
wfMessage
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 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
ApiFeedContributions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiFeedContributions.php:230
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:203
ApiFormatFeedWrapper\setResult
static setResult( $result, $feed, $feedItems)
Call this method to initialize output data.
Definition: ApiFormatFeedWrapper.php:43