FeedUtils.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // TODO: document
  3. class FeedUtils {
  4. public static function checkPurge( $timekey, $key ) {
  5. global $wgRequest, $wgUser, $messageMemc;
  6. $purge = $wgRequest->getVal( 'action' ) === 'purge';
  7. if ( $purge && $wgUser->isAllowed('purge') ) {
  8. $messageMemc->delete( $timekey );
  9. $messageMemc->delete( $key );
  10. }
  11. }
  12. public static function checkFeedOutput( $type ) {
  13. global $wgFeed, $wgOut, $wgFeedClasses;
  14. if ( !$wgFeed ) {
  15. global $wgOut;
  16. $wgOut->addWikiMsg( 'feed-unavailable' );
  17. return false;
  18. }
  19. if( !isset( $wgFeedClasses[$type] ) ) {
  20. wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
  21. return false;
  22. }
  23. return true;
  24. }
  25. /**
  26. * Format a diff for the newsfeed
  27. */
  28. public static function formatDiff( $row ) {
  29. global $wgUser;
  30. $titleObj = Title::makeTitle( $row->rc_namespace, $row->rc_title );
  31. $timestamp = wfTimestamp( TS_MW, $row->rc_timestamp );
  32. $actiontext = '';
  33. if( $row->rc_type == RC_LOG ) {
  34. if( $row->rc_deleted & LogPage::DELETED_ACTION ) {
  35. $actiontext = wfMsgHtml('rev-deleted-event');
  36. } else {
  37. $actiontext = LogPage::actionText( $row->rc_log_type, $row->rc_log_action,
  38. $titleObj, $wgUser->getSkin(), LogPage::extractParams($row->rc_params,true,true) );
  39. }
  40. }
  41. return self::formatDiffRow( $titleObj,
  42. $row->rc_last_oldid, $row->rc_this_oldid,
  43. $timestamp,
  44. ($row->rc_deleted & Revision::DELETED_COMMENT) ? wfMsgHtml('rev-deleted-comment') : $row->rc_comment,
  45. $actiontext );
  46. }
  47. public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
  48. global $wgFeedDiffCutoff, $wgContLang, $wgUser;
  49. wfProfileIn( __FUNCTION__ );
  50. $skin = $wgUser->getSkin();
  51. # log enties
  52. $completeText = '<p>' . implode( ' ',
  53. array_filter(
  54. array(
  55. $actiontext,
  56. $skin->formatComment( $comment ) ) ) ) . "</p>\n";
  57. //NOTE: Check permissions for anonymous users, not current user.
  58. // No "privileged" version should end up in the cache.
  59. // Most feed readers will not log in anway.
  60. $anon = new User();
  61. $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
  62. if( $title->getNamespace() >= 0 && !$accErrors ) {
  63. if( $oldid ) {
  64. wfProfileIn( __FUNCTION__."-dodiff" );
  65. #$diffText = $de->getDiff( wfMsg( 'revisionasof',
  66. # $wgContLang->timeanddate( $timestamp ) ),
  67. # wfMsg( 'currentrev' ) );
  68. // Don't bother generating the diff if we won't be able to show it
  69. if ( $wgFeedDiffCutoff > 0 ) {
  70. $de = new DifferenceEngine( $title, $oldid, $newid );
  71. $diffText = $de->getDiff(
  72. wfMsg( 'previousrevision' ), // hack
  73. wfMsg( 'revisionasof',
  74. $wgContLang->timeanddate( $timestamp ) ) );
  75. }
  76. if ( ( strlen( $diffText ) > $wgFeedDiffCutoff ) || ( $wgFeedDiffCutoff <= 0 ) ) {
  77. // Omit large diffs
  78. $diffLink = $title->escapeFullUrl(
  79. 'diff=' . $newid .
  80. '&oldid=' . $oldid );
  81. $diffText = '<a href="' .
  82. $diffLink .
  83. '">' .
  84. htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
  85. '</a>';
  86. } elseif ( $diffText === false ) {
  87. // Error in diff engine, probably a missing revision
  88. $diffText = "<p>Can't load revision $newid</p>";
  89. } else {
  90. // Diff output fine, clean up any illegal UTF-8
  91. $diffText = UtfNormal::cleanUp( $diffText );
  92. $diffText = self::applyDiffStyle( $diffText );
  93. }
  94. wfProfileOut( __FUNCTION__."-dodiff" );
  95. } else {
  96. $rev = Revision::newFromId( $newid );
  97. if( is_null( $rev ) ) {
  98. $newtext = '';
  99. } else {
  100. $newtext = $rev->getText();
  101. }
  102. $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
  103. '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
  104. }
  105. $completeText .= $diffText;
  106. }
  107. wfProfileOut( __FUNCTION__ );
  108. return $completeText;
  109. }
  110. /**
  111. * Hacky application of diff styles for the feeds.
  112. * Might be 'cleaner' to use DOM or XSLT or something,
  113. * but *gack* it's a pain in the ass.
  114. *
  115. * @param $text String:
  116. * @return string
  117. * @private
  118. */
  119. public static function applyDiffStyle( $text ) {
  120. $styles = array(
  121. 'diff' => 'background-color: white; color:black;',
  122. 'diff-otitle' => 'background-color: white; color:black;',
  123. 'diff-ntitle' => 'background-color: white; color:black;',
  124. 'diff-addedline' => 'background: #cfc; color:black; font-size: smaller;',
  125. 'diff-deletedline' => 'background: #ffa; color:black; font-size: smaller;',
  126. 'diff-context' => 'background: #eee; color:black; font-size: smaller;',
  127. 'diffchange' => 'color: red; font-weight: bold; text-decoration: none;',
  128. );
  129. foreach( $styles as $class => $style ) {
  130. $text = preg_replace( "/(<[^>]+)class=(['\"])$class\\2([^>]*>)/",
  131. "\\1style=\"$style\"\\3", $text );
  132. }
  133. return $text;
  134. }
  135. }