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 # Early exist when the page is not an article, on errors and no newid to
107 if( $title->getNamespace() < 0 ||
$accErrors ||
!$newid ) {
108 wfProfileOut( __METHOD__
);
109 return $completeText;
113 wfProfileIn( __METHOD__
."-dodiff" );
115 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
116 # $wgLang->timeanddate( $timestamp ),
117 # $wgLang->date( $timestamp ),
118 # $wgLang->time( $timestamp ) ),
119 # wfMsg( 'currentrev' ) );
121 // Don't bother generating the diff if we won't be able to show it
122 if ( $wgFeedDiffCutoff > 0 ) {
123 $de = new DifferenceEngine( $title, $oldid, $newid );
124 $diffText = $de->getDiff(
125 wfMsg( 'previousrevision' ), // hack
126 wfMsg( 'revisionasof',
127 $wgLang->timeanddate( $timestamp ),
128 $wgLang->date( $timestamp ),
129 $wgLang->time( $timestamp ) ) );
132 if ( $wgFeedDiffCutoff <= 0 ||
( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
134 $diffText = self
::getDiffText( $title, $newid, $oldid);
135 } elseif ( $diffText === false ) {
136 // Error in diff engine, probably a missing revision
137 $diffText = "<p>Can't load revision $newid</p>";
139 // Diff output fine, clean up any illegal UTF-8
140 $diffText = UtfNormal
::cleanUp( $diffText );
141 $diffText = self
::applyDiffStyle( $diffText );
143 wfProfileOut( __METHOD__
."-dodiff" );
145 $rev = Revision
::newFromId( $newid );
146 if( $wgFeedDiffCutoff <= 0 ||
is_null( $rev ) ) {
149 $newtext = $rev->getText();
151 if ( $wgFeedDiffCutoff <= 0 ||
strlen( $newtext ) > $wgFeedDiffCutoff ) {
152 // Omit large new page diffs, bug 29110
153 $diffText = self
::getDiffText( $title, $newid );
155 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
156 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
159 $completeText .= $diffText;
161 wfProfileOut( __METHOD__
);
162 return $completeText;
166 * Generates a diff link. Used when the full diff is not wanted for example
167 * when $wgFeedDiffCutoff is 0.
169 * @param $title Title object: used to generate the diff URL
170 * @param $newid Integer newid for this diff
171 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
173 protected static function getDiffText( Title
$title, $newid, $oldid = null ) {
174 $queryParameters = ($oldid == null)
176 : "diff={$newid}&oldid={$oldid}" ;
177 $diffLink = $title->escapeFullUrl( $queryParameters );
179 $diffText = Html
::RawElement( 'a', array( 'href' => $diffLink ),
180 htmlspecialchars( wfMsgForContent( 'showdiff' ) )
187 * Hacky application of diff styles for the feeds.
188 * Might be 'cleaner' to use DOM or XSLT or something,
189 * but *gack* it's a pain in the ass.
191 * @param $text String: diff's HTML output
192 * @return String: modified HTML
194 public static function applyDiffStyle( $text ) {
196 'diff' => 'background-color: white; color:black;',
197 'diff-otitle' => 'background-color: white; color:black;',
198 'diff-ntitle' => 'background-color: white; color:black;',
199 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
200 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
201 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
202 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
205 foreach( $styles as $class => $style ) {
206 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
207 "\\1style=\"$style\"\\3", $text );