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