awjrichards has submitted this change and it was merged.

Change subject: Allow weekly updated feeds
......................................................................


Allow weekly updated feeds

The $wgFeaturedFeedsDefault parameter now accept a new option allowing to tune 
feed update frequency.
Option key: limit
Option value: daily or weekly.
Default value: daily

Change-Id: Ib094bea189be3ee166a2467674675e3c8cc650c9
---
M FeaturedFeeds.body.php
M FeaturedFeeds.php
2 files changed, 52 insertions(+), 8 deletions(-)

Approvals:
  awjrichards: Verified; Looks good to me, approved



diff --git a/FeaturedFeeds.body.php b/FeaturedFeeds.body.php
index 821a4df..a09e3ad 100644
--- a/FeaturedFeeds.body.php
+++ b/FeaturedFeeds.body.php
@@ -85,6 +85,7 @@
        public static function beforePageDisplay( OutputPage &$out ) {
                global $wgAdvertisedFeedTypes;
                if ( $out->getTitle()->isMainPage() ) {
+                       /** @var FeaturedFeedChannel $feed */
                        foreach ( self::getFeeds( 
$out->getLanguage()->getCode() ) as $feed ) {
                                foreach ( $wgAdvertisedFeedTypes as $type ) {
                                        $out->addLink( array(
@@ -115,6 +116,7 @@
                        $feeds = self::getFeeds( 
$sk->getContext()->getLanguage()->getCode() );
                        $links = array();
                        $format = $wgAdvertisedFeedTypes[0]; // @fixme:
+                       /** @var FeaturedFeedChannel $feed */
                        foreach ( $feeds as $feed ) {
                                $links[] = array(
                                        'href' => $feed->getURL( $format ),
@@ -200,21 +202,50 @@
        }
 
        /**
+        * Returns the Unix timestamp of current week's first second
+        *
+        * @return int Timestamp
+        */
+       public static function startOfThisWeek() {
+               static $time = false;
+               if ( !$time ) {
+                       $dt = new DateTime( 'this week', self::getTimezone() );
+                       $dt->setTime( 0, 0, 0 );
+                       $time = $dt->getTimestamp();
+               }
+               return $time;
+       }
+
+       /**
         * Returns the Unix timestamp of current day's first second
         *
         * @param $timestamp
         * @return int Timestamp
         */
        public static function startOfDay( $timestamp ) {
-               global $wgLocaltimezone;
-               if ( isset( $wgLocaltimezone ) ) {
-                       $tz = new DateTimeZone( $wgLocaltimezone );
-               } else {
-                       $tz = new DateTimeZone( date_default_timezone_get() );
-               }
-               $dt = new DateTime( "@$timestamp", $tz );
+               $dt = new DateTime( "@$timestamp", self::getTimezone() );
                $dt->setTime( 0, 0, 0 );
                return $dt->getTimestamp();
+       }
+
+       /**
+        * @return DateTimeZone
+        */
+       private static function getTimezone() {
+               global $wgLocaltimezone;
+               static $timeZone;
+
+               if ( $timeZone === null ) {
+                       if ( isset( $wgLocaltimezone ) ) {
+                               $tz = $wgLocaltimezone;
+                       } else {
+                               wfSuppressWarnings();
+                               $tz = date_default_timezone_get();
+                               wfRestoreWarnings();
+                       }
+                       $timeZone = new DateTimeZone( $tz );
+               }
+               return $timeZone;
        }
 
        /**
@@ -343,8 +374,20 @@
                $this->init();
                if ( $this->items === false ) {
                        $this->items = array();
+                       switch ( $this->options['frequency'] ) {
+                               case 'daily':
+                                       $ratio = 1;
+                                       $baseTime = 
FeaturedFeeds::todaysStart();
+                                       break;
+                               case 'weekly':
+                                       $ratio = 7;
+                                       $baseTime = 
FeaturedFeeds::startOfThisWeek();
+                                       break;
+                               default:
+                                       throw new MWException( 
"'{$this->options['frequency']}' is not a valid frequency" );
+                       }
                        for ( $i = 1 - $this->options['limit']; $i <= 0; $i++ ) 
{
-                               $timestamp = FeaturedFeeds::todaysStart() + $i 
* 24 * 3600;
+                               $timestamp = $baseTime + $i * $ratio * 24 * 
3600;
                                $item = $this->getFeedItem( $timestamp );
                                if ( $item ) {
                                        $this->items[] = $item;
diff --git a/FeaturedFeeds.php b/FeaturedFeeds.php
index 6e26b8e..d3175e6 100644
--- a/FeaturedFeeds.php
+++ b/FeaturedFeeds.php
@@ -41,6 +41,7 @@
 
 $wgFeaturedFeedsDefaults = array(
        'limit' => 10,
+       'frequency' => 'daily',
        'inUserLanguage' => false,
 );
 

-- 
To view, visit https://gerrit.wikimedia.org/r/23108
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ib094bea189be3ee166a2467674675e3c8cc650c9
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/FeaturedFeeds
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
Gerrit-Reviewer: Dereckson <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: awjrichards <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to