MediaWiki REL1_31
FeedUtils.php
Go to the documentation of this file.
1<?php
29class FeedUtils {
30
38 public static function checkPurge( $timekey, $key ) {
40
41 $purge = $wgRequest->getVal( 'action' ) === 'purge';
42 // Allow users with 'purge' right to clear feed caches
43 if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
44 $cache = ObjectCache::getMainWANInstance();
45 $cache->delete( $timekey, 1 );
46 $cache->delete( $key, 1 );
47 }
48 }
49
56 public static function checkFeedOutput( $type ) {
58
59 if ( !$wgFeed ) {
60 $wgOut->addWikiMsg( 'feed-unavailable' );
61 return false;
62 }
63
64 if ( !isset( $wgFeedClasses[$type] ) ) {
65 $wgOut->addWikiMsg( 'feed-invalid' );
66 return false;
67 }
68
69 return true;
70 }
71
79 public static function formatDiff( $row ) {
80 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
81 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
82 $actiontext = '';
83 if ( $row->rc_type == RC_LOG ) {
84 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
85 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
86 }
87 return self::formatDiffRow( $titleObj,
88 $row->rc_last_oldid, $row->rc_this_oldid,
89 $timestamp,
90 $row->rc_deleted & Revision::DELETED_COMMENT
91 ? wfMessage( 'rev-deleted-comment' )->escaped()
92 : CommentStore::getStore()->getComment( 'rc_comment', $row )->text,
93 $actiontext
94 );
95 }
96
108 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
109 $comment, $actiontext = ''
110 ) {
112
113 // log entries
114 $completeText = '<p>' . implode( ' ',
115 array_filter(
116 [
117 $actiontext,
118 Linker::formatComment( $comment ) ] ) ) . "</p>\n";
119
120 // NOTE: Check permissions for anonymous users, not current user.
121 // No "privileged" version should end up in the cache.
122 // Most feed readers will not log in anyway.
123 $anon = new User();
124 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
125
126 // Can't diff special pages, unreadable pages or pages with no new revision
127 // to compare against: just return the text.
128 if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
129 return $completeText;
130 }
131
132 if ( $oldid ) {
133 $diffText = '';
134 // Don't bother generating the diff if we won't be able to show it
135 if ( $wgFeedDiffCutoff > 0 ) {
136 $rev = Revision::newFromId( $oldid );
137
138 if ( !$rev ) {
139 $diffText = false;
140 } else {
142 $context->setTitle( $title );
143
144 $contentHandler = $rev->getContentHandler();
145 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
146 $diffText = $de->getDiff(
147 wfMessage( 'previousrevision' )->text(), // hack
148 wfMessage( 'revisionasof',
149 $wgLang->timeanddate( $timestamp ),
150 $wgLang->date( $timestamp ),
151 $wgLang->time( $timestamp ) )->text() );
152 }
153 }
154
155 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
156 // Omit large diffs
157 $diffText = self::getDiffLink( $title, $newid, $oldid );
158 } elseif ( $diffText === false ) {
159 // Error in diff engine, probably a missing revision
160 $diffText = "<p>Can't load revision $newid</p>";
161 } else {
162 // Diff output fine, clean up any illegal UTF-8
163 $diffText = UtfNormal\Validator::cleanUp( $diffText );
164 $diffText = self::applyDiffStyle( $diffText );
165 }
166 } else {
167 $rev = Revision::newFromId( $newid );
168 if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
169 $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
170 } else {
171 $newContent = $rev->getContent();
172 }
173
174 if ( $newContent instanceof TextContent ) {
175 // only textual content has a "source view".
176 $text = $newContent->getNativeData();
177
178 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
179 $html = null;
180 } else {
181 $html = nl2br( htmlspecialchars( $text ) );
182 }
183 } else {
184 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
185 // contain JS magic and generally may not be suitable for inclusion in a feed.
186 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
187 // Compare also ApiFeedContributions::feedItemDesc
188 $html = null;
189 }
190
191 if ( $html === null ) {
192 // Omit large new page diffs, T31110
193 // Also use diff link for non-textual content
194 $diffText = self::getDiffLink( $title, $newid );
195 } else {
196 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
197 '<div>' . $html . '</div>';
198 }
199 }
200 $completeText .= $diffText;
201
202 return $completeText;
203 }
204
214 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
215 $queryParameters = [ 'diff' => $newid ];
216 if ( $oldid != null ) {
217 $queryParameters['oldid'] = $oldid;
218 }
219 $diffUrl = $title->getFullURL( $queryParameters );
220
221 $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
222 wfMessage( 'showdiff' )->inContentLanguage()->text() );
223
224 return $diffLink;
225 }
226
235 public static function applyDiffStyle( $text ) {
236 $styles = [
237 'diff' => 'background-color: #fff; color: #222;',
238 'diff-otitle' => 'background-color: #fff; color: #222; text-align: center;',
239 'diff-ntitle' => 'background-color: #fff; color: #222; text-align: center;',
240 'diff-addedline' => 'color: #222; font-size: 88%; border-style: solid; '
241 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
242 . 'vertical-align: top; white-space: pre-wrap;',
243 'diff-deletedline' => 'color: #222; font-size: 88%; border-style: solid; '
244 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
245 . 'vertical-align: top; white-space: pre-wrap;',
246 'diff-context' => 'background-color: #f8f9fa; color: #222; font-size: 88%; '
247 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
248 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
249 'diffchange' => 'font-weight: bold; text-decoration: none;',
250 ];
251
252 foreach ( $styles as $class => $style ) {
253 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
254 "\\1style=\"$style\"\\3", $text );
255 }
256
257 return $text;
258 }
259
260}
$wgFeedDiffCutoff
When generating Recentchanges RSS/Atom feed, diffs will not be generated for pages larger than this s...
$wgFeedClasses
Available feeds objects.
$wgFeed
Provide syndication feeds (RSS, Atom) for, e.g., Recentchanges, Newpages.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
$wgUser
Definition Setup.php:902
$wgOut
Definition Setup.php:912
if(! $wgDBerrorLogTZ) $wgRequest
Definition Setup.php:737
Helper functions for feeds.
Definition FeedUtils.php:29
static formatDiff( $row)
Format a diff for the newsfeed.
Definition FeedUtils.php:79
static checkFeedOutput( $type)
Check whether feeds can be used and that $type is a valid feed type.
Definition FeedUtils.php:56
static formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='')
Really format a diff for the newsfeed.
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
static applyDiffStyle( $text)
Hacky application of diff styles for the feeds.
static getDiffLink(Title $title, $newid, $oldid=null)
Generates a diff link.
static formatComment( $comment, $title=null, $local=false, $wikiId=null)
This function is called by all recent changes variants, by the page history, and by the user contribu...
Definition Linker.php:1109
static newFromRow( $row)
Handy shortcut for constructing a formatter directly from database row.
static getMain()
Get the RequestContext object associated with the main request.
Content object implementation for representing flat text.
Represents a title within MediaWiki.
Definition Title.php:39
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:53
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition design.txt:18
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
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
const RC_LOG
Definition Defines.php:154
the array() calling protocol came about after MediaWiki 1.4rc1.
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 you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2811
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:964
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;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:2013
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1777
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:37
$cache
Definition mcc.php:33