In WantedQueryPage:
[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 global $wgUser;
57
58 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
59 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
60 $actiontext = '';
61 if( $row->rc_type == RC_LOG ) {
62 if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
63 $actiontext = wfMsgHtml('rev-deleted-event');
64 } else {
65 $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
66 $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
67 }
68 }
69 return self::formatDiffRow( $titleObj,
70 $row->rc_last_oldid, $row->rc_this_oldid,
71 $timestamp,
72 ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
73 $actiontext );
74 }
75
76 /**
77 * Really format a diff for the newsfeed
78 *
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
85 * @return String
86 */
87 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
88 global $wgFeedDiffCutoff, $wgLang, $wgUser;
89 wfProfileIn( __METHOD__ );
90
91 $skin = $wgUser->getSkin();
92 # log enties
93 $completeText = '<p>' . implode( ' ',
94 array_filter(
95 array(
96 $actiontext,
97 $skin->formatComment( $comment ) ) ) ) . "</p>\n";
98
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.
102 $anon = new User();
103 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
104
105 if( $title->getNamespace() >= 0 && !$accErrors && $newid ) {
106 if( $oldid ) {
107 wfProfileIn( __METHOD__."-dodiff" );
108
109 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
110 # $wgLang->timeanddate( $timestamp ),
111 # $wgLang->date( $timestamp ),
112 # $wgLang->time( $timestamp ) ),
113 # wfMsg( 'currentrev' ) );
114
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 ) ) );
124 }
125
126 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
127 // Omit large diffs
128 $diffLink = $title->escapeFullUrl(
129 'diff=' . $newid .
130 '&oldid=' . $oldid );
131 $diffText = '<a href="' .
132 $diffLink .
133 '">' .
134 htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
135 '</a>';
136 } elseif ( $diffText === false ) {
137 // Error in diff engine, probably a missing revision
138 $diffText = "<p>Can't load revision $newid</p>";
139 } else {
140 // Diff output fine, clean up any illegal UTF-8
141 $diffText = UtfNormal::cleanUp( $diffText );
142 $diffText = self::applyDiffStyle( $diffText );
143 }
144 wfProfileOut( __METHOD__."-dodiff" );
145 } else {
146 $rev = Revision::newFromId( $newid );
147 if( is_null( $rev ) ) {
148 $newtext = '';
149 } else {
150 $newtext = $rev->getText();
151 }
152 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
153 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
154 }
155 $completeText .= $diffText;
156 }
157
158 wfProfileOut( __METHOD__ );
159 return $completeText;
160 }
161
162 /**
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.
166 *
167 * @param $text String: diff's HTML output
168 * @return String: modified HTML
169 */
170 public static function applyDiffStyle( $text ) {
171 $styles = array(
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;',
179 );
180
181 foreach( $styles as $class => $style ) {
182 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
183 "\\1style=\"$style\"\\3", $text );
184 }
185
186 return $text;
187 }
188
189 }