re r106025 — apply follow up from Luca Fulchir
[lhc/web/wiklou.git] / includes / FeedUtils.php
1 <?php
2
3 /**
4 * Helper functions for feeds
5 *
6 * @ingroup Feed
7 */
8 class FeedUtils {
9
10 /**
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
13 * $messageMemc
14 *
15 * @param $timekey String: cache key of the timestamp of the last item
16 * @param $key String: cache key of feed's content
17 */
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 );
24 }
25 }
26
27 /**
28 * Check whether feeds can be used and that $type is a valid feed type
29 *
30 * @param $type String: feed type, as requested by the user
31 * @return Boolean
32 */
33 public static function checkFeedOutput( $type ) {
34 global $wgOut, $wgFeed, $wgFeedClasses;
35
36 if ( !$wgFeed ) {
37 $wgOut->addWikiMsg( 'feed-unavailable' );
38 return false;
39 }
40
41 if( !isset( $wgFeedClasses[$type] ) ) {
42 $wgOut->addWikiMsg( 'feed-invalid' );
43 return false;
44 }
45
46 return true;
47 }
48
49 /**
50 * Format a diff for the newsfeed
51 *
52 * @param $row Object: row from the recentchanges table
53 * @return String
54 */
55 public static function formatDiff( $row ) {
56 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
57 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
58 $actiontext = '';
59 if( $row->rc_type == RC_LOG ) {
60 if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
61 $actiontext = wfMsgHtml('rev-deleted-event');
62 } else {
63 $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
64 $titleObj, RequestContext::getMain()->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
65 }
66 }
67 return self::formatDiffRow( $titleObj,
68 $row->rc_last_oldid, $row->rc_this_oldid,
69 $timestamp,
70 ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
71 $actiontext );
72 }
73
74 /**
75 * Really format a diff for the newsfeed
76 *
77 * @param $title Title object
78 * @param $oldid Integer: old revision's id
79 * @param $newid Integer: new revision's id
80 * @param $timestamp Integer: new revision's timestamp
81 * @param $comment String: new revision's comment
82 * @param $actiontext String: text of the action; in case of log event
83 * @return String
84 */
85 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
86 global $wgFeedDiffCutoff, $wgLang;
87 wfProfileIn( __METHOD__ );
88
89 # log enties
90 $completeText = '<p>' . implode( ' ',
91 array_filter(
92 array(
93 $actiontext,
94 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
95
96 // NOTE: Check permissions for anonymous users, not current user.
97 // No "privileged" version should end up in the cache.
98 // Most feed readers will not log in anway.
99 $anon = new User();
100 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
101
102 // Can't diff special pages, unreadable pages or pages with no new revision
103 // to compare against: just return the text.
104 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
105 wfProfileOut( __METHOD__ );
106 return $completeText;
107 }
108
109 if( $oldid ) {
110 wfProfileIn( __METHOD__."-dodiff" );
111
112 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
113 # $wgLang->timeanddate( $timestamp ),
114 # $wgLang->date( $timestamp ),
115 # $wgLang->time( $timestamp ) ),
116 # wfMsg( 'currentrev' ) );
117
118 // Don't bother generating the diff if we won't be able to show it
119 if ( $wgFeedDiffCutoff > 0 ) {
120 $de = new DifferenceEngine( $title, $oldid, $newid );
121 $diffText = $de->getDiff(
122 wfMsg( 'previousrevision' ), // hack
123 wfMsg( 'revisionasof',
124 $wgLang->timeanddate( $timestamp ),
125 $wgLang->date( $timestamp ),
126 $wgLang->time( $timestamp ) ) );
127 }
128
129 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
130 // Omit large diffs
131 $diffText = self::getDiffLink( $title, $newid, $oldid );
132 } elseif ( $diffText === false ) {
133 // Error in diff engine, probably a missing revision
134 $diffText = "<p>Can't load revision $newid</p>";
135 } else {
136 // Diff output fine, clean up any illegal UTF-8
137 $diffText = UtfNormal::cleanUp( $diffText );
138 $diffText = self::applyDiffStyle( $diffText );
139 }
140 wfProfileOut( __METHOD__."-dodiff" );
141 } else {
142 $rev = Revision::newFromId( $newid );
143 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
144 $newtext = '';
145 } else {
146 $newtext = $rev->getText();
147 }
148 if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
149 // Omit large new page diffs, bug 29110
150 $diffText = self::getDiffLink( $title, $newid );
151 } else {
152 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
153 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
154 }
155 }
156 $completeText .= $diffText;
157
158 wfProfileOut( __METHOD__ );
159 return $completeText;
160 }
161
162 /**
163 * Generates a diff link. Used when the full diff is not wanted for example
164 * when $wgFeedDiffCutoff is 0.
165 *
166 * @param $title Title object: used to generate the diff URL
167 * @param $newid Integer newid for this diff
168 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
169 */
170 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
171 $queryParameters = ($oldid == null)
172 ? "diff={$newid}"
173 : "diff={$newid}&oldid={$oldid}" ;
174 $diffUrl = $title->getFullUrl( $queryParameters );
175
176 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
177 wfMsgForContent( 'showdiff' ) );
178
179 return $diffLink;
180 }
181
182 /**
183 * Hacky application of diff styles for the feeds.
184 * Might be 'cleaner' to use DOM or XSLT or something,
185 * but *gack* it's a pain in the ass.
186 *
187 * @param $text String: diff's HTML output
188 * @return String: modified HTML
189 */
190 public static function applyDiffStyle( $text ) {
191 $styles = array(
192 'diff' => 'background-color: white; color:black;',
193 'diff-otitle' => 'background-color: white; color:black;',
194 'diff-ntitle' => 'background-color: white; color:black;',
195 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
196 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
197 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
198 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
199 );
200
201 foreach( $styles as $class => $style ) {
202 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
203 "\\1style=\"$style\"\\3", $text );
204 }
205
206 return $text;
207 }
208
209 }