FeedUtils.php 8.5 KB

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