Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / FeedUtils.php
blob4842c38e1b131e1d39b93a18813ad9c4f9e5b9fc
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
34 * $messageMemc
36 * @param string $timekey cache key of the timestamp of the last item
37 * @param string $key cache key of feed's content
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 );
48 /**
49 * Check whether feeds can be used and that $type is a valid feed type
51 * @param string $type feed type, as requested by the user
52 * @return Boolean
54 public static function checkFeedOutput( $type ) {
55 global $wgOut, $wgFeed, $wgFeedClasses;
57 if ( !$wgFeed ) {
58 $wgOut->addWikiMsg( 'feed-unavailable' );
59 return false;
62 if( !isset( $wgFeedClasses[$type] ) ) {
63 $wgOut->addWikiMsg( 'feed-invalid' );
64 return false;
67 return true;
70 /**
71 * Format a diff for the newsfeed
73 * @param $row Object: row from the recentchanges table
74 * @return String
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();
84 return self::formatDiffRow( $titleObj,
85 $row->rc_last_oldid, $row->rc_this_oldid,
86 $timestamp,
87 ($row->rc_deleted & Revision::DELETED_COMMENT)
88 ? wfMessage( 'rev-deleted-comment' )->escaped()
89 : $row->rc_comment,
90 $actiontext
94 /**
95 * Really format a diff for the newsfeed
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 string $comment new revision's comment
102 * @param string $actiontext text of the action; in case of log event
103 * @return String
105 public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext = '' ) {
106 global $wgFeedDiffCutoff, $wgLang;
107 wfProfileIn( __METHOD__ );
109 // log entries
110 $completeText = '<p>' . implode( ' ',
111 array_filter(
112 array(
113 $actiontext,
114 Linker::formatComment( $comment ) ) ) ) . "</p>\n";
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 anyway.
119 $anon = new User();
120 $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
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;
129 if( $oldid ) {
130 wfProfileIn( __METHOD__ . "-dodiff" );
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::cleanUp( $diffText );
169 $diffText = self::applyDiffStyle( $diffText );
171 wfProfileOut( __METHOD__ . "-dodiff" );
172 } else {
173 $rev = Revision::newFromId( $newid );
174 if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
175 $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
176 } else {
177 $newContent = $rev->getContent();
180 if ( $newContent instanceof TextContent ) {
181 // only textual content has a "source view".
182 $text = $newContent->getNativeData();
184 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
185 $html = null;
186 } else {
187 $html = nl2br( htmlspecialchars( $text ) );
189 } else {
190 //XXX: we could get an HTML representation of the content via getParserOutput, but that may
191 // contain JS magic and generally may not be suitable for inclusion in a feed.
192 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
193 //Compare also ApiFeedContributions::feedItemDesc
194 $html = null;
197 if ( $html === null ) {
199 // Omit large new page diffs, bug 29110
200 // Also use diff link for non-textual content
201 $diffText = self::getDiffLink( $title, $newid );
202 } else {
203 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
204 '<div>' . $html . '</div>';
207 $completeText .= $diffText;
209 wfProfileOut( __METHOD__ );
210 return $completeText;
214 * Generates a diff link. Used when the full diff is not wanted for example
215 * when $wgFeedDiffCutoff is 0.
217 * @param $title Title object: used to generate the diff URL
218 * @param $newid Integer newid for this diff
219 * @param $oldid Integer|null oldid for the diff. Null means it is a new article
220 * @return string
222 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
223 $queryParameters = ($oldid == null)
224 ? "diff={$newid}"
225 : "diff={$newid}&oldid={$oldid}";
226 $diffUrl = $title->getFullURL( $queryParameters );
228 $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
229 wfMessage( 'showdiff' )->inContentLanguage()->text() );
231 return $diffLink;
235 * Hacky application of diff styles for the feeds.
236 * Might be 'cleaner' to use DOM or XSLT or something,
237 * but *gack* it's a pain in the ass.
239 * @param string $text diff's HTML output
240 * @return String: modified HTML
242 public static function applyDiffStyle( $text ) {
243 $styles = array(
244 'diff' => 'background-color: white; color:black;',
245 'diff-otitle' => 'background-color: white; color:black; text-align: center;',
246 'diff-ntitle' => 'background-color: white; color:black; text-align: center;',
247 'diff-addedline' => 'color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;',
248 'diff-deletedline' => 'color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;',
249 'diff-context' => 'background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;',
250 'diffchange' => 'font-weight: bold; text-decoration: none;',
253 foreach( $styles as $class => $style ) {
254 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
255 "\\1style=\"$style\"\\3", $text );
258 return $text;