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
24 use MediaWiki\Revision\RevisionRecord
;
27 * Helper functions for feeds
34 * Check whether feeds can be used and that $type is a valid feed type
36 * @param string $type Feed type, as requested by the user
39 public static function checkFeedOutput( $type ) {
40 global $wgOut, $wgFeed, $wgFeedClasses;
43 $wgOut->addWikiMsg( 'feed-unavailable' );
47 if ( !isset( $wgFeedClasses[$type] ) ) {
48 $wgOut->addWikiMsg( 'feed-invalid' );
56 * Format a diff for the newsfeed
58 * @param object $row Row from the recentchanges table, including fields as
59 * appropriate for CommentStore
62 public static function formatDiff( $row ) {
63 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
64 $timestamp = wfTimestamp( TS_MW
, $row->rc_timestamp
);
66 if ( $row->rc_type
== RC_LOG
) {
67 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
68 $actiontext = LogFormatter
::newFromRow( $rcRow )->getActionText();
70 return self
::formatDiffRow( $titleObj,
71 $row->rc_last_oldid
, $row->rc_this_oldid
,
73 $row->rc_deleted
& RevisionRecord
::DELETED_COMMENT
74 ?
wfMessage( 'rev-deleted-comment' )->escaped()
75 : CommentStore
::getStore()->getComment( 'rc_comment', $row )->text
,
81 * Really format a diff for the newsfeed
84 * @param int $oldid Old revision's id
85 * @param int $newid New revision's id
86 * @param int $timestamp New revision's timestamp
87 * @param string $comment New revision's comment
88 * @param string $actiontext Text of the action; in case of log event
91 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
92 $comment, $actiontext = ''
94 global $wgFeedDiffCutoff, $wgLang;
97 $completeText = '<p>' . implode( ' ',
101 Linker
::formatComment( $comment ) ] ) ) . "</p>\n";
103 // NOTE: Check permissions for anonymous users, not current user.
104 // No "privileged" version should end up in the cache.
105 // Most feed readers will not log in anyway.
107 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
109 // Can't diff special pages, unreadable pages or pages with no new revision
110 // to compare against: just return the text.
111 if ( $title->getNamespace() < 0 ||
$accErrors ||
!$newid ) {
112 return $completeText;
117 // Don't bother generating the diff if we won't be able to show it
118 if ( $wgFeedDiffCutoff > 0 ) {
119 $rev = Revision
::newFromId( $oldid );
124 $context = clone RequestContext
::getMain();
125 $context->setTitle( $title );
127 $contentHandler = $rev->getContentHandler();
128 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
129 $diffText = $de->getDiff(
130 wfMessage( 'previousrevision' )->text(), // hack
131 wfMessage( 'revisionasof',
132 $wgLang->timeanddate( $timestamp ),
133 $wgLang->date( $timestamp ),
134 $wgLang->time( $timestamp ) )->text() );
138 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
140 $diffText = self
::getDiffLink( $title, $newid, $oldid );
141 } elseif ( $diffText === false ) {
142 // Error in diff engine, probably a missing revision
143 $diffText = "<p>Can't load revision $newid</p>";
145 // Diff output fine, clean up any illegal UTF-8
146 $diffText = UtfNormal\Validator
::cleanUp( $diffText );
147 $diffText = self
::applyDiffStyle( $diffText );
150 $rev = Revision
::newFromId( $newid );
151 if ( $wgFeedDiffCutoff <= 0 ||
is_null( $rev ) ) {
152 $newContent = ContentHandler
::getForTitle( $title )->makeEmptyContent();
154 $newContent = $rev->getContent();
157 if ( $newContent instanceof TextContent
) {
158 // only textual content has a "source view".
159 $text = $newContent->getText();
161 if ( $wgFeedDiffCutoff <= 0 ||
strlen( $text ) > $wgFeedDiffCutoff ) {
164 $html = nl2br( htmlspecialchars( $text ) );
167 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
168 // contain JS magic and generally may not be suitable for inclusion in a feed.
169 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
170 // Compare also ApiFeedContributions::feedItemDesc
174 if ( $html === null ) {
175 // Omit large new page diffs, T31110
176 // Also use diff link for non-textual content
177 $diffText = self
::getDiffLink( $title, $newid );
179 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
180 '<div>' . $html . '</div>';
183 $completeText .= $diffText;
185 return $completeText;
189 * Generates a diff link. Used when the full diff is not wanted for example
190 * when $wgFeedDiffCutoff is 0.
192 * @param Title $title Title object: used to generate the diff URL
193 * @param int $newid Newid for this diff
194 * @param int|null $oldid Oldid for the diff. Null means it is a new article
197 protected static function getDiffLink( Title
$title, $newid, $oldid = null ) {
198 $queryParameters = [ 'diff' => $newid ];
199 if ( $oldid != null ) {
200 $queryParameters['oldid'] = $oldid;
202 $diffUrl = $title->getFullURL( $queryParameters );
204 $diffLink = Html
::element( 'a', [ 'href' => $diffUrl ],
205 wfMessage( 'showdiff' )->inContentLanguage()->text() );
211 * Hacky application of diff styles for the feeds.
212 * Might be 'cleaner' to use DOM or XSLT or something,
213 * but *gack* it's a pain in the ass.
215 * @param string $text Diff's HTML output
216 * @return string Modified HTML
218 public static function applyDiffStyle( $text ) {
220 'diff' => 'background-color: #fff; color: #222;',
221 'diff-otitle' => 'background-color: #fff; color: #222; text-align: center;',
222 'diff-ntitle' => 'background-color: #fff; color: #222; text-align: center;',
223 'diff-addedline' => 'color: #222; font-size: 88%; border-style: solid; '
224 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
225 . 'vertical-align: top; white-space: pre-wrap;',
226 'diff-deletedline' => 'color: #222; font-size: 88%; border-style: solid; '
227 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
228 . 'vertical-align: top; white-space: pre-wrap;',
229 'diff-context' => 'background-color: #f8f9fa; color: #222; font-size: 88%; '
230 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
231 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
232 'diffchange' => 'font-weight: bold; text-decoration: none;',
235 foreach ( $styles as $class => $style ) {
236 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
237 "\\1style=\"$style\"\\3", $text );