Catch excpt to avoid fatal in Message::__toString
[lhc/web/wiklou.git] / includes / FeedUtils.php
index d60d5c8..82c6e4a 100644 (file)
@@ -85,9 +85,9 @@ class FeedUtils {
                        $row->rc_last_oldid, $row->rc_this_oldid,
                        $timestamp,
                        ($row->rc_deleted & Revision::DELETED_COMMENT)
-                               ? wfMsgHtml('rev-deleted-comment') 
+                               ? wfMessage('rev-deleted-comment')->escaped()
                                : $row->rc_comment,
-                       $actiontext 
+                       $actiontext
                );
        }
 
@@ -129,23 +129,32 @@ class FeedUtils {
                if( $oldid ) {
                        wfProfileIn( __METHOD__."-dodiff" );
 
-                       #$diffText = $de->getDiff( wfMsg( 'revisionasof',
+                       #$diffText = $de->getDiff( wfMessage( 'revisionasof',
                        #       $wgLang->timeanddate( $timestamp ),
                        #       $wgLang->date( $timestamp ),
-                       #       $wgLang->time( $timestamp ) ),
-                       #       wfMsg( 'currentrev' ) );
+                       #       $wgLang->time( $timestamp ) )->text(),
+                       #       wfMessage( 'currentrev' )->text() );
 
                        $diffText = '';
                        // Don't bother generating the diff if we won't be able to show it
                        if ( $wgFeedDiffCutoff > 0 ) {
-                               $contentHandler = ContentHandler::getForTitle( $title );
-                               $de = $contentHandler->createDifferenceEngine( $title, $oldid, $newid );
-                               $diffText = $de->getDiff(
-                                       wfMsg( 'previousrevision' ), // hack
-                                       wfMsg( 'revisionasof',
-                                       $wgLang->timeanddate( $timestamp ),
-                                       $wgLang->date( $timestamp ),
-                                       $wgLang->time( $timestamp ) ) );
+                               $rev = Revision::newFromId( $oldid );
+
+                               if ( !$rev ) {
+                                       $diffText = false;
+                               } else {
+                                       $context = clone RequestContext::getMain();
+                                       $context->setTitle( $title );
+
+                                       $contentHandler = $rev->getContentHandler();
+                                       $de = $contentHandler->createDifferenceEngine( $context, $oldid, $newid );
+                                       $diffText = $de->getDiff(
+                                               wfMessage( 'previousrevision' )->text(), // hack
+                                               wfMessage( 'revisionasof',
+                                                       $wgLang->timeanddate( $timestamp ),
+                                                       $wgLang->date( $timestamp ),
+                                                       $wgLang->time( $timestamp ) )->text() );
+                               }
                        }
 
                        if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
@@ -163,16 +172,36 @@ class FeedUtils {
                } else {
                        $rev = Revision::newFromId( $newid );
                        if( $wgFeedDiffCutoff <= 0 || is_null( $rev ) ) {
-                               $newtext = '';
+                               $newContent = ContentHandler::getForTitle( $title )->makeEmptyContent();
+                       } else {
+                               $newContent = $rev->getContent();
+                       }
+
+                       if ( $newContent instanceof TextContent ) {
+                               // only textual content has a "source view".
+                               $text = $newContent->getNativeData();
+
+                               if ( $wgFeedDiffCutoff <= 0 || strlen( $text ) > $wgFeedDiffCutoff ) {
+                                       $html = null;
+                               } else {
+                                       $html = nl2br( htmlspecialchars( $text ) );
+                               }
                        } else {
-                               $newtext = $rev->getText();
+                               //XXX: we could get an HTML representation of the content via getParserOutput, but that may
+                               //     contain JS magic and generally may not be suitable for inclusion in a feed.
+                               //     Perhaps Content should have a getDescriptiveHtml method and/or a getSourceText method.
+                               //Compare also ApiFeedContributions::feedItemDesc
+                               $html = null;
                        }
-                       if ( $wgFeedDiffCutoff <= 0 || strlen( $newtext ) > $wgFeedDiffCutoff ) {
+
+                       if ( $html === null ) {
+
                                // Omit large new page diffs, bug 29110
+                               // Also use diff link for non-textual content
                                $diffText = self::getDiffLink( $title, $newid );
                        } else {
-                               $diffText = '<p><b>' . wfMsg( 'newpage' ) . '</b></p>' .
-                                       '<div>' . nl2br( htmlspecialchars( $newtext ) ) . '</div>';
+                               $diffText = '<p><b>' . wfMessage( 'newpage' )->text() . '</b></p>' .
+                                       '<div>' . $html . '</div>';
                        }
                }
                $completeText .= $diffText;
@@ -197,7 +226,7 @@ class FeedUtils {
                $diffUrl = $title->getFullUrl( $queryParameters );
 
                $diffLink = Html::element( 'a', array( 'href' => $diffUrl ),
-                       wfMsgForContent( 'showdiff' ) );
+                       wfMessage( 'showdiff' )->inContentLanguage()->text() );
 
                return $diffLink;
        }