script to renew websub subscriptions v0.2.0
authorChristian Weiske <cweiske@cweiske.de>
Fri, 25 Nov 2016 06:54:49 +0000 (07:54 +0100)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 25 Nov 2016 06:56:16 +0000 (07:56 +0100)
README.rst
bin/renew-subscriptions.php [new file with mode: 0755]
bin/subscribe.php
src/phinde/Subscriptions.php

index e34cd15225ea71d9ac32877f4b7e9086720ef7b0..10083614c007a200c402e37af6e06b3389543c81 100644 (file)
@@ -30,6 +30,7 @@ Features
   - or use the ``site`` GET parameter:
     ``/?q=foo&site=example.org/dir``
 - OpenSearch support with HTML and Atom result lists
   - or use the ``site`` GET parameter:
     ``/?q=foo&site=example.org/dir``
 - OpenSearch support with HTML and Atom result lists
+* Instant indexing with WebSub (formerly PubSubHubbub)
 
 
 ============
 
 
 ============
@@ -42,6 +43,16 @@ Dependencies
 - Net_URL2
 
 
 - Net_URL2
 
 
+=====
+Setup
+=====
+This section is incomplete.
+
+Cron job
+========
+Run ``bin/renew-subscriptions.php`` once a day with cron.
+
+
 ============
 About phinde
 ============
 ============
 About phinde
 ============
diff --git a/bin/renew-subscriptions.php b/bin/renew-subscriptions.php
new file mode 100755 (executable)
index 0000000..16bc11b
--- /dev/null
@@ -0,0 +1,16 @@
+#!/usr/bin/env php
+<?php
+namespace phinde;
+/**
+ * Renew subscriptions
+ * Call this once a day with cron.
+ */
+require_once __DIR__ . '/../src/init.php';
+
+chdir(__DIR__);
+$subDb = new Subscriptions();
+foreach ($subDb->getExpiring() as $sub) {
+    Log::info('Expires soon: ' . $sub['sub_topic']);
+    passthru('./subscribe.php ' . escapeshellarg($sub['sub_topic']));
+}
+?>
index 768ee102a0182967c2fd8856de9f6a460d2b45b4..411fa8671cb86dfde43ec9c56eb9494e0f7be122 100755 (executable)
@@ -43,12 +43,16 @@ if ($topic != $url) {
 }
 
 $sub = $subDb->get($topic);
 }
 
 $sub = $subDb->get($topic);
-if ($sub !== false) {
-    Log::error('Topic exists already in subscription table');
-    Log::info('Current status: ' . $sub->sub_status);
-    exit(3);
+if ($sub === false) {
+    $subDb->create($topic);
+} else {
+    Log::info(
+        'Topic exists already in subscription table with status '
+        . $sub->sub_status
+    );
+    Log::info('Renewing subscription...');
+    $subDb->renew($sub->sub_id);
 }
 }
-$subDb->create($topic);
 $sub = $subDb->get($topic);
 
 $callbackUrl = $GLOBALS['phinde']['baseurl'] . 'push-subscription.php'
 $sub = $subDb->get($topic);
 
 $callbackUrl = $GLOBALS['phinde']['baseurl'] . 'push-subscription.php'
index 5aac9b25d1200d7b99c4cf0b371f7474b29f8f97..87119c8b2cb2e9bc73e1d74256255d66e98688dc 100644 (file)
@@ -59,6 +59,23 @@ class Subscriptions
         return $res;
     }
 
         return $res;
     }
 
+    /**
+     * Get all topics that either expired or expire soon
+     *
+     * @return \PDOStatement Result iterator
+     */
+    public function getExpiring()
+    {
+        $stmt = $this->db->prepare(
+            'SELECT * FROM subscriptions'
+            . ' WHERE sub_status IN ("active", "expired")'
+            . ' AND DATEDIFF(sub_expires, NOW()) <= 2'
+        );
+        $stmt->execute();
+
+        return $stmt;
+    }
+
     /**
      * Create a new subscription entry in database.
      * Automatically generates secret, capkey and lease seconds.
     /**
      * Create a new subscription entry in database.
      * Automatically generates secret, capkey and lease seconds.
@@ -94,6 +111,23 @@ class Subscriptions
         );
     }
 
         );
     }
 
+    /**
+     * Renew a subscription: Set its status to "subscribing"
+     *
+     * @param integer $subId Subscription ID
+     *
+     * @return void
+     */
+    public function renew($subId)
+    {
+        $this->db->prepare(
+            'UPDATE subscriptions'
+            . ' SET sub_status  = "subscribing"'
+            . '   , sub_updated = NOW()'
+            . ' WHERE sub_id = :id'
+        )->execute([':id' => $subId]);
+    }
+
     /**
      * A subscription has been confirmed by the hub - mark it as active.
      *
     /**
      * A subscription has been confirmed by the hub - mark it as active.
      *
@@ -137,6 +171,14 @@ class Subscriptions
         )->execute([':id' => $subId]);
     }
 
         )->execute([':id' => $subId]);
     }
 
+    /**
+     * Subscription has been cancelled/denied for some reason
+     *
+     * @param integer $subId  Subscription ID
+     * @param string  $reason Cancellation reason
+     *
+     * @return void
+     */
     public function denied($subId, $reason)
     {
         $this->db->prepare(
     public function denied($subId, $reason)
     {
         $this->db->prepare(
@@ -148,6 +190,13 @@ class Subscriptions
         )->execute([':id' => $subId, ':reason' => $reason]);
     }
 
         )->execute([':id' => $subId, ':reason' => $reason]);
     }
 
+    /**
+     * Topic update notification has been received
+     *
+     * @param integer $subId  Subscription ID
+     *
+     * @return void
+     */
     public function pinged($subId)
     {
         $this->db->prepare(
     public function pinged($subId)
     {
         $this->db->prepare(