FeedUtils.php 7.9 KB

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