MediaWiki  1.30.0
ChangesFeed.php
Go to the documentation of this file.
1 <?php
24 
30 class ChangesFeed {
32 
37  public function __construct( $format, $type ) {
38  $this->format = $format;
39  $this->type = $type;
40  }
41 
50  public function getFeedObject( $title, $description, $url ) {
52 
53  if ( !isset( $wgFeedClasses[$this->format] ) ) {
54  return false;
55  }
56 
57  if ( !array_key_exists( $this->format, $wgFeedClasses ) ) {
58  // falling back to atom
59  $this->format = 'atom';
60  }
61 
62  $feedTitle = "$wgSitename - {$title} [$wgLanguageCode]";
63  return new $wgFeedClasses[$this->format](
64  $feedTitle, htmlspecialchars( $description ), $url );
65  }
66 
78  public function execute( $feed, $rows, $lastmod, $opts ) {
80 
81  if ( !FeedUtils::checkFeedOutput( $this->format ) ) {
82  return null;
83  }
84 
85  $optionsHash = md5( serialize( $opts->getAllValues() ) ) . $wgRenderHashAppend;
86  $timekey = wfMemcKey(
87  $this->type, $this->format, $wgLang->getCode(), $optionsHash, 'timestamp' );
88  $key = wfMemcKey( $this->type, $this->format, $wgLang->getCode(), $optionsHash );
89 
90  FeedUtils::checkPurge( $timekey, $key );
91 
97  $cachedFeed = $this->loadFromCache( $lastmod, $timekey, $key );
98  if ( is_string( $cachedFeed ) ) {
99  wfDebug( "RC: Outputting cached feed\n" );
100  $feed->httpHeaders();
101  echo $cachedFeed;
102  } else {
103  wfDebug( "RC: rendering new feed and caching it\n" );
104  ob_start();
105  self::generateFeed( $rows, $feed );
106  $cachedFeed = ob_get_contents();
107  ob_end_flush();
108  $this->saveToCache( $cachedFeed, $timekey, $key );
109  }
110  return true;
111  }
112 
120  public function saveToCache( $feed, $timekey, $key ) {
122  $cache->set( $key, $feed, $cache::TTL_DAY );
123  $cache->set( $timekey, wfTimestamp( TS_MW ), $cache::TTL_DAY );
124  }
125 
134  public function loadFromCache( $lastmod, $timekey, $key ) {
136 
138  $feedLastmod = $cache->get( $timekey );
139 
140  if ( ( $wgFeedCacheTimeout > 0 ) && $feedLastmod ) {
148  $feedAge = time() - wfTimestamp( TS_UNIX, $feedLastmod );
149  $feedLastmodUnix = wfTimestamp( TS_UNIX, $feedLastmod );
150  $lastmodUnix = wfTimestamp( TS_UNIX, $lastmod );
151 
152  if ( $feedAge < $wgFeedCacheTimeout || $feedLastmodUnix > $lastmodUnix ) {
153  wfDebug( "RC: loading feed from cache ($key; $feedLastmod; $lastmod)...\n" );
154  if ( $feedLastmodUnix < $lastmodUnix ) {
155  $wgOut->setLastModified( $feedLastmod ); // T23916
156  }
157  return $cache->get( $key );
158  } else {
159  wfDebug( "RC: cached feed timestamp check failed ($feedLastmod; $lastmod)\n" );
160  }
161  }
162  return false;
163  }
164 
170  public static function generateFeed( $rows, &$feed ) {
171  $items = self::buildItems( $rows );
172  $feed->outHeader();
173  foreach ( $items as $item ) {
174  $feed->outItem( $item );
175  }
176  $feed->outFooter();
177  }
178 
184  public static function buildItems( $rows ) {
185  $items = [];
186 
187  # Merge adjacent edits by one user
188  $sorted = [];
189  $n = 0;
190  foreach ( $rows as $obj ) {
191  if ( $obj->rc_type == RC_EXTERNAL ) {
192  continue;
193  }
194 
195  if ( $n > 0 &&
196  $obj->rc_type == RC_EDIT &&
197  $obj->rc_namespace >= 0 &&
198  $obj->rc_cur_id == $sorted[$n - 1]->rc_cur_id &&
199  $obj->rc_user_text == $sorted[$n - 1]->rc_user_text ) {
200  $sorted[$n - 1]->rc_last_oldid = $obj->rc_last_oldid;
201  } else {
202  $sorted[$n] = $obj;
203  $n++;
204  }
205  }
206 
207  foreach ( $sorted as $obj ) {
208  $title = Title::makeTitle( $obj->rc_namespace, $obj->rc_title );
209  $talkpage = MWNamespace::canTalk( $obj->rc_namespace )
210  ? $title->getTalkPage()->getFullURL()
211  : '';
212 
213  // Skip items with deleted content (avoids partially complete/inconsistent output)
214  if ( $obj->rc_deleted ) {
215  continue;
216  }
217 
218  if ( $obj->rc_this_oldid ) {
219  $url = $title->getFullURL( [
220  'diff' => $obj->rc_this_oldid,
221  'oldid' => $obj->rc_last_oldid,
222  ] );
223  } else {
224  // log entry or something like that.
225  $url = $title->getFullURL();
226  }
227 
228  $items[] = new FeedItem(
229  $title->getPrefixedText(),
230  FeedUtils::formatDiff( $obj ),
231  $url,
232  $obj->rc_timestamp,
233  ( $obj->rc_deleted & Revision::DELETED_USER )
234  ? wfMessage( 'rev-deleted-user' )->escaped() : $obj->rc_user_text,
235  $talkpage
236  );
237  }
238 
239  return $items;
240  }
241 }
Revision\DELETED_USER
const DELETED_USER
Definition: Revision.php:92
ChangesFeed\generateFeed
static generateFeed( $rows, &$feed)
Generate the feed items given a row from the database, printing the feed.
Definition: ChangesFeed.php:170
ChangesFeed\__construct
__construct( $format, $type)
Definition: ChangesFeed.php:37
FeedItem
A base class for basic support for outputting syndication feeds in RSS and other formats.
Definition: Feed.php:38
RC_EXTERNAL
const RC_EXTERNAL
Definition: Defines.php:146
type
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition: postgres.txt:22
ChangesFeed\$type
$type
Definition: ChangesFeed.php:31
$wgFeedCacheTimeout
$wgFeedCacheTimeout
Minimum timeout for cached Recentchanges feed, in seconds.
Definition: DefaultSettings.php:6866
ChangesFeed\$format
$format
Definition: ChangesFeed.php:31
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
ChangesFeed\loadFromCache
loadFromCache( $lastmod, $timekey, $key)
Try to load the feed result from cache.
Definition: ChangesFeed.php:134
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RC_EDIT
const RC_EDIT
Definition: Defines.php:143
serialize
serialize()
Definition: ApiMessage.php:177
Wikimedia\Rdbms\ResultWrapper
Result wrapper for grabbing data queried from an IDatabase object.
Definition: ResultWrapper.php:24
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
ChangesFeed\$descMsg
$descMsg
Definition: ChangesFeed.php:31
ChangesFeed\getFeedObject
getFeedObject( $title, $description, $url)
Get a ChannelFeed subclass object to use.
Definition: ChangesFeed.php:50
wfMemcKey
wfMemcKey()
Make a cache key for the local wiki.
Definition: GlobalFunctions.php:2756
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:932
MWNamespace\canTalk
static canTalk( $index)
Does this namespace ever have a talk namespace?
Definition: MWNamespace.php:288
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
FeedUtils\checkPurge
static checkPurge( $timekey, $key)
Check whether feed's cache should be cleared; for changes feeds If the feed should be purged; $timeke...
Definition: FeedUtils.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:529
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
$wgFeedClasses
$wgFeedClasses
Available feeds objects.
Definition: DefaultSettings.php:6893
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:1047
ChangesFeed
Feed to Special:RecentChanges and Special:RecentChangesLiked.
Definition: ChangesFeed.php:30
ChangesFeed\$titleMsg
$titleMsg
Definition: ChangesFeed.php:31
$wgLanguageCode
$wgLanguageCode
Site language code.
Definition: DefaultSettings.php:2861
$wgSitename
$wgSitename
Name of the site.
Definition: DefaultSettings.php:83
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1639
FeedUtils\formatDiff
static formatDiff( $row)
Format a diff for the newsfeed.
Definition: FeedUtils.php:79
$cache
$cache
Definition: mcc.php:33
$rows
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition: hooks.txt:2581
ObjectCache\getMainWANInstance
static getMainWANInstance()
Get the main WAN cache object.
Definition: ObjectCache.php:370
ChangesFeed\execute
execute( $feed, $rows, $lastmod, $opts)
Generates feed's content.
Definition: ChangesFeed.php:78
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
$wgRenderHashAppend
$wgRenderHashAppend
Append a configured value to the parser cache and the sitenotice key so that they can be kept separat...
Definition: DefaultSettings.php:2574
ChangesFeed\buildItems
static buildItems( $rows)
Generate the feed items given a row from the database.
Definition: ChangesFeed.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
FeedUtils\checkFeedOutput
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition: FeedUtils.php:56
ChangesFeed\saveToCache
saveToCache( $feed, $timekey, $key)
Save to feed result to cache.
Definition: ChangesFeed.php:120
$wgOut
$wgOut
Definition: Setup.php:819