CentralIdLookup: Add @since to factoryNonLocal()
[mediawiki.git] / includes / FeedUtils.php
blobad9b6417b01da5c5960bf7326c065dcb7545a5d9
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 * @param string $type Feed type, as requested by the user
39 * @return bool
41 public static function checkFeedOutput( $type ) {
42 global $wgOut, $wgFeed, $wgFeedClasses;
44 if ( !$wgFeed ) {
45 $wgOut->addWikiMsg( 'feed-unavailable' );
46 return false;
49 if ( !isset( $wgFeedClasses[$type] ) ) {
50 $wgOut->addWikiMsg( 'feed-invalid' );
51 return false;
54 return true;
57 /**
58 * Format a diff for the newsfeed
60 * @param object $row Row from the recentchanges table, including fields as
61 * appropriate for CommentStore
62 * @return string
64 public static function formatDiff( $row ) {
65 $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
66 $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
67 $actiontext = '';
68 if ( $row->rc_type == RC_LOG ) {
69 $rcRow = (array)$row; // newFromRow() only accepts arrays for RC rows
70 $actiontext = LogFormatter::newFromRow( $rcRow )->getActionText();
72 return self::formatDiffRow( $titleObj,
73 $row->rc_last_oldid, $row->rc_this_oldid,
74 $timestamp,
75 $row->rc_deleted & RevisionRecord::DELETED_COMMENT
76 ? wfMessage( 'rev-deleted-comment' )->escaped()
77 : CommentStore::getStore()->getComment( 'rc_comment', $row )->text,
78 $actiontext
82 /**
83 * Really format a diff for the newsfeed
85 * @param Title $title
86 * @param int $oldid Old revision's id
87 * @param int $newid New revision's id
88 * @param int $timestamp New revision's timestamp
89 * @param string $comment New revision's comment
90 * @param string $actiontext Text of the action; in case of log event
91 * @return string
93 public static function formatDiffRow( $title, $oldid, $newid, $timestamp,
94 $comment, $actiontext = ''
95 ) {
96 global $wgFeedDiffCutoff, $wgLang;
98 // log entries
99 $completeText = '<p>' . implode( ' ',
100 array_filter(
102 $actiontext,
103 Linker::formatComment( $comment ) ] ) ) . "</p>\n";
105 // NOTE: Check permissions for anonymous users, not current user.
106 // No "privileged" version should end up in the cache.
107 // Most feed readers will not log in anyway.
108 $anon = new User();
109 $services = MediaWikiServices::getInstance();
110 $permManager = $services->getPermissionManager();
111 $accErrors = $permManager->getPermissionErrors(
112 'read',
113 $anon,
114 $title
117 // Can't diff special pages, unreadable pages or pages with no new revision
118 // to compare against: just return the text.
119 if ( $title->getNamespace() < 0 || $accErrors || !$newid ) {
120 return $completeText;
123 $revLookup = $services->getRevisionLookup();
124 $contentHandlerFactory = $services->getContentHandlerFactory();
125 if ( $oldid ) {
126 $diffText = '';
127 // Don't bother generating the diff if we won't be able to show it
128 if ( $wgFeedDiffCutoff > 0 ) {
129 $revRecord = $revLookup->getRevisionById( $oldid );
131 if ( !$revRecord ) {
132 $diffText = false;
133 } else {
134 $context = clone RequestContext::getMain();
135 $context->setTitle( $title );
137 $model = $revRecord->getSlot(
138 SlotRecord::MAIN,
139 RevisionRecord::RAW
140 )->getModel();
141 $contentHandler = $contentHandlerFactory->getContentHandler( $model );
142 $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
143 $diffText = $de->getDiff(
144 wfMessage( 'previousrevision' )->text(), // hack
145 wfMessage( 'revisionasof',
146 $wgLang->timeanddate( $timestamp ),
147 $wgLang->date( $timestamp ),
148 $wgLang->time( $timestamp ) )->text() );
152 if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
153 // Omit large diffs
154 $diffText = self::getDiffLink( $title, $newid, $oldid );
155 } elseif ( $diffText === false ) {
156 // Error in diff engine, probably a missing revision
157 $diffText = "<p>Can't load revision $newid</p>";
158 } else {
159 // Diff output fine, clean up any illegal UTF-8
160 $diffText = UtfNormal\Validator::cleanUp( $diffText );
161 $diffText = self::applyDiffStyle( $diffText );
163 } else {
164 $revRecord = $revLookup->getRevisionById( $newid );
165 if ( $wgFeedDiffCutoff <= 0 || $revRecord === null ) {
166 $newContent = $contentHandlerFactory
167 ->getContentHandler( $title->getContentModel() )
168 ->makeEmptyContent();
169 } else {
170 $newContent = $revRecord->getContent( SlotRecord::MAIN );
173 if ( $newContent instanceof TextContent ) {
174 // only textual content has a "source view".
175 $text = $newContent->getText();
177 if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
178 $html = null;
179 } else {
180 $html = nl2br( htmlspecialchars( $text ) );
182 } else {
183 // XXX: we could get an HTML representation of the content via getParserOutput, but that may
184 // contain JS magic and generally may not be suitable for inclusion in a feed.
185 // Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
186 // Compare also ApiFeedContributions::feedItemDesc
187 $html = null;
190 if ( $html === null ) {
191 // Omit large new page diffs, T31110
192 // Also use diff link for non-textual content
193 $diffText = self::getDiffLink( $title, $newid );
194 } else {
195 $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
196 '<div>' . $html . '</div>';
199 $completeText .= $diffText;
201 return $completeText;
205 * Generates a diff link. Used when the full diff is not wanted for example
206 * when $wgFeedDiffCutoff is 0.
208 * @param Title $title Title object: used to generate the diff URL
209 * @param int $newid Newid for this diff
210 * @param int|null $oldid Oldid for the diff. Null means it is a new article
211 * @return string
213 protected static function getDiffLink( Title $title, $newid, $oldid = null ) {
214 $queryParameters = [ 'diff' => $newid ];
215 if ( $oldid != null ) {
216 $queryParameters['oldid'] = $oldid;
218 $diffUrl = $title->getFullURL( $queryParameters );
220 $diffLink = Html::element( 'a', [ 'href' => $diffUrl ],
221 wfMessage( 'showdiff' )->inContentLanguage()->text() );
223 return $diffLink;
227 * Hacky application of diff styles for the feeds.
228 * Might be 'cleaner' to use DOM or XSLT or something,
229 * but *gack* it's a pain in the ass.
231 * @param string $text Diff's HTML output
232 * @return string Modified HTML
234 public static function applyDiffStyle( $text ) {
235 $styles = [
236 'diff' => 'background-color: #fff; color: #202122;',
237 'diff-otitle' => 'background-color: #fff; color: #202122; text-align: center;',
238 'diff-ntitle' => 'background-color: #fff; color: #202122; text-align: center;',
239 'diff-addedline' => 'color: #202122; font-size: 88%; border-style: solid; '
240 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; '
241 . 'vertical-align: top; white-space: pre-wrap;',
242 'diff-deletedline' => 'color: #202122; font-size: 88%; border-style: solid; '
243 . 'border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; '
244 . 'vertical-align: top; white-space: pre-wrap;',
245 'diff-context' => 'background-color: #f8f9fa; color: #202122; font-size: 88%; '
246 . 'border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; '
247 . 'border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;',
248 'diffchange' => 'font-weight: bold; text-decoration: none;',
251 foreach ( $styles as $class => $style ) {
252 $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
253 "\\1style=\"$style\"\\3", $text );
256 return $text;