MediaWiki  1.31.0
ApiFeedContributions.php
Go to the documentation of this file.
1 <?php
27 
33  public function getCustomPrinter() {
34  return new ApiFormatFeedWrapper( $this->getMain() );
35  }
36 
37  public function execute() {
38  $params = $this->extractRequestParams();
39 
40  $config = $this->getConfig();
41  if ( !$config->get( 'Feed' ) ) {
42  $this->dieWithError( 'feed-unavailable' );
43  }
44 
45  $feedClasses = $config->get( 'FeedClasses' );
46  if ( !isset( $feedClasses[$params['feedformat']] ) ) {
47  $this->dieWithError( 'feed-invalid' );
48  }
49 
50  if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
51  $this->dieWithError( 'apierror-sizediffdisabled' );
52  }
53 
54  $msg = wfMessage( 'Contributions' )->inContentLanguage()->text();
55  $feedTitle = $config->get( 'Sitename' ) . ' - ' . $msg .
56  ' [' . $config->get( 'LanguageCode' ) . ']';
57  $feedUrl = SpecialPage::getTitleFor( 'Contributions', $params['user'] )->getFullURL();
58 
59  $target = $params['user'] == 'newbies'
60  ? 'newbies'
61  : Title::makeTitleSafe( NS_USER, $params['user'] )->getText();
62 
63  $feed = new $feedClasses[$params['feedformat']] (
64  $feedTitle,
65  htmlspecialchars( $msg ),
66  $feedUrl
67  );
68 
69  // Convert year/month parameters to end parameter
70  $params['start'] = '';
71  $params['end'] = '';
73 
74  $pager = new ContribsPager( $this->getContext(), [
75  'target' => $target,
76  'namespace' => $params['namespace'],
77  'start' => $params['start'],
78  'end' => $params['end'],
79  'tagFilter' => $params['tagfilter'],
80  'deletedOnly' => $params['deletedonly'],
81  'topOnly' => $params['toponly'],
82  'newOnly' => $params['newonly'],
83  'hideMinor' => $params['hideminor'],
84  'showSizeDiff' => $params['showsizediff'],
85  ] );
86 
87  $feedLimit = $this->getConfig()->get( 'FeedLimit' );
88  if ( $pager->getLimit() > $feedLimit ) {
89  $pager->setLimit( $feedLimit );
90  }
91 
92  $feedItems = [];
93  if ( $pager->getNumRows() > 0 ) {
94  $count = 0;
95  $limit = $pager->getLimit();
96  foreach ( $pager->mResult as $row ) {
97  // ContribsPager selects one more row for navigation, skip that row
98  if ( ++$count > $limit ) {
99  break;
100  }
101  $item = $this->feedItem( $row );
102  if ( $item !== null ) {
103  $feedItems[] = $item;
104  }
105  }
106  }
107 
108  ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
109  }
110 
111  protected function feedItem( $row ) {
112  // This hook is the api contributions equivalent to the
113  // ContributionsLineEnding hook. Hook implementers may cancel
114  // the hook to signal the user is not allowed to read this item.
115  $feedItem = null;
116  $hookResult = Hooks::run(
117  'ApiFeedContributions::feedItem',
118  [ $row, $this->getContext(), &$feedItem ]
119  );
120  // Hook returned a valid feed item
121  if ( $feedItem instanceof FeedItem ) {
122  return $feedItem;
123  // Hook was canceled and did not return a valid feed item
124  } elseif ( !$hookResult ) {
125  return null;
126  }
127 
128  // Hook completed and did not return a valid feed item
129  $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
130  if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
131  $date = $row->rev_timestamp;
132  $comments = $title->getTalkPage()->getFullURL();
133  $revision = Revision::newFromRow( $row );
134 
135  return new FeedItem(
136  $title->getPrefixedText(),
137  $this->feedItemDesc( $revision ),
138  $title->getFullURL( [ 'diff' => $revision->getId() ] ),
139  $date,
140  $this->feedItemAuthor( $revision ),
141  $comments
142  );
143  }
144 
145  return null;
146  }
147 
152  protected function feedItemAuthor( $revision ) {
153  return $revision->getUserText();
154  }
155 
160  protected function feedItemDesc( $revision ) {
161  if ( $revision ) {
162  $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
163  $content = $revision->getContent();
164 
165  if ( $content instanceof TextContent ) {
166  // only textual content has a "source view".
167  $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
168  } else {
169  // XXX: we could get an HTML representation of the content via getParserOutput, but that may
170  // contain JS magic and generally may not be suitable for inclusion in a feed.
171  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
172  // Compare also FeedUtils::formatDiffRow.
173  $html = '';
174  }
175 
176  return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
177  htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
178  "</p>\n<hr />\n<div>" . $html . '</div>';
179  }
180 
181  return '';
182  }
183 
184  public function getAllowedParams() {
185  $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
186 
187  $ret = [
188  'feedformat' => [
189  ApiBase::PARAM_DFLT => 'rss',
190  ApiBase::PARAM_TYPE => $feedFormatNames
191  ],
192  'user' => [
193  ApiBase::PARAM_TYPE => 'user',
194  ApiBase::PARAM_REQUIRED => true,
195  ],
196  'namespace' => [
197  ApiBase::PARAM_TYPE => 'namespace'
198  ],
199  'year' => [
200  ApiBase::PARAM_TYPE => 'integer'
201  ],
202  'month' => [
203  ApiBase::PARAM_TYPE => 'integer'
204  ],
205  'tagfilter' => [
206  ApiBase::PARAM_ISMULTI => true,
208  ApiBase::PARAM_DFLT => '',
209  ],
210  'deletedonly' => false,
211  'toponly' => false,
212  'newonly' => false,
213  'hideminor' => false,
214  'showsizediff' => [
215  ApiBase::PARAM_DFLT => false,
216  ],
217  ];
218 
219  if ( $this->getConfig()->get( 'MiserMode' ) ) {
220  $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
221  }
222 
223  return $ret;
224  }
225 
226  protected function getExamplesMessages() {
227  return [
228  'action=feedcontributions&user=Example'
229  => 'apihelp-feedcontributions-example-simple',
230  ];
231  }
232 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
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:40
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:111
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1895
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:124
ApiFeedContributions\getCustomPrinter
getCustomPrinter()
This module uses a custom feed wrapper printer.
Definition: ApiFeedContributions.php:33
ApiFeedContributions\feedItemAuthor
feedItemAuthor( $revision)
Definition: ApiFeedContributions.php:152
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:87
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:641
ApiFeedContributions\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiFeedContributions.php:37
FeedItem\stripComment
static stripComment( $text)
Quickie hack...
Definition: Feed.php:223
$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:37
ContribsPager\processDateFilter
static processDateFilter(array $opts)
Set up date filter options, given request data.
Definition: ContribsPager.php:642
ApiFeedContributions\feedItemDesc
feedItemDesc( $revision)
Definition: ApiFeedContributions.php:160
$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:1987
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
ApiFormatFeedWrapper
This printer is used to wrap an instance of the Feed class.
Definition: ApiFormatFeedWrapper.php:27
ApiFeedContributions
Definition: ApiFeedContributions.php:26
ChangeTags\listDefinedTags
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything.
Definition: ChangeTags.php:1304
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:534
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:562
ApiBase\extractRequestParams
extractRequestParams( $parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:749
Revision\newFromRow
static newFromRow( $row)
Definition: Revision.php:218
$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:1987
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:48
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:51
NS_USER
const NS_USER
Definition: Defines.php:67
ApiFeedContributions\feedItem
feedItem( $row)
Definition: ApiFeedContributions.php:111
ApiBase\getMain
getMain()
Get the main module.
Definition: ApiBase.php:537
ApiFeedContributions\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiFeedContributions.php:184
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:226
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:39