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
25 * Helper functions for feeds
32 * Check whether feeds can be used and that $type is a valid feed type
34 * @param string $type Feed type, as requested by the user
37 public static function checkFeedOutput( $type ) {
38 global $wgOut, $wgFeed, $wgFeedClasses;
41 $wgOut->addWikiMsg( 'feed-unavailable' );
45 if ( !isset( $wgFeedClasses[$type] ) ) {
46 $wgOut->addWikiMsg( 'feed-invalid' );
54 * Format a diff for the newsfeed
56 * @param object $row Row from the recentchanges table, including fields as
57 * appropriate for CommentStore
60 public static function formatDiff( $row ) {
61 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
62 $timestamp = wfTimestamp( TS_MW
, $row->rc_timestamp
);
64 if ( $row->rc_type
== RC_LOG
) {
65 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
66 $actiontext = LogFormatter
::newFromRow( $rcRow )->getActionText();
68 return self
::formatDiffRow( $titleObj,
69 $row->rc_last_oldid
, $row->rc_this_oldid
,
71 $row->rc_deleted
& Revision
::DELETED_COMMENT
72 ?
wfMessage( 'rev-deleted-comment' )->escaped()
73 : CommentStore
::getStore()->getComment( 'rc_comment', $row )->text
,
79 * Really format a diff for the newsfeed
82 * @param int $oldid Old revision's id
83 * @param int $newid New revision's id
84 * @param int $timestamp New revision's timestamp
85 * @param string $comment New revision's comment
86 * @param string $actiontext Text of the action; in case of log event
89 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
90 $comment, $actiontext = ''
92 global $wgFeedDiffCutoff, $wgLang;
95 $completeText = '<p>' . implode( ' ',
99 Linker
::formatComment( $comment ) ] ) ) . "</p>\n";
101 // NOTE: Check permissions for anonymous users, not current user.
102 // No "privileged" version should end up in the cache.
103 // Most feed readers will not log in anyway.
105 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
107 // Can't diff special pages, unreadable pages or pages with no new revision
108 // to compare against: just return the text.
109 if ( $title->getNamespace() < 0 ||
$accErrors ||
!$newid ) {
110 return $completeText;
115 // Don't bother generating the diff if we won't be able to show it
116 if ( $wgFeedDiffCutoff > 0 ) {
117 $rev = Revision
::newFromId( $oldid );
122 $context = clone RequestContext
::getMain();
123 $context->setTitle( $title );
125 $contentHandler = $rev->getContentHandler();
126 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
127 $diffText = $de->getDiff(
128 wfMessage( 'previousrevision' )->text(), // hack
129 wfMessage( 'revisionasof',
130 $wgLang->timeanddate( $timestamp ),
131 $wgLang->date( $timestamp ),
132 $wgLang->time( $timestamp ) )->text() );
136 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
138 $diffText = self
::getDiffLink( $title, $newid, $oldid );
139 } elseif ( $diffText === false ) {
140 // Error in diff engine, probably a missing revision
141 $diffText = "<p>Can't load revision $newid</p>";
143 // Diff output fine, clean up any illegal UTF-8
144 $diffText = UtfNormal\Validator
::cleanUp( $diffText );
145 $diffText = self
::applyDiffStyle( $diffText );
148 $rev = Revision
::newFromId( $newid );
149 if ( $wgFeedDiffCutoff <= 0 ||
is_null( $rev ) ) {
150 $newContent = ContentHandler
::getForTitle( $title )->makeEmptyContent();
152 $newContent = $rev->getContent();
155 if ( $newContent instanceof TextContent
) {
156 // only textual content has a "source view".
157 $text = $newContent->getText();
159 if ( $wgFeedDiffCutoff <= 0 ||
strlen( $text ) > $wgFeedDiffCutoff ) {
162 $html = nl2br( htmlspecialchars( $text ) );
165 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
166 // contain JS magic and generally may not be suitable for inclusion in a feed.
167 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
168 // Compare also ApiFeedContributions::feedItemDesc
172 if ( $html === null ) {
173 // Omit large new page diffs, T31110
174 // Also use diff link for non-textual content
175 $diffText = self
::getDiffLink( $title, $newid );
177 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
178 '<div>' . $html . '</div>';
181 $completeText .= $diffText;
183 return $completeText;
187 * Generates a diff link. Used when the full diff is not wanted for example
188 * when $wgFeedDiffCutoff is 0.
190 * @param Title $title Title object: used to generate the diff URL
191 * @param int $newid Newid for this diff
192 * @param int|null $oldid Oldid for the diff. Null means it is a new article
195 protected static function getDiffLink( Title
$title, $newid, $oldid = null ) {
196 $queryParameters = [ 'diff' => $newid ];
197 if ( $oldid != null ) {
198 $queryParameters['oldid'] = $oldid;
200 $diffUrl = $title->getFullURL( $queryParameters );
202 $diffLink = Html
::element( 'a', [ 'href' => $diffUrl ],
203 wfMessage( 'showdiff' )->inContentLanguage()->text() );
209 * Hacky application of diff styles for the feeds.
210 * Might be 'cleaner' to use DOM or XSLT or something,
211 * but *gack* it's a pain in the ass.
213 * @param string $text Diff's HTML output
214 * @return string Modified HTML
216 public static function applyDiffStyle( $text ) {
218 'diff' => 'background-color: #fff; color: #222;',
219 'diff-otitle' => 'background-color: #fff; color: #222; text-align: center;',
220 'diff-ntitle' => 'background-color: #fff; color: #222; text-align: center;',
221 'diff-addedline' => 'color: #222; font-size: 88%; border-style: solid; '
222 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
223 . 'vertical-align: top; white-space: pre-wrap;',
224 'diff-deletedline' => 'color: #222; font-size: 88%; border-style: solid; '
225 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
226 . 'vertical-align: top; white-space: pre-wrap;',
227 'diff-context' => 'background-color: #f8f9fa; color: #222; font-size: 88%; '
228 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
229 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
230 'diffchange' => 'font-weight: bold; text-decoration: none;',
233 foreach ( $styles as $class => $style ) {
234 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
235 "\\1style=\"$style\"\\3", $text );