Wrap libxml_disable_entity_loader() calls in version constraint
[mediawiki.git] / includes / FeedUtils.php
blob5da27405076554ab042a19c71573af47755df0ee
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 use MediaWiki\MediaWikiServices;
25 use MediaWiki\Revision\RevisionRecord;
26 use MediaWiki\Revision\SlotRecord;
28 /**
29 * Helper functions for feeds
31 * @ingroup Feed
33 class FeedUtils {
35 /**
36 * Check whether feeds can be used and that $type is a valid feed type
38 * @since 1.36 $output parameter added
40 * @param string $type Feed type, as requested by the user
41 * @param OutputPage|null $output Null falls back to $wgOut
42 * @return bool
44 public static function checkFeedOutput( $type, $output = null ) {
45 global $wgFeed, $wgFeedClasses;
47 if ( $output === null ) {
48 // Todo update GoogleNewsSitemap and deprecate
49 global $wgOut;
50 $output = $wgOut;
53 if ( !$wgFeed ) {
54 $output->addWikiMsg( 'feed-unavailable' );
55 return false;
58 if ( !isset( $wgFeedClasses[$type] ) ) {
59 $output->addWikiMsg( 'feed-invalid' );
60 return false;
63 return true;
66 /**
67 * Format a diff for the newsfeed
69 * @param stdClass $row Row from the recentchanges table, including fields as
70 * appropriate for CommentStore
71 * @return string
73 public static function formatDiff( $row ) {
74 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
75 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
76 $actiontext = '';
77 if ( $row->rc_type == RC_LOG ) {
78 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
79 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
81 return self::formatDiffRow( $titleObj,
82 $row->rc_last_oldid, $row->rc_this_oldid,
83 $timestamp,
84 $row->rc_deleted & RevisionRecord::DELETED_COMMENT
85 ? wfMessage( 'rev-deleted-comment' )->escaped()
86 : CommentStore::getStore()->getComment( 'rc_comment', $row )->text,
87 $actiontext
91 /**
92 * Really format a diff for the newsfeed
94 * @param Title $title
95 * @param int $oldid Old revision's id
96 * @param int $newid New revision's id
97 * @param int $timestamp New revision's timestamp
98 * @param string $comment New revision's comment
99 * @param string $actiontext Text of the action; in case of log event
100 * @return string
102 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
103 $comment, $actiontext = ''
105 global $wgFeedDiffCutoff, $wgLang;
107 // log entries
108 $completeText = '<p>' . implode( ' ',
109 array_filter(
111 $actiontext,
112 Linker::formatComment( $comment ) ] ) ) . "</p>\n";
114 // NOTE: Check permissions for anonymous users, not current user.
115 // No "privileged" version should end up in the cache.
116 // Most feed readers will not log in anyway.
117 $anon = new User();
118 $services = MediaWikiServices::getInstance();
119 $permManager = $services->getPermissionManager();
120 $accErrors = $permManager->getPermissionErrors(
121 'read',
122 $anon,
123 $title
126 // Can't diff special pages, unreadable pages or pages with no new revision
127 // to compare against: just return the text.
128 if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
129 return $completeText;
132 $revLookup = $services->getRevisionLookup();
133 $contentHandlerFactory = $services->getContentHandlerFactory();
134 if ( $oldid ) {
135 $diffText = '';
136 // Don't bother generating the diff if we won't be able to show it
137 if ( $wgFeedDiffCutoff > 0 ) {
138 $revRecord = $revLookup->getRevisionById( $oldid );
140 if ( !$revRecord ) {
141 $diffText = false;
142 } else {
143 $context = clone RequestContext::getMain();
144 $context->setTitle( $title );
146 $model = $revRecord->getSlot(
147 SlotRecord::MAIN,
148 RevisionRecord::RAW
149 )->getModel();
150 $contentHandler = $contentHandlerFactory->getContentHandler( $model );
151 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
152 $diffText = $de->getDiff(
153 wfMessage( 'previousrevision' )->text(), // hack
154 wfMessage( 'revisionasof',
155 $wgLang->timeanddate( $timestamp ),
156 $wgLang->date( $timestamp ),
157 $wgLang->time( $timestamp ) )->text() );
161 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
162 // Omit large diffs
163 $diffText = self::getDiffLink( $title, $newid, $oldid );
164 } elseif ( $diffText === false ) {
165 // Error in diff engine, probably a missing revision
166 $diffText = "<p>Can't load revision $newid</p>";
167 } else {
168 // Diff output fine, clean up any illegal UTF-8
169 $diffText = UtfNormal\Validator::cleanUp( $diffText );
170 $diffText = self::applyDiffStyle( $diffText );
172 } else {
173 $revRecord = $revLookup->getRevisionById( $newid );
174 if ( $wgFeedDiffCutoff <= 0 || $revRecord === null ) {
175 $newContent = $contentHandlerFactory
176 ->getContentHandler( $title->getContentModel() )
177 ->makeEmptyContent();
178 } else {
179 $newContent = $revRecord->getContent( SlotRecord::MAIN );
182 if ( $newContent instanceof TextContent ) {
183 // only textual content has a "source view".
184 $text = $newContent->getText();
186 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
187 $html = null;
188 } else {
189 $html = nl2br( htmlspecialchars( $text ) );
191 } else {
192 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
193 // contain JS magic and generally may not be suitable for inclusion in a feed.
194 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
195 // Compare also ApiFeedContributions::feedItemDesc
196 $html = null;
199 if ( $html === null ) {
200 // Omit large new page diffs, T31110
201 // Also use diff link for non-textual content
202 $diffText = self::getDiffLink( $title, $newid );
203 } else {
204 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
205 '<div>' . $html . '</div>';
208 $completeText .= $diffText;
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 Title object: used to generate the diff URL
218 * @param int $newid Newid for this diff
219 * @param int|null $oldid 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 = [ 'diff' => $newid ];
224 if ( $oldid != null ) {
225 $queryParameters['oldid'] = $oldid;
227 $diffUrl = $title->getFullURL( $queryParameters );
229 $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
230 wfMessage( 'showdiff' )->inContentLanguage()->text() );
232 return $diffLink;
236 * Hacky application of diff styles for the feeds.
237 * Might be 'cleaner' to use DOM or XSLT or something,
238 * but *gack* it's a pain in the ass.
240 * @param string $text Diff's HTML output
241 * @return string Modified HTML
243 public static function applyDiffStyle( $text ) {
244 $styles = [
245 'diff' => 'background-color: #fff; color: #202122;',
246 'diff-otitle' => 'background-color: #fff; color: #202122; text-align: center;',
247 'diff-ntitle' => 'background-color: #fff; color: #202122; text-align: center;',
248 'diff-addedline' => 'color: #202122; font-size: 88%; border-style: solid; '
249 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
250 . 'vertical-align: top; white-space: pre-wrap;',
251 'diff-deletedline' => 'color: #202122; font-size: 88%; border-style: solid; '
252 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
253 . 'vertical-align: top; white-space: pre-wrap;',
254 'diff-context' => 'background-color: #f8f9fa; color: #202122; font-size: 88%; '
255 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
256 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
257 'diffchange' => 'font-weight: bold; text-decoration: none;',
260 foreach ( $styles as $class => $style ) {
261 $text = preg_replace( '/(<\w+\b[^<>]*)\bclass=([\'"])(?:[^\'"]*\s)?' .
262 preg_quote( $class ) . '(?:\s[^\'"]*)?\2(?=[^<>]*>)/',
263 '$1style="' . $style . '"', $text );
266 return $text;