From 5959bcb0a34927682b349eda557dda2be33290c3 Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Wed, 15 Sep 2010 19:40:57 +0000
Subject: [PATCH] #867892 alex_b: PubSubHubbub - slow down import frequency of
 feeds that are subscribed to hub.

---
 CHANGELOG.txt                |  2 ++
 includes/FeedsSource.inc     |  8 +++++++-
 plugins/FeedsFetcher.inc     | 13 +++++++++++++
 plugins/FeedsHTTPFetcher.inc |  9 +++++++++
 4 files changed, 31 insertions(+), 1 deletion(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index 5bff6e14..efb8161a 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -3,6 +3,8 @@
 Feeds 6.x xxxxxxxxxxxxxxxxxxxxxx
 --------------------------------
 
+- #867892 alex_b: PubSubHubbub - slow down import frequency of feeds that are
+  subscribed to hub.
 - #908964 alex_b: Break out scheduler. Note: Features depends on Job Scheduler
   module now: http://drupal.org/project/job_scheduler
 - #663860 funkmasterjones, infojunkie, alex_b et. al.: hook_feeds_after_parse().
diff --git a/includes/FeedsSource.inc b/includes/FeedsSource.inc
index 0ebadf06..682fdf76 100644
--- a/includes/FeedsSource.inc
+++ b/includes/FeedsSource.inc
@@ -198,12 +198,18 @@ class FeedsSource extends FeedsConfigurable {
    * Schedule this source.
    */
   public function schedule() {
+    // Check whether any fetcher is overriding the import period.
+    $period = $this->importer->config['import_period'];
+    $fetcher_period = $this->importer->fetcher->importPeriod($this);
+    if (is_numeric($fetcher_period)) {
+      $period = $fetcher_period;
+    }
     $job = array(
       'callback' => 'feeds_source_import',
       'type' => $this->id,
       'id' => $this->feed_nid,
       // Schedule as soon as possible if a batch is active.
-      'period' => $this->batch ? 0 : $this->importer->config['import_period'],
+      'period' => $this->batch ? 0 : $period,
       'periodic' => TRUE,
     );
     if ($job['period'] != FEEDS_SCHEDULE_NEVER) {
diff --git a/plugins/FeedsFetcher.inc b/plugins/FeedsFetcher.inc
index 4a42ccf0..3b0120ab 100644
--- a/plugins/FeedsFetcher.inc
+++ b/plugins/FeedsFetcher.inc
@@ -94,4 +94,17 @@ abstract class FeedsFetcher extends FeedsPlugin {
    *   Source information for unsubscribing.
    */
   public function unsubscribe(FeedsSource $source) {}
+
+  /**
+   * Override import period settings. This can be used to force a certain import
+   * interval.
+   *
+   * @param $source
+   *   A FeedsSource object.
+   *
+   * @return
+   *   A time span in seconds if periodic import should be overridden for given
+   *   $source, NULL otherwise.
+   */
+  public function importPeriod(FeedsSource $source) {}
 }
diff --git a/plugins/FeedsHTTPFetcher.inc b/plugins/FeedsHTTPFetcher.inc
index 467821f4..46b71112 100644
--- a/plugins/FeedsHTTPFetcher.inc
+++ b/plugins/FeedsHTTPFetcher.inc
@@ -192,6 +192,15 @@ class FeedsHTTPFetcher extends FeedsFetcher {
     $this->subscriber($source->feed_nid)->unsubscribe($source_config['source'], url($this->path($source->feed_nid), array('absolute' => TRUE)));
   }
 
+  /**
+   * Implement FeedsFetcher::importPeriod().
+   */
+  public function importPeriod(FeedsSource $source) {
+    if ($this->subscriber($source->feed_nid)->subscribed()) {
+      return 259200; // Delay for three days if there is a successful subscription.
+    }
+  }
+
   /**
    * Convenience method for instantiating a subscriber object.
    */
-- 
GitLab