Merge "Remove not used private member variable mParserWarnings from OutputPage"
[mediawiki.git] / includes / FeedUtils.php
blobb058975b736f0e898380087277f2329c81bcbe0c
1 <?php
2 /**
3 * Helper functions for feeds.
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.
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.
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
20 * @file
21 * @ingroup Feed
24 /**
25 * Helper functions for feeds
27 * @ingroup Feed
29 class FeedUtils {
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 cache
35 * @param string $timekey Cache key of the timestamp of the last item
36 * @param string $key Cache key of feed's content
38 public static function checkPurge( $timekey, $key ) {
39 global $wgRequest, $wgUser;
41 $purge = $wgRequest->getVal( 'action' ) === 'purge';
42 if ( $purge && $wgUser->isAllowed( 'purge' ) ) {
43 $cache = ObjectCache::getMainWANInstance();
44 $cache->delete( $timekey, 1 );
45 $cache->delete( $key, 1 );
49 /**
50 * Check whether feeds can be used and that $type is a valid feed type
52 * @param string $type Feed type, as requested by the user
53 * @return bool
55 public static function checkFeedOutput( $type ) {
56 global $wgOut, $wgFeed, $wgFeedClasses;
58 if ( !$wgFeed ) {
59 $wgOut->addWikiMsg( 'feed-unavailable' );
60 return false;
63 if ( !isset( $wgFeedClasses[$type] ) ) {
64 $wgOut->addWikiMsg( 'feed-invalid' );
65 return false;
68 return true;
71 /**
72 * Format a diff for the newsfeed
74 * @param object $row Row from the recentchanges table
75 * @return string
77 public static function formatDiff( $row ) {
78 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
79 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
80 $actiontext = '';
81 if ( $row->rc_type == RC_LOG ) {
82 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
83 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
85 return self::formatDiffRow( $titleObj,
86 $row->rc_last_oldid, $row->rc_this_oldid,
87 $timestamp,
88 $row->rc_deleted & Revision::DELETED_COMMENT
89 ? wfMessage( 'rev-deleted-comment' )->escaped()
90 : $row->rc_comment,
91 $actiontext
95 /**
96 * Really format a diff for the newsfeed
98 * @param Title $title Title object
99 * @param int $oldid Old revision's id
100 * @param int $newid New revision's id
101 * @param int $timestamp New revision's timestamp
102 * @param string $comment New revision's comment
103 * @param string $actiontext Text of the action; in case of log event
104 * @return string
106 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
107 $comment, $actiontext = ''
109 global $wgFeedDiffCutoff, $wgLang;
111 // log entries
112 $completeText = '<p>' . implode( ' ',
113 array_filter(
114 array(
115 $actiontext,
116 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
118 // NOTE: Check permissions for anonymous users, not current user.
119 // No "privileged" version should end up in the cache.
120 // Most feed readers will not log in anyway.
121 $anon = new User();
122 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
124 // Can't diff special pages, unreadable pages or pages with no new revision
125 // to compare against: just return the text.
126 if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
127 return $completeText;
130 if ( $oldid ) {
132 # $diffText = $de->getDiff( wfMessage( 'revisionasof',
133 # $wgLang->timeanddate( $timestamp ),
134 # $wgLang->date( $timestamp ),
135 # $wgLang->time( $timestamp ) )->text(),
136 # wfMessage( 'currentrev' )->text() );
138 $diffText = '';
139 // Don't bother generating the diff if we won't be able to show it
140 if ( $wgFeedDiffCutoff > 0 ) {
141 $rev = Revision::newFromId( $oldid );
143 if ( !$rev ) {
144 $diffText = false;
145 } else {
146 $context = clone RequestContext::getMain();
147 $context->setTitle( $title );
149 $contentHandler = $rev->getContentHandler();
150 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
151 $diffText = $de->getDiff(
152 wfMessage( 'previousrevision' )->text(), // hack
153 wfMessage( 'revisionasof',
154 $wgLang->timeanddate( $timestamp ),
155 $wgLang->date( $timestamp ),
156 $wgLang->time( $timestamp ) )->text() );
160 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
161 // Omit large diffs
162 $diffText = self::getDiffLink( $title, $newid, $oldid );
163 } elseif ( $diffText === false ) {
164 // Error in diff engine, probably a missing revision
165 $diffText = "<p>Can't load revision $newid</p>";
166 } else {
167 // Diff output fine, clean up any illegal UTF-8
168 $diffText = UtfNormal\Validator::cleanUp( $diffText );
169 $diffText = self::applyDiffStyle( $diffText );
171 } else {
172 $rev = Revision::newFromId( $newid );
173 if ( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
174 $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
175 } else {
176 $newContent = $rev->getContent();
179 if ( $newContent instanceof TextContent ) {
180 // only textual content has a "source view".
181 $text = $newContent->getNativeData();
183 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
184 $html = null;
185 } else {
186 $html = nl2br( htmlspecialchars( $text ) );
188 } else {
189 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
190 // contain JS magic and generally may not be suitable for inclusion in a feed.
191 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
192 // Compare also ApiFeedContributions::feedItemDesc
193 $html = null;
196 if ( $html === null ) {
198 // Omit large new page diffs, bug 29110
199 // Also use diff link for non-textual content
200 $diffText = self::getDiffLink( $title, $newid );
201 } else {
202 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
203 '<div>' . $html . '</div>';
206 $completeText .= $diffText;
208 return $completeText;
212 * Generates a diff link. Used when the full diff is not wanted for example
213 * when $wgFeedDiffCutoff is 0.
215 * @param Title $title Title object: used to generate the diff URL
216 * @param int $newid Newid for this diff
217 * @param int|null $oldid Oldid for the diff. Null means it is a new article
218 * @return string
220 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
221 $queryParameters = array( 'diff' => $newid );
222 if ( $oldid != null ) {
223 $queryParameters['oldid'] = $oldid;
225 $diffUrl = $title->getFullURL( $queryParameters );
227 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
228 wfMessage( 'showdiff' )->inContentLanguage()->text() );
230 return $diffLink;
234 * Hacky application of diff styles for the feeds.
235 * Might be 'cleaner' to use DOM or XSLT or something,
236 * but *gack* it's a pain in the ass.
238 * @param string $text Diff's HTML output
239 * @return string Modified HTML
241 public static function applyDiffStyle( $text ) {
242 $styles = array(
243 'diff' => 'background-color: white; color:black;',
244 'diff-otitle' => 'background-color: white; color:black; text-align: center;',
245 'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
246 'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; '
247 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
248 . 'vertical-align: top; white-space: pre-wrap;',
249 'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; '
250 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
251 . 'vertical-align: top; white-space: pre-wrap;',
252 'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; '
253 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
254 . 'border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
255 'diffchange' => 'font-weight: bold; text-decoration: none;',
258 foreach ( $styles as $class => $style ) {
259 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
260 "\\1style=\"$style\"\\3", $text );
263 return $text;