3 * Helper functions for feeds.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * Helper functions for feeds
33 * Check whether feed's cache should be cleared; for changes feeds
34 * If the feed should be purged; $timekey and $key will be removed from cache
36 * @param string $timekey Cache key of the timestamp of the last item
37 * @param string $key Cache key of feed's content
39 public static function checkPurge( $timekey, $key ) {
40 global $wgRequest, $wgUser;
42 $purge = $wgRequest->getVal( 'action' ) === 'purge';
43 // Allow users with 'purge' right to clear feed caches
44 if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
45 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
46 $cache->delete( $timekey, 1 );
47 $cache->delete( $key, 1 );
52 * Check whether feeds can be used and that $type is a valid feed type
54 * @param string $type Feed type, as requested by the user
57 public static function checkFeedOutput( $type ) {
58 global $wgOut, $wgFeed, $wgFeedClasses;
61 $wgOut->addWikiMsg( 'feed-unavailable' );
65 if ( !isset( $wgFeedClasses[$type] ) ) {
66 $wgOut->addWikiMsg( 'feed-invalid' );
74 * Format a diff for the newsfeed
76 * @param object $row Row from the recentchanges table, including fields as
77 * appropriate for CommentStore
80 public static function formatDiff( $row ) {
81 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
82 $timestamp = wfTimestamp( TS_MW
, $row->rc_timestamp
);
84 if ( $row->rc_type
== RC_LOG
) {
85 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
86 $actiontext = LogFormatter
::newFromRow( $rcRow )->getActionText();
88 return self
::formatDiffRow( $titleObj,
89 $row->rc_last_oldid
, $row->rc_this_oldid
,
91 $row->rc_deleted
& Revision
::DELETED_COMMENT
92 ?
wfMessage( 'rev-deleted-comment' )->escaped()
93 : CommentStore
::getStore()->getComment( 'rc_comment', $row )->text
,
99 * Really format a diff for the newsfeed
101 * @param Title $title
102 * @param int $oldid Old revision's id
103 * @param int $newid New revision's id
104 * @param int $timestamp New revision's timestamp
105 * @param string $comment New revision's comment
106 * @param string $actiontext Text of the action; in case of log event
109 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
110 $comment, $actiontext = ''
112 global $wgFeedDiffCutoff, $wgLang;
115 $completeText = '<p>' . implode( ' ',
119 Linker
::formatComment( $comment ) ] ) ) . "</p>\n";
121 // NOTE: Check permissions for anonymous users, not current user.
122 // No "privileged" version should end up in the cache.
123 // Most feed readers will not log in anyway.
125 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
127 // Can't diff special pages, unreadable pages or pages with no new revision
128 // to compare against: just return the text.
129 if ( $title->getNamespace() < 0 ||
$accErrors ||
!$newid ) {
130 return $completeText;
135 // Don't bother generating the diff if we won't be able to show it
136 if ( $wgFeedDiffCutoff > 0 ) {
137 $rev = Revision
::newFromId( $oldid );
142 $context = clone RequestContext
::getMain();
143 $context->setTitle( $title );
145 $contentHandler = $rev->getContentHandler();
146 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
147 $diffText = $de->getDiff(
148 wfMessage( 'previousrevision' )->text(), // hack
149 wfMessage( 'revisionasof',
150 $wgLang->timeanddate( $timestamp ),
151 $wgLang->date( $timestamp ),
152 $wgLang->time( $timestamp ) )->text() );
156 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
158 $diffText = self
::getDiffLink( $title, $newid, $oldid );
159 } elseif ( $diffText === false ) {
160 // Error in diff engine, probably a missing revision
161 $diffText = "<p>Can't load revision $newid</p>";
163 // Diff output fine, clean up any illegal UTF-8
164 $diffText = UtfNormal\Validator
::cleanUp( $diffText );
165 $diffText = self
::applyDiffStyle( $diffText );
168 $rev = Revision
::newFromId( $newid );
169 if ( $wgFeedDiffCutoff <= 0 ||
is_null( $rev ) ) {
170 $newContent = ContentHandler
::getForTitle( $title )->makeEmptyContent();
172 $newContent = $rev->getContent();
175 if ( $newContent instanceof TextContent
) {
176 // only textual content has a "source view".
177 $text = $newContent->getText();
179 if ( $wgFeedDiffCutoff <= 0 ||
strlen( $text ) > $wgFeedDiffCutoff ) {
182 $html = nl2br( htmlspecialchars( $text ) );
185 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
186 // contain JS magic and generally may not be suitable for inclusion in a feed.
187 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
188 // Compare also ApiFeedContributions::feedItemDesc
192 if ( $html === null ) {
193 // Omit large new page diffs, T31110
194 // Also use diff link for non-textual content
195 $diffText = self
::getDiffLink( $title, $newid );
197 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
198 '<div>' . $html . '</div>';
201 $completeText .= $diffText;
203 return $completeText;
207 * Generates a diff link. Used when the full diff is not wanted for example
208 * when $wgFeedDiffCutoff is 0.
210 * @param Title $title Title object: used to generate the diff URL
211 * @param int $newid Newid for this diff
212 * @param int|null $oldid Oldid for the diff. Null means it is a new article
215 protected static function getDiffLink( Title
$title, $newid, $oldid = null ) {
216 $queryParameters = [ 'diff' => $newid ];
217 if ( $oldid != null ) {
218 $queryParameters['oldid'] = $oldid;
220 $diffUrl = $title->getFullURL( $queryParameters );
222 $diffLink = Html
::element( 'a', [ 'href' => $diffUrl ],
223 wfMessage( 'showdiff' )->inContentLanguage()->text() );
229 * Hacky application of diff styles for the feeds.
230 * Might be 'cleaner' to use DOM or XSLT or something,
231 * but *gack* it's a pain in the ass.
233 * @param string $text Diff's HTML output
234 * @return string Modified HTML
236 public static function applyDiffStyle( $text ) {
238 'diff' => 'background-color: #fff; color: #222;',
239 'diff-otitle' => 'background-color: #fff; color: #222; text-align: center;',
240 'diff-ntitle' => 'background-color: #fff; color: #222; text-align: center;',
241 'diff-addedline' => 'color: #222; font-size: 88%; border-style: solid; '
242 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
243 . 'vertical-align: top; white-space: pre-wrap;',
244 'diff-deletedline' => 'color: #222; font-size: 88%; border-style: solid; '
245 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
246 . 'vertical-align: top; white-space: pre-wrap;',
247 'diff-context' => 'background-color: #f8f9fa; color: #222; font-size: 88%; '
248 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
249 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
250 'diffchange' => 'font-weight: bold; text-decoration: none;',
253 foreach ( $styles as $class => $style ) {
254 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
255 "\\1style=\"$style\"\\3", $text );