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;
111 $completeText = '<p>' . implode( ' ',
115 Linker
::formatComment( $comment ) ) ) ) . "</p>\n";
117 // NOTE: Check permissions for anonymous users, not current user.
118 // No "privileged" version should end up in the cache.
119 // Most feed readers will not log in anyway.
121 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
123 // Can't diff special pages, unreadable pages or pages with no new revision
124 // to compare against: just return the text.
125 if ( $title->getNamespace() < 0 ||
$accErrors ||
!$newid ) {
126 return $completeText;
131 #$diffText = $de->getDiff( wfMessage( 'revisionasof',
132 # $wgLang->timeanddate( $timestamp ),
133 # $wgLang->date( $timestamp ),
134 # $wgLang->time( $timestamp ) )->text(),
135 # wfMessage( 'currentrev' )->text() );
138 // Don't bother generating the diff if we won't be able to show it
139 if ( $wgFeedDiffCutoff > 0 ) {
140 $rev = Revision
::newFromId( $oldid );
145 $context = clone RequestContext
::getMain();
146 $context->setTitle( $title );
148 $contentHandler = $rev->getContentHandler();
149 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
150 $diffText = $de->getDiff(
151 wfMessage( 'previousrevision' )->text(), // hack
152 wfMessage( 'revisionasof',
153 $wgLang->timeanddate( $timestamp ),
154 $wgLang->date( $timestamp ),
155 $wgLang->time( $timestamp ) )->text() );
159 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
161 $diffText = self
::getDiffLink( $title, $newid, $oldid );
162 } elseif ( $diffText === false ) {
163 // Error in diff engine, probably a missing revision
164 $diffText = "<p>Can't load revision $newid</p>";
166 // Diff output fine, clean up any illegal UTF-8
167 $diffText = UtfNormal
::cleanUp( $diffText );
168 $diffText = self
::applyDiffStyle( $diffText );
171 $rev = Revision
::newFromId( $newid );
172 if ( $wgFeedDiffCutoff <= 0 ||
is_null( $rev ) ) {
173 $newContent = ContentHandler
::getForTitle( $title )->makeEmptyContent();
175 $newContent = $rev->getContent();
178 if ( $newContent instanceof TextContent
) {
179 // only textual content has a "source view".
180 $text = $newContent->getNativeData();
182 if ( $wgFeedDiffCutoff <= 0 ||
strlen( $text ) > $wgFeedDiffCutoff ) {
185 $html = nl2br( htmlspecialchars( $text ) );
188 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
189 // contain JS magic and generally may not be suitable for inclusion in a feed.
190 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
191 //Compare also ApiFeedContributions::feedItemDesc
195 if ( $html === null ) {
197 // Omit large new page diffs, bug 29110
198 // Also use diff link for non-textual content
199 $diffText = self
::getDiffLink( $title, $newid );
201 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
202 '<div>' . $html . '</div>';
205 $completeText .= $diffText;
207 return $completeText;
211 * Generates a diff link. Used when the full diff is not wanted for example
212 * when $wgFeedDiffCutoff is 0.
214 * @param Title $title Title object: used to generate the diff URL
215 * @param int $newid Newid for this diff
216 * @param int|null $oldid Oldid for the diff. Null means it is a new article
219 protected static function getDiffLink( Title
$title, $newid, $oldid = null ) {
220 $queryParameters = array( 'diff' => $newid );
221 if ( $oldid != null ) {
222 $queryParameters['oldid'] = $oldid;
224 $diffUrl = $title->getFullURL( $queryParameters );
226 $diffLink = Html
::element( 'a', array( 'href' => $diffUrl ),
227 wfMessage( 'showdiff' )->inContentLanguage()->text() );
233 * Hacky application of diff styles for the feeds.
234 * Might be 'cleaner' to use DOM or XSLT or something,
235 * but *gack* it's a pain in the ass.
237 * @param string $text Diff's HTML output
238 * @return string Modified HTML
240 public static function applyDiffStyle( $text ) {
242 'diff' => 'background-color: white; color:black;',
243 'diff-otitle' => 'background-color: white; color:black; text-align: center;',
244 'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
245 'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; '
246 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
247 . 'vertical-align: top; white-space: pre-wrap;',
248 'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; '
249 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
250 . 'vertical-align: top; white-space: pre-wrap;',
251 'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; '
252 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
253 . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
254 'diffchange' => 'font-weight: bold; text-decoration: none;',
257 foreach ( $styles as $class => $style ) {
258 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
259 "\\1style=\"$style\"\\3", $text );