RELEASE-NOTES-1.19 for r103706, r103708
[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 # Early exist when the page is not an article, on errors and no newid to
106 # compare.
107 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
108 wfProfileOut( __METHOD__ );
109 return $completeText;
110 }
111
112 if( $oldid ) {
113 wfProfileIn( __METHOD__."-dodiff" );
114
115 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
116 # $wgLang->timeanddate( $timestamp ),
117 # $wgLang->date( $timestamp ),
118 # $wgLang->time( $timestamp ) ),
119 # wfMsg( 'currentrev' ) );
120
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 ) ) );
130 }
131
132 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
133 // Omit large diffs
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>";
138 } else {
139 // Diff output fine, clean up any illegal UTF-8
140 $diffText = UtfNormal::cleanUp( $diffText );
141 $diffText = self::applyDiffStyle( $diffText );
142 }
143 wfProfileOut( __METHOD__."-dodiff" );
144 } else {
145 $rev = Revision::newFromId( $newid );
146 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
147 $newtext = '';
148 } else {
149 $newtext = $rev->getText();
150 }
151 if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
152 // Omit large new page diffs, bug 29110
153 $diffText = self::getDiffText( $title, $newid );
154 } else {
155 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
156 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
157 }
158 }
159 $completeText .= $diffText;
160
161 wfProfileOut( __METHOD__ );
162 return $completeText;
163 }
164
165 /**
166 * Generates a diff link. Used when the full diff is not wanted for example
167 * when $wgFeedDiffCutoff is 0.
168 *
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
172 */
173 protected static function getDiffText( Title $title, $newid, $oldid = null ) {
174 $queryParameters = ($oldid == null)
175 ? "diff={$newid}"
176 : "diff={$newid}&oldid={$oldid}" ;
177 $diffLink = $title->escapeFullUrl( $queryParameters );
178
179 $diffText = Html::RawElement( 'a', array( 'href' => $diffLink ),
180 htmlspecialchars( wfMsgForContent( 'showdiff' ) )
181 );
182
183 return $diffText;
184 }
185
186 /**
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.
190 *
191 * @param $text String: diff's HTML output
192 * @return String: modified HTML
193 */
194 public static function applyDiffStyle( $text ) {
195 $styles = array(
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;',
203 );
204
205 foreach( $styles as $class => $style ) {
206 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
207 "\\1style=\"$style\"\\3", $text );
208 }
209
210 return $text;
211 }
212
213 }