Add support for Number grouping(commafy) based on CLDR number grouping patterns like...
[lhc/web/wiklou.git] / includes / FeedUtils.php
index 4412a14..b6df0c6 100644 (file)
@@ -1,8 +1,20 @@
 <?php
 
-// TODO: document
+/**
+ * Helper functions for feeds
+ *
+ * @ingroup Feed
+ */
 class FeedUtils {
 
+       /**
+        * Check whether feed's cache should be cleared; for changes feeds
+        * If the feed should be purged; $timekey and $key will be removed from
+        * $messageMemc
+        *
+        * @param $timekey String: cache key of the timestamp of the last item
+        * @param $key String: cache key of feed's content
+        */
        public static function checkPurge( $timekey, $key ) {
                global $wgRequest, $wgUser, $messageMemc;
                $purge = $wgRequest->getVal( 'action' ) === 'purge';
@@ -12,17 +24,22 @@ class FeedUtils {
                }
        }
 
+       /**
+        * Check whether feeds can be used and that $type is a valid feed type
+        *
+        * @param $type String: feed type, as requested by the user
+        * @return Boolean
+        */
        public static function checkFeedOutput( $type ) {
-               global $wgFeed, $wgOut, $wgFeedClasses;
+               global $wgOut, $wgFeed, $wgFeedClasses;
 
                if ( !$wgFeed ) {
-                       global $wgOut;
                        $wgOut->addWikiMsg( 'feed-unavailable' );
                        return false;
                }
 
                if( !isset( $wgFeedClasses[$type] ) ) {
-                       wfHttpError( 500, "Internal Server Error", "Unsupported feed type." );
+                       $wgOut->addWikiMsg( 'feed-invalid' );
                        return false;
                }
 
@@ -30,8 +47,11 @@ class FeedUtils {
        }
 
        /**
-       * Format a diff for the newsfeed
-       */
+        * Format a diff for the newsfeed
+        *
+        * @param $row Object: row from the recentchanges table
+        * @return String
+        */
        public static function formatDiff( $row ) {
                global $wgUser;
 
@@ -53,16 +73,28 @@ class FeedUtils {
                        $actiontext );
        }
 
+       /**
+        * Really format a diff for the newsfeed
+        *
+        * @param $title Title object
+        * @param $oldid Integer: old revision's id
+        * @param $newid Integer: new revision's id
+        * @param $timestamp Integer: new revision's timestamp
+        * @param $comment String: new revision's comment
+        * @param $actiontext String: text of the action; in case of log event
+        * @return String
+        */
        public static function formatDiffRow( $title, $oldid, $newid, $timestamp, $comment, $actiontext='' ) {
-               global $wgFeedDiffCutoff, $wgContLang, $wgUser;
-               wfProfileIn( __FUNCTION__ );
+               global $wgFeedDiffCutoff, $wgLang, $wgUser;
+               wfProfileIn( __METHOD__ );
 
                $skin = $wgUser->getSkin();
                # log enties
-               if( $actiontext ) {
-                       $comment = "$actiontext $comment";
-               }
-               $completeText = '<p>' . $skin->formatComment( $comment ) . "</p>\n";
+               $completeText = '<p>' . implode( ' ',
+                       array_filter(
+                               array(
+                                       $actiontext,
+                                       $skin->formatComment( $comment ) ) ) ) . "</p>\n";
 
                //NOTE: Check permissions for anonymous users, not current user.
                //      No "privileged" version should end up in the cache.
@@ -70,21 +102,28 @@ class FeedUtils {
                $anon = new User();
                $accErrors = $title->getUserPermissionsErrors( 'read', $anon, true );
 
-               if( $title->getNamespace() >= 0 && !$accErrors ) {
+               if( $title->getNamespace() >= 0 && !$accErrors && $newid ) {
                        if( $oldid ) {
-                               wfProfileIn( __FUNCTION__."-dodiff" );
+                               wfProfileIn( __METHOD__."-dodiff" );
 
-                               $de = new DifferenceEngine( $title, $oldid, $newid );
                                #$diffText = $de->getDiff( wfMsg( 'revisionasof',
-                               #       $wgContLang->timeanddate( $timestamp ) ),
+                               #       $wgLang->timeanddate( $timestamp ),
+                               #       $wgLang->date( $timestamp ),
+                               #       $wgLang->time( $timestamp ) ),
                                #       wfMsg( 'currentrev' ) );
-                               $diffText = $de->getDiff(
-                                       wfMsg( 'previousrevision' ), // hack
-                                       wfMsg( 'revisionasof',
-                                               $wgContLang->timeanddate( $timestamp ) ) );
 
+                               // Don't bother generating the diff if we won't be able to show it
+                               if ( $wgFeedDiffCutoff > 0 ) {
+                                       $de = new DifferenceEngine( $title, $oldid, $newid );
+                                       $diffText = $de->getDiff(
+                                               wfMsg( 'previousrevision' ), // hack
+                                               wfMsg( 'revisionasof',
+                                                       $wgLang->timeanddate( $timestamp ),
+                                                       $wgLang->date( $timestamp ),
+                                                       $wgLang->time( $timestamp ) ) );
+                               }
 
-                               if ( strlen( $diffText ) > $wgFeedDiffCutoff ) {
+                               if ( $wgFeedDiffCutoff <= 0 || ( strlen( $diffText ) > $wgFeedDiffCutoff ) ) {
                                        // Omit large diffs
                                        $diffLink = $title->escapeFullUrl(
                                                'diff=' . $newid .
@@ -92,7 +131,7 @@ class FeedUtils {
                                        $diffText = '<a href="' .
                                                $diffLink .
                                                '">' .
-                                               htmlspecialchars( wfMsgForContent( 'difference' ) ) .
+                                               htmlspecialchars( wfMsgForContent( 'showdiff' ) ) .
                                                '</a>';
                                } elseif ( $diffText === false ) {
                                        // Error in diff engine, probably a missing revision
@@ -102,7 +141,7 @@ class FeedUtils {
                                        $diffText = UtfNormal::cleanUp( $diffText );
                                        $diffText = self::applyDiffStyle( $diffText );
                                }
-                               wfProfileOut( __FUNCTION__."-dodiff" );
+                               wfProfileOut( __METHOD__."-dodiff" );
                        } else {
                                $rev = Revision::newFromId( $newid );
                                if( is_null( $rev ) ) {
@@ -116,19 +155,18 @@ class FeedUtils {
                        $completeText .= $diffText;
                }
 
-               wfProfileOut( __FUNCTION__ );
+               wfProfileOut( __METHOD__ );
                return $completeText;
        }
 
        /**
-       * Hacky application of diff styles for the feeds.
-       * Might be 'cleaner' to use DOM or XSLT or something,
-       * but *gack* it's a pain in the ass.
-       *
-       * @param $text String:
-       * @return string
-       * @private
-       */
+        * Hacky application of diff styles for the feeds.
+        * Might be 'cleaner' to use DOM or XSLT or something,
+        * but *gack* it's a pain in the ass.
+        *
+        * @param $text String: diff's HTML output
+        * @return String: modified HTML
+        */
        public static function applyDiffStyle( $text ) {
                $styles = array(
                        'diff'             => 'background-color: white; color:black;',
@@ -148,4 +186,4 @@ class FeedUtils {
                return $text;
        }
 
-}
\ No newline at end of file
+}