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