Merge "Fix trailing whitespace (and mixed spaces) in XSD files"
[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 $de = new DifferenceEngine( $title, $oldid, $newid );
142 $diffText = $de->getDiff(
143 wfMsg( 'previousrevision' ), // hack
144 wfMsg( 'revisionasof',
145 $wgLang->timeanddate( $timestamp ),
146 $wgLang->date( $timestamp ),
147 $wgLang->time( $timestamp ) ) );
148 }
149
150 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
151 // Omit large diffs
152 $diffText = self::getDiffLink( $title, $newid, $oldid );
153 } elseif ( $diffText === false ) {
154 // Error in diff engine, probably a missing revision
155 $diffText = "<p>Can't load revision $newid</p>";
156 } else {
157 // Diff output fine, clean up any illegal UTF-8
158 $diffText = UtfNormal::cleanUp( $diffText );
159 $diffText = self::applyDiffStyle( $diffText );
160 }
161 wfProfileOut( __METHOD__."-dodiff" );
162 } else {
163 $rev = Revision::newFromId( $newid );
164 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
165 $newtext = '';
166 } else {
167 $newtext = $rev->getText();
168 }
169 if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
170 // Omit large new page diffs, bug 29110
171 $diffText = self::getDiffLink( $title, $newid );
172 } else {
173 $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
174 '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
175 }
176 }
177 $completeText .= $diffText;
178
179 wfProfileOut( __METHOD__ );
180 return $completeText;
181 }
182
183 /**
184 * Generates a diff link. Used when the full diff is not wanted for example
185 * when $wgFeedDiffCutoff is 0.
186 *
187 * @param $title Title object: used to generate the diff URL
188 * @param $newid Integer newid for this diff
189 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
190 * @return string
191 */
192 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
193 $queryParameters = ($oldid == null)
194 ? "diff={$newid}"
195 : "diff={$newid}&oldid={$oldid}" ;
196 $diffUrl = $title->getFullUrl( $queryParameters );
197
198 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
199 wfMsgForContent( 'showdiff' ) );
200
201 return $diffLink;
202 }
203
204 /**
205 * Hacky application of diff styles for the feeds.
206 * Might be 'cleaner' to use DOM or XSLT or something,
207 * but *gack* it's a pain in the ass.
208 *
209 * @param $text String: diff's HTML output
210 * @return String: modified HTML
211 */
212 public static function applyDiffStyle( $text ) {
213 $styles = array(
214 'diff' => 'background-color: white; color:black;',
215 'diff-otitle' => 'background-color: white; color:black;',
216 'diff-ntitle' => 'background-color: white; color:black;',
217 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
218 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
219 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
220 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
221 );
222
223 foreach( $styles as $class => $style ) {
224 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
225 "\\1style=\"$style\"\\3", $text );
226 }
227
228 return $text;
229 }
230
231 }