77be12b20f53a97ae603f855bca1bbe392b4d389
[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, RequestContext::getMain()->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 # log enties
92 $completeText = '<p>' . implode( ' ',
93 array_filter(
94 array(
95 $actiontext,
96 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
97
98 //NOTE: Check permissions for anonymous users, not current user.
99 // No "privileged" version should end up in the cache.
100 // Most feed readers will not log in anway.
101 $anon = new User();
102 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
103
104 // Can't diff special pages, unreadable pages or pages with no new revision
105 // to compare against: just return the text.
106 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
107 wfProfileOut( __METHOD__ );
108 return $completeText;
109 }
110
111 if( $oldid ) {
112 wfProfileIn( __METHOD__."-dodiff" );
113
114 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
115 # $wgLang->timeanddate( $timestamp ),
116 # $wgLang->date( $timestamp ),
117 # $wgLang->time( $timestamp ) ),
118 # wfMsg( 'currentrev' ) );
119
120 // Don't bother generating the diff if we won't be able to show it
121 if ( $wgFeedDiffCutoff > 0 ) {
122 $de = new DifferenceEngine( $title, $oldid, $newid );
123 $diffText = $de->getDiff(
124 wfMsg( 'previousrevision' ), // hack
125 wfMsg( 'revisionasof',
126 $wgLang->timeanddate( $timestamp ),
127 $wgLang->date( $timestamp ),
128 $wgLang->time( $timestamp ) ) );
129 }
130
131 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
132 // Omit large diffs
133 $diffText = self::getDiffLink( $title, $newid, $oldid );
134 } elseif ( $diffText === false ) {
135 // Error in diff engine, probably a missing revision
136 $diffText = "<p>Can't load revision $newid</p>";
137 } else {
138 // Diff output fine, clean up any illegal UTF-8
139 $diffText = UtfNormal::cleanUp( $diffText );
140 $diffText = self::applyDiffStyle( $diffText );
141 }
142 wfProfileOut( __METHOD__."-dodiff" );
143 } else {
144 $rev = Revision::newFromId( $newid );
145 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
146 $newtext = '';
147 } else {
148 $newtext = $rev->getText();
149 }
150 if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
151 // Omit large new page diffs, bug 29110
152 $diffText = self::getDiffLink( $title, $newid );
153 } else {
154 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
155 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
156 }
157 }
158 $completeText .= $diffText;
159
160 wfProfileOut( __METHOD__ );
161 return $completeText;
162 }
163
164 /**
165 * Generates a diff link. Used when the full diff is not wanted for example
166 * when $wgFeedDiffCutoff is 0.
167 *
168 * @param $title Title object: used to generate the diff URL
169 * @param $newid Integer newid for this diff
170 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
171 */
172 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
173 $queryParameters = ($oldid == null)
174 ? "diff={$newid}"
175 : "diff={$newid}&oldid={$oldid}" ;
176 $diffUrl = $title->getFullUrl( $queryParameters );
177
178 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
179 wfMsgForContent( 'showdiff' ) );
180
181 return $diffLink;
182 }
183
184 /**
185 * Hacky application of diff styles for the feeds.
186 * Might be 'cleaner' to use DOM or XSLT or something,
187 * but *gack* it's a pain in the ass.
188 *
189 * @param $text String: diff's HTML output
190 * @return String: modified HTML
191 */
192 public static function applyDiffStyle( $text ) {
193 $styles = array(
194 'diff' => 'background-color: white; color:black;',
195 'diff-otitle' => 'background-color: white; color:black;',
196 'diff-ntitle' => 'background-color: white; color:black;',
197 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
198 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
199 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
200 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
201 );
202
203 foreach( $styles as $class => $style ) {
204 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
205 "\\1style=\"$style\"\\3", $text );
206 }
207
208 return $text;
209 }
210
211 }