merged master
[lhc/web/wiklou.git] / includes / FeedUtils.php
1 <?php
2 /**
3 * Helper functions for feeds.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Feed
22 */
23
24 /**
25 * Helper functions for feeds
26 *
27 * @ingroup Feed
28 */
29 class FeedUtils {
30
31 /**
32 * Check whether feed's cache should be cleared; for changes feeds
33 * If the feed should be purged; $timekey and $key will be removed from
34 * $messageMemc
35 *
36 * @param $timekey String: cache key of the timestamp of the last item
37 * @param $key String: cache key of feed's content
38 */
39 public static function checkPurge( $timekey, $key ) {
40 global $wgRequest, $wgUser, $messageMemc;
41 $purge = $wgRequest->getVal( 'action' ) === 'purge';
42 if ( $purge && $wgUser->isAllowed('purge') ) {
43 $messageMemc->delete( $timekey );
44 $messageMemc->delete( $key );
45 }
46 }
47
48 /**
49 * Check whether feeds can be used and that $type is a valid feed type
50 *
51 * @param $type String: feed type, as requested by the user
52 * @return Boolean
53 */
54 public static function checkFeedOutput( $type ) {
55 global $wgOut, $wgFeed, $wgFeedClasses;
56
57 if ( !$wgFeed ) {
58 $wgOut->addWikiMsg( 'feed-unavailable' );
59 return false;
60 }
61
62 if( !isset( $wgFeedClasses[$type] ) ) {
63 $wgOut->addWikiMsg( 'feed-invalid' );
64 return false;
65 }
66
67 return true;
68 }
69
70 /**
71 * Format a diff for the newsfeed
72 *
73 * @param $row Object: row from the recentchanges table
74 * @return String
75 */
76 public static function formatDiff( $row ) {
77 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
78 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
79 $actiontext = '';
80 if( $row->rc_type == RC_LOG ) {
81 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
82 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
83 }
84 return self::formatDiffRow( $titleObj,
85 $row->rc_last_oldid, $row->rc_this_oldid,
86 $timestamp,
87 ($row->rc_deleted & Revision::DELETED_COMMENT)
88 ? wfMsgHtml('rev-deleted-comment')
89 : $row->rc_comment,
90 $actiontext
91 );
92 }
93
94 /**
95 * Really format a diff for the newsfeed
96 *
97 * @param $title Title object
98 * @param $oldid Integer: old revision's id
99 * @param $newid Integer: new revision's id
100 * @param $timestamp Integer: new revision's timestamp
101 * @param $comment String: new revision's comment
102 * @param $actiontext String: text of the action; in case of log event
103 * @return String
104 */
105 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
106 global $wgFeedDiffCutoff, $wgLang;
107 wfProfileIn( __METHOD__ );
108
109 # log enties
110 $completeText = '<p>' . implode( ' ',
111 array_filter(
112 array(
113 $actiontext,
114 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
115
116 // NOTE: Check permissions for anonymous users, not current user.
117 // No "privileged" version should end up in the cache.
118 // Most feed readers will not log in anway.
119 $anon = new User();
120 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
121
122 // Can't diff special pages, unreadable pages or pages with no new revision
123 // to compare against: just return the text.
124 if( $title->getNamespace() < 0 || $accErrors || !$newid ) {
125 wfProfileOut( __METHOD__ );
126 return $completeText;
127 }
128
129 if( $oldid ) {
130 wfProfileIn( __METHOD__."-dodiff" );
131
132 #$diffText = $de->getDiff( wfMsg( 'revisionasof',
133 # $wgLang->timeanddate( $timestamp ),
134 # $wgLang->date( $timestamp ),
135 # $wgLang->time( $timestamp ) ),
136 # wfMsg( 'currentrev' ) );
137
138 $diffText = '';
139 // Don't bother generating the diff if we won't be able to show it
140 if ( $wgFeedDiffCutoff > 0 ) {
141 $contentHandler = ContentHandler::getForTitle( $title );
142 $de = $contentHandler->createDifferenceEngine( $title, $oldid, $newid );
143 $diffText = $de->getDiff(
144 wfMsg( 'previousrevision' ), // hack
145 wfMsg( 'revisionasof',
146 $wgLang->timeanddate( $timestamp ),
147 $wgLang->date( $timestamp ),
148 $wgLang->time( $timestamp ) ) );
149 }
150
151 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
152 // Omit large diffs
153 $diffText = self::getDiffLink( $title, $newid, $oldid );
154 } elseif ( $diffText === false ) {
155 // Error in diff engine, probably a missing revision
156 $diffText = "<p>Can't load revision $newid</p>";
157 } else {
158 // Diff output fine, clean up any illegal UTF-8
159 $diffText = UtfNormal::cleanUp( $diffText );
160 $diffText = self::applyDiffStyle( $diffText );
161 }
162 wfProfileOut( __METHOD__."-dodiff" );
163 } else {
164 $rev = Revision::newFromId( $newid );
165 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
166 $newtext = '';
167 } else {
168 $newtext = $rev->getText();
169 }
170 if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
171 // Omit large new page diffs, bug 29110
172 $diffText = self::getDiffLink( $title, $newid );
173 } else {
174 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
175 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
176 }
177 }
178 $completeText .= $diffText;
179
180 wfProfileOut( __METHOD__ );
181 return $completeText;
182 }
183
184 /**
185 * Generates a diff link. Used when the full diff is not wanted for example
186 * when $wgFeedDiffCutoff is 0.
187 *
188 * @param $title Title object: used to generate the diff URL
189 * @param $newid Integer newid for this diff
190 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
191 * @return string
192 */
193 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
194 $queryParameters = ($oldid == null)
195 ? "diff={$newid}"
196 : "diff={$newid}&oldid={$oldid}" ;
197 $diffUrl = $title->getFullUrl( $queryParameters );
198
199 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
200 wfMsgForContent( 'showdiff' ) );
201
202 return $diffLink;
203 }
204
205 /**
206 * Hacky application of diff styles for the feeds.
207 * Might be 'cleaner' to use DOM or XSLT or something,
208 * but *gack* it's a pain in the ass.
209 *
210 * @param $text String: diff's HTML output
211 * @return String: modified HTML
212 */
213 public static function applyDiffStyle( $text ) {
214 $styles = array(
215 'diff' => 'background-color: white; color:black;',
216 'diff-otitle' => 'background-color: white; color:black;',
217 'diff-ntitle' => 'background-color: white; color:black;',
218 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
219 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
220 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
221 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
222 );
223
224 foreach( $styles as $class => $style ) {
225 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
226 "\\1style=\"$style\"\\3", $text );
227 }
228
229 return $text;
230 }
231
232 }