MediaWiki  1.28.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->dieUsage( 'Syndication feeds are not available', 'feed-unavailable' );
47  }
48 
49  $feedClasses = $config->get( 'FeedClasses' );
50  if ( !isset( $feedClasses[$params['feedformat']] ) ) {
51  $this->dieUsage( 'Invalid subscription feed type', 'feed-invalid' );
52  }
53 
54  if ( $params['showsizediff'] && $this->getConfig()->get( 'MiserMode' ) ) {
55  $this->dieUsage( 'Size difference is disabled in Miser Mode', '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  $pager = new ContribsPager( $this->getContext(), [
74  'target' => $target,
75  'namespace' => $params['namespace'],
76  'year' => $params['year'],
77  'month' => $params['month'],
78  'tagFilter' => $params['tagfilter'],
79  'deletedOnly' => $params['deletedonly'],
80  'topOnly' => $params['toponly'],
81  'newOnly' => $params['newonly'],
82  'hideMinor' => $params['hideminor'],
83  'showSizeDiff' => $params['showsizediff'],
84  ] );
85 
86  $feedLimit = $this->getConfig()->get( 'FeedLimit' );
87  if ( $pager->getLimit() > $feedLimit ) {
88  $pager->setLimit( $feedLimit );
89  }
90 
91  $feedItems = [];
92  if ( $pager->getNumRows() > 0 ) {
93  $count = 0;
94  $limit = $pager->getLimit();
95  foreach ( $pager->mResult as $row ) {
96  // ContribsPager selects one more row for navigation, skip that row
97  if ( ++$count > $limit ) {
98  break;
99  }
100  $item = $this->feedItem( $row );
101  if ( $item !== null ) {
102  $feedItems[] = $item;
103  }
104  }
105  }
106 
107  ApiFormatFeedWrapper::setResult( $this->getResult(), $feed, $feedItems );
108  }
109 
110  protected function feedItem( $row ) {
111  // This hook is the api contributions equivalent to the
112  // ContributionsLineEnding hook. Hook implementers may cancel
113  // the hook to signal the user is not allowed to read this item.
114  $feedItem = null;
115  $hookResult = Hooks::run(
116  'ApiFeedContributions::feedItem',
117  [ $row, $this->getContext(), &$feedItem ]
118  );
119  // Hook returned a valid feed item
120  if ( $feedItem instanceof FeedItem ) {
121  return $feedItem;
122  // Hook was canceled and did not return a valid feed item
123  } elseif ( !$hookResult ) {
124  return null;
125  }
126 
127  // Hook completed and did not return a valid feed item
128  $title = Title::makeTitle( intval( $row->page_namespace ), $row->page_title );
129  if ( $title && $title->userCan( 'read', $this->getUser() ) ) {
130  $date = $row->rev_timestamp;
131  $comments = $title->getTalkPage()->getFullURL();
132  $revision = Revision::newFromRow( $row );
133 
134  return new FeedItem(
135  $title->getPrefixedText(),
136  $this->feedItemDesc( $revision ),
137  $title->getFullURL( [ 'diff' => $revision->getId() ] ),
138  $date,
139  $this->feedItemAuthor( $revision ),
140  $comments
141  );
142  }
143 
144  return null;
145  }
146 
151  protected function feedItemAuthor( $revision ) {
152  return $revision->getUserText();
153  }
154 
159  protected function feedItemDesc( $revision ) {
160  if ( $revision ) {
161  $msg = wfMessage( 'colon-separator' )->inContentLanguage()->text();
162  $content = $revision->getContent();
163 
164  if ( $content instanceof TextContent ) {
165  // only textual content has a "source view".
166  $html = nl2br( htmlspecialchars( $content->getNativeData() ) );
167  } else {
168  // XXX: we could get an HTML representation of the content via getParserOutput, but that may
169  // contain JS magic and generally may not be suitable for inclusion in a feed.
170  // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
171  // Compare also FeedUtils::formatDiffRow.
172  $html = '';
173  }
174 
175  return '<p>' . htmlspecialchars( $revision->getUserText() ) . $msg .
176  htmlspecialchars( FeedItem::stripComment( $revision->getComment() ) ) .
177  "</p>\n<hr />\n<div>" . $html . '</div>';
178  }
179 
180  return '';
181  }
182 
183  public function getAllowedParams() {
184  $feedFormatNames = array_keys( $this->getConfig()->get( 'FeedClasses' ) );
185 
186  $ret = [
187  'feedformat' => [
188  ApiBase::PARAM_DFLT => 'rss',
189  ApiBase::PARAM_TYPE => $feedFormatNames
190  ],
191  'user' => [
192  ApiBase::PARAM_TYPE => 'user',
193  ApiBase::PARAM_REQUIRED => true,
194  ],
195  'namespace' => [
196  ApiBase::PARAM_TYPE => 'namespace'
197  ],
198  'year' => [
199  ApiBase::PARAM_TYPE => 'integer'
200  ],
201  'month' => [
202  ApiBase::PARAM_TYPE => 'integer'
203  ],
204  'tagfilter' => [
205  ApiBase::PARAM_ISMULTI => true,
207  ApiBase::PARAM_DFLT => '',
208  ],
209  'deletedonly' => false,
210  'toponly' => false,
211  'newonly' => false,
212  'hideminor' => false,
213  'showsizediff' => [
214  ApiBase::PARAM_DFLT => false,
215  ],
216  ];
217 
218  if ( $this->getConfig()->get( 'MiserMode' ) ) {
219  $ret['showsizediff'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
220  }
221 
222  return $ret;
223  }
224 
225  protected function getExamplesMessages() {
226  return [
227  'action=feedcontributions&user=Example'
228  => 'apihelp-feedcontributions-example-simple',
229  ];
230  }
231 }
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
static listDefinedTags()
Basically lists defined tags which count even if they aren't applied to anything. ...
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:1936
getResult()
Get the result object.
Definition: ApiBase.php:577
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:1936
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
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
getMain()
Get the main module.
Definition: ApiBase.php:473
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:112
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:678
static stripComment($text)
Quickie hack...
Definition: Feed.php:180
Content object implementation for representing flat text.
Definition: TextContent.php:35
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 unsetoffset-wrap String Wrap the message in html(usually something like"&lt
getConfig()
Get the Config object.
getContext()
Get the base IContextSource object.
$params
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
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
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:125
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
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1046
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:53
static newFromRow($row)
Definition: Revision.php:230
static setResult($result, $feed, $feedItems)
Call this method to initialize output data.
dieUsage($description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1574
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1046
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
$count
Pager for Special:Contributions.
getCustomPrinter()
This module uses a custom feed wrapper printer.
This printer is used to wrap an instance of the Feed class.
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511