4 * Helper functions for feeds
11 * Check whether feed's cache should be cleared; for changes feeds
12 * If the feed should be purged; $timekey and $key will be removed from
15 * @param $timekey String: cache key of the timestamp of the last item
16 * @param $key String: cache key of feed's content
18 public static function checkPurge( $timekey, $key ) {
19 global $wgRequest, $wgUser, $messageMemc;
20 $purge = $wgRequest->getVal( 'action' ) === 'purge';
21 if ( $purge && $wgUser->isAllowed('purge') ) {
22 $messageMemc->delete( $timekey );
23 $messageMemc->delete( $key );
28 * Check whether feeds can be used and that $type is a valid feed type
30 * @param $type String: feed type, as requested by the user
33 public static function checkFeedOutput( $type ) {
34 global $wgOut, $wgFeed, $wgFeedClasses;
37 $wgOut->addWikiMsg( 'feed-unavailable' );
41 if( !isset( $wgFeedClasses[$type] ) ) {
42 $wgOut->addWikiMsg( 'feed-invalid' );
50 * Format a diff for the newsfeed
52 * @param $row Object: row from the recentchanges table
55 public static function formatDiff( $row ) {
58 $titleObj = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
59 $timestamp = wfTimestamp( TS_MW
, $row->rc_timestamp
);
61 if( $row->rc_type
== RC_LOG
) {
62 if( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
63 $actiontext = wfMsgHtml('rev-deleted-event');
65 $actiontext = LogPage
::actionText( $row->rc_log_type
, $row->rc_log_action
,
66 $titleObj, $wgUser->getSkin(), LogPage
::extractParams($row->rc_params
,true,true) );
69 return self
::formatDiffRow( $titleObj,
70 $row->rc_last_oldid
, $row->rc_this_oldid
,
72 ($row->rc_deleted
& Revision
::DELETED_COMMENT
) ?
wfMsgHtml('rev-deleted-comment') : $row->rc_comment
,
77 * Really format a diff for the newsfeed
79 * @param $title Title object
80 * @param $oldid Integer: old revision's id
81 * @param $newid Integer: new revision's id
82 * @param $timestamp Integer: new revision's timestamp
83 * @param $comment String: new revision's comment
84 * @param $actiontext String: text of the action; in case of log event
87 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
88 global $wgFeedDiffCutoff, $wgLang, $wgUser;
89 wfProfileIn( __METHOD__
);
91 $skin = $wgUser->getSkin();
93 $completeText = '<p>' . implode( ' ',
97 $skin->formatComment( $comment ) ) ) ) . "</p>\n";
99 //NOTE: Check permissions for anonymous users, not current user.
100 // No "privileged" version should end up in the cache.
101 // Most feed readers will not log in anway.
103 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
105 if( $title->getNamespace() >= 0 && !$accErrors && $newid ) {
107 wfProfileIn( __METHOD__
."-dodiff" );
109 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
110 # $wgLang->timeanddate( $timestamp ),
111 # $wgLang->date( $timestamp ),
112 # $wgLang->time( $timestamp ) ),
113 # wfMsg( 'currentrev' ) );
115 // Don't bother generating the diff if we won't be able to show it
116 if ( $wgFeedDiffCutoff > 0 ) {
117 $de = new DifferenceEngine( $title, $oldid, $newid );
118 $diffText = $de->getDiff(
119 wfMsg( 'previousrevision' ), // hack
120 wfMsg( 'revisionasof',
121 $wgLang->timeanddate( $timestamp ),
122 $wgLang->date( $timestamp ),
123 $wgLang->time( $timestamp ) ) );
126 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
128 $diffLink = $title->escapeFullUrl(
130 '&oldid=' . $oldid );
131 $diffText = '<a href="' .
134 htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
136 } elseif ( $diffText === false ) {
137 // Error in diff engine, probably a missing revision
138 $diffText = "<p>Can't load revision $newid</p>";
140 // Diff output fine, clean up any illegal UTF-8
141 $diffText = UtfNormal
::cleanUp( $diffText );
142 $diffText = self
::applyDiffStyle( $diffText );
144 wfProfileOut( __METHOD__
."-dodiff" );
146 $rev = Revision
::newFromId( $newid );
147 if( is_null( $rev ) ) {
150 $newtext = $rev->getText();
152 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
153 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
155 $completeText .= $diffText;
158 wfProfileOut( __METHOD__
);
159 return $completeText;
163 * Hacky application of diff styles for the feeds.
164 * Might be 'cleaner' to use DOM or XSLT or something,
165 * but *gack* it's a pain in the ass.
167 * @param $text String: diff's HTML output
168 * @return String: modified HTML
170 public static function applyDiffStyle( $text ) {
172 'diff' => 'background-color: white; color:black;',
173 'diff-otitle' => 'background-color: white; color:black;',
174 'diff-ntitle' => 'background-color: white; color:black;',
175 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
176 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
177 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
178 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
181 foreach( $styles as $class => $style ) {
182 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
183 "\\1style=\"$style\"\\3", $text );