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 feed's cache should be cleared; for changes feeds
33 * If the feed should be purged; $timekey and $key will be removed from
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, $messageMemc;
41 $purge = $wgRequest->getVal( 'action' ) === 'purge';
42 if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
43 $messageMemc->delete( $timekey );
44 $messageMemc->delete( $key );
49 * Check whether feeds can be used and that $type is a valid feed type
51 * @param string $type Feed type, as requested by the user
54 public static function checkFeedOutput( $type ) {
55 global $wgOut, $wgFeed, $wgFeedClasses;
58 $wgOut->addWikiMsg( 'feed-unavailable' );
62 if ( !isset( $wgFeedClasses[$type] ) ) {
63 $wgOut->addWikiMsg( 'feed-invalid' );
71 * Format a diff for the newsfeed
73 * @param object $row Row from the recentchanges table
76 public static function formatDiff( $row ) {
77 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
78 $timestamp = wfTimestamp( TS_MW
, $row->rc_timestamp
);
80 if ( $row->rc_type
== RC_LOG
) {
81 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
82 $actiontext = LogFormatter
::newFromRow( $rcRow )->getActionText();
84 return self
::formatDiffRow( $titleObj,
85 $row->rc_last_oldid
, $row->rc_this_oldid
,
87 $row->rc_deleted
& Revision
::DELETED_COMMENT
88 ?
wfMessage( 'rev-deleted-comment' )->escaped()
95 * Really format a diff for the newsfeed
97 * @param Title $title Title object
98 * @param int $oldid Old revision's id
99 * @param int $newid New revision's id
100 * @param int $timestamp New revision's timestamp
101 * @param string $comment New revision's comment
102 * @param string $actiontext Text of the action; in case of log event
105 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
106 $comment, $actiontext = ''
108 global $wgFeedDiffCutoff, $wgLang;
109 wfProfileIn( __METHOD__
);
112 $completeText = '<p>' . implode( ' ',
116 Linker
::formatComment( $comment ) ) ) ) . "</p>\n";
118 // NOTE: Check permissions for anonymous users, not current user.
119 // No "privileged" version should end up in the cache.
120 // Most feed readers will not log in anyway.
122 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
124 // Can't diff special pages, unreadable pages or pages with no new revision
125 // to compare against: just return the text.
126 if ( $title->getNamespace() < 0 ||
$accErrors ||
!$newid ) {
127 wfProfileOut( __METHOD__
);
128 return $completeText;
132 wfProfileIn( __METHOD__
. "-dodiff" );
134 #$diffText = $de->getDiff( wfMessage( 'revisionasof',
135 # $wgLang->timeanddate( $timestamp ),
136 # $wgLang->date( $timestamp ),
137 # $wgLang->time( $timestamp ) )->text(),
138 # wfMessage( 'currentrev' )->text() );
141 // Don't bother generating the diff if we won't be able to show it
142 if ( $wgFeedDiffCutoff > 0 ) {
143 $rev = Revision
::newFromId( $oldid );
148 $context = clone RequestContext
::getMain();
149 $context->setTitle( $title );
151 $contentHandler = $rev->getContentHandler();
152 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
153 $diffText = $de->getDiff(
154 wfMessage( 'previousrevision' )->text(), // hack
155 wfMessage( 'revisionasof',
156 $wgLang->timeanddate( $timestamp ),
157 $wgLang->date( $timestamp ),
158 $wgLang->time( $timestamp ) )->text() );
162 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
164 $diffText = self
::getDiffLink( $title, $newid, $oldid );
165 } elseif ( $diffText === false ) {
166 // Error in diff engine, probably a missing revision
167 $diffText = "<p>Can't load revision $newid</p>";
169 // Diff output fine, clean up any illegal UTF-8
170 $diffText = UtfNormal
::cleanUp( $diffText );
171 $diffText = self
::applyDiffStyle( $diffText );
173 wfProfileOut( __METHOD__
. "-dodiff" );
175 $rev = Revision
::newFromId( $newid );
176 if ( $wgFeedDiffCutoff <= 0 ||
is_null( $rev ) ) {
177 $newContent = ContentHandler
::getForTitle( $title )->makeEmptyContent();
179 $newContent = $rev->getContent();
182 if ( $newContent instanceof TextContent
) {
183 // only textual content has a "source view".
184 $text = $newContent->getNativeData();
186 if ( $wgFeedDiffCutoff <= 0 ||
strlen( $text ) > $wgFeedDiffCutoff ) {
189 $html = nl2br( htmlspecialchars( $text ) );
192 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
193 // contain JS magic and generally may not be suitable for inclusion in a feed.
194 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
195 //Compare also ApiFeedContributions::feedItemDesc
199 if ( $html === null ) {
201 // Omit large new page diffs, bug 29110
202 // Also use diff link for non-textual content
203 $diffText = self
::getDiffLink( $title, $newid );
205 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
206 '<div>' . $html . '</div>';
209 $completeText .= $diffText;
211 wfProfileOut( __METHOD__
);
212 return $completeText;
216 * Generates a diff link. Used when the full diff is not wanted for example
217 * when $wgFeedDiffCutoff is 0.
219 * @param Title $title Title object: used to generate the diff URL
220 * @param int $newid Newid for this diff
221 * @param int|null $oldid Oldid for the diff. Null means it is a new article
224 protected static function getDiffLink( Title
$title, $newid, $oldid = null ) {
225 $queryParameters = array( 'diff' => $newid );
226 if ( $oldid != null ) {
227 $queryParameters['oldid'] = $oldid;
229 $diffUrl = $title->getFullURL( $queryParameters );
231 $diffLink = Html
::element( 'a', array( 'href' => $diffUrl ),
232 wfMessage( 'showdiff' )->inContentLanguage()->text() );
238 * Hacky application of diff styles for the feeds.
239 * Might be 'cleaner' to use DOM or XSLT or something,
240 * but *gack* it's a pain in the ass.
242 * @param string $text Diff's HTML output
243 * @return string Modified HTML
245 public static function applyDiffStyle( $text ) {
247 'diff' => 'background-color: white; color:black;',
248 'diff-otitle' => 'background-color: white; color:black; text-align: center;',
249 'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
250 'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; '
251 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
252 . 'vertical-align: top; white-space: pre-wrap;',
253 'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; '
254 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
255 . 'vertical-align: top; white-space: pre-wrap;',
256 'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; '
257 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
258 . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
259 'diffchange' => 'font-weight: bold; text-decoration: none;',
262 foreach ( $styles as $class => $style ) {
263 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
264 "\\1style=\"$style\"\\3", $text );