Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

implement pubsub notifications

  • Loading branch information...
commit c29e841967cc6f241b8712b4c3d6bbbd2a8b37ae 1 parent 5485d09
authored January 08, 2010
189  libs/lx/notifier.php
... ...
@@ -0,0 +1,189 @@
  1
+<?php
  2
+/**
  3
+  Copyright 2009 Liip AG
  4
+
  5
+   Licensed under the Apache License, Version 2.0 (the "License");
  6
+   you may not use this file except in compliance with the License.
  7
+   You may obtain a copy of the License at
  8
+
  9
+       http://www.apache.org/licenses/LICENSE-2.0
  10
+
  11
+   Unless required by applicable law or agreed to in writing, software
  12
+   distributed under the License is distributed on an "AS IS" BASIS,
  13
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14
+   See the License for the specific language governing permissions and
  15
+   limitations under the License.
  16
+
  17
+   Code partly copied from Padraic Brady's Zend_Feed_Pubsubhubbub
  18
+*/
  19
+
  20
+/**
  21
+    Example
  22
+
  23
+    $noti = new lx_notifier();
  24
+
  25
+    $blogname = "kleiner test";
  26
+    $url = "http://example.org/";
  27
+    $checkurl = $url;
  28
+
  29
+    $mainfeed = $url ."rss.xml";
  30
+    $topicurls = array(
  31
+            $url . 'atom.xml',
  32
+            $mainfeed
  33
+    );
  34
+    $services = array("http://rpc.pingomatic.com/");
  35
+    $noti->addWeblogUpdates(array($mainfeed), $services, $url, $blogname, $checkurl );
  36
+
  37
+    $clouds = array('http://rpc.rsscloud.org:5337/rsscloud/ping');
  38
+    $noti->addRssClouds($topicurls,$clouds);
  39
+
  40
+    $hubs = array("http://pubsubhubbub.appspot.com");
  41
+    $noti->addPubSubHubs($topicurls, $hubs);
  42
+
  43
+    $supid = getSUPId();
  44
+    $noti->addSup("http://friendfeed.com/api/public-sup-ping?supid=" . $supid . "&url=" . $mainfeed);
  45
+
  46
+    $noti->notifyAll();
  47
+
  48
+ */
  49
+
  50
+class lx_notifier {
  51
+
  52
+    protected $multi = null;
  53
+    protected $chs = array();
  54
+
  55
+    public function __construct() {
  56
+        $this->multi = curl_multi_init();
  57
+
  58
+    }
  59
+
  60
+    public function addPubSubHubs($topicUrls = array(), $huburls = array("http://pubsubhubbub.appspot.com")) {
  61
+        $params = array();
  62
+        $params[] = 'hub.mode=publish';
  63
+        $topics = $topicUrls;
  64
+        foreach ($topics as $topicUrl) {
  65
+            $params[] = 'hub.url=' . urlencode($topicUrl);
  66
+        }
  67
+        $paramString = implode('&', $params);
  68
+        $success = 0;
  69
+        foreach ($huburls as $huburl) {
  70
+            $success += $this->_notifyPubSub($paramString, $huburl);
  71
+        }
  72
+        return $success;
  73
+    }
  74
+
  75
+    protected function _notifyPubSub($postdata, $huburl) {
  76
+        $ch = $this->getDefaultCurl($huburl);
  77
+        curl_setopt($ch, CURLOPT_POST, true);
  78
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  79
+        return $this->addCurl($ch);
  80
+    }
  81
+
  82
+
  83
+    public function addWeblogUpdates($topicurls, $pingurls, $url, $name,  $checkUrl) {
  84
+        foreach ($topicurls as $topicurl) {
  85
+            foreach ($pingurls as $pingurl) {
  86
+                $this->addWeblogUpdate($topicurl, $pingurl, $url, $name, $checkUrl);
  87
+            }
  88
+        }
  89
+    }
  90
+
  91
+    protected function addWeblogUpdate($topicurl, $pingurl, $url, $name,  $checkurl) {
  92
+        $ch = $this->getDefaultCurl($pingurl);
  93
+        $postdata =
  94
+
  95
+        '<?xml version="1.0" encoding="UTF-8"?>
  96
+        <methodCall>
  97
+        <methodName>weblogUpdates.extendedPing</methodName>
  98
+        <params>
  99
+        <param>
  100
+        <value><string>' . htmlspecialchars($name) . '</string></value>
  101
+        </param>
  102
+        <param>
  103
+        <value><string>' . htmlspecialchars($url) . '</string></value>
  104
+        </param>
  105
+        <param>
  106
+        <value><string>' . htmlspecialchars($checkurl) . '</string></value>
  107
+        </param>
  108
+        <param>
  109
+        <value><string>' . htmlspecialchars($topicurl) . '</string></value>
  110
+        </param>
  111
+        </params>
  112
+        </methodCall>';
  113
+
  114
+        curl_setopt($ch, CURLOPT_POST, true);
  115
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  116
+        return $this->addCurl($ch);
  117
+
  118
+    }
  119
+
  120
+    public function addSup($supurl) {
  121
+        $ch = $this->getDefaultCurl($supurl);
  122
+        return $this->addCurl($ch);
  123
+    }
  124
+
  125
+    public function addRssClouds($topicurls, $cloudurls) {
  126
+        foreach ($cloudurls as $url) {
  127
+            foreach ($topicurls as $topicurl) {
  128
+                $this->addRssCloud($topicurl, $url);
  129
+            }
  130
+        }
  131
+    }
  132
+
  133
+    public function addRssCloud($topicurl, $cloudurl) {
  134
+        $ch = $this->getDefaultCurl($cloudurl);
  135
+        $postdata = 'url=' . urlencode($topicurl);
  136
+
  137
+        curl_setopt($ch, CURLOPT_POST, true);
  138
+        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
  139
+        return $this->addCurl($ch);
  140
+    }
  141
+
  142
+
  143
+    protected function getDefaultCurl($url) {
  144
+        $ch = curl_init();
  145
+        curl_setopt($ch, CURLOPT_URL, $url);
  146
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  147
+        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
  148
+        curl_setopt($ch, CURLOPT_HEADER, false);
  149
+        curl_setopt($ch, CURLOPT_TIMEOUT, 2);
  150
+        return $ch;
  151
+    }
  152
+
  153
+    protected function addCurl($ch) {
  154
+        if ($this->multi) {
  155
+            curl_multi_add_handle($this->multi, $ch);
  156
+            $this->chs[] = $ch;
  157
+        } else {
  158
+            curl_exec($ch);
  159
+            $info = curl_getinfo($ch);
  160
+            curl_close($ch);
  161
+            if ($info['http_code'] != 204) {
  162
+                return 0;
  163
+            }
  164
+        }
  165
+        return 1;
  166
+    }
  167
+
  168
+    public function notifyAll() {
  169
+        if ($this->multi) {
  170
+            $running = null;
  171
+            //execute the handles
  172
+            curl_multi_exec($this->multi, $running);
  173
+            while ($running > 0) {
  174
+                usleep(100000); // 1/10 sec
  175
+                curl_multi_exec($this->multi, $running);
  176
+            }
  177
+
  178
+        }
  179
+
  180
+        foreach ($this->chs as $ch) {
  181
+            //error_log(var_Export(curl_getinfo($ch), true));
  182
+            curl_multi_remove_handle($this->multi, $ch);
  183
+            curl_close($ch);
  184
+        }
  185
+        curl_multi_close($this->multi);
  186
+
  187
+    }
  188
+
  189
+}
11  libs/scripts/aggregate.php
@@ -5,3 +5,14 @@
5 5
 $agg = new aggregator();
6 6
 $agg->aggregateAllBlogs($argv[1]);
7 7
 
  8
+$noti = new lx_notifier();
  9
+
  10
+$url = "http://www.planet-php.net/";
  11
+$topicurls = array(
  12
+        $url . 'atom/',
  13
+);
  14
+
  15
+$hubs = array("http://pubsubhubbub.appspot.com");
  16
+$noti->addPubSubHubs($topicurls, $hubs);
  17
+    $noti->notifyAll();
  18
+
1  xsl/atom1.xsl
@@ -31,6 +31,7 @@
31 31
             <updated>
32 32
                 <xsl:value-of select="/planet/entries/entry[1]/date_iso"/>
33 33
             </updated>
  34
+            <link rel="hub" href="http://pubsubhubbub.appspot.com"/>
34 35
 
35 36
             <xsl:apply-templates select="/planet/entries/entry"/>
36 37
         </feed>

0 notes on commit c29e841

Please sign in to comment.
Something went wrong with that request. Please try again.