]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/RSSCloud/RSSCloudPlugin.php
added readme
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudPlugin.php
index 816046ffbb4d89b64dd36534f5f5ad7c3ef59bcf..661c32141faedf7a3f23c4edf86036543b3365df 100644 (file)
@@ -33,32 +33,57 @@ if (!defined('STATUSNET')) {
 
 define('RSSCLOUDPLUGIN_VERSION', '0.1');
 
-class RSSCloudPlugin extends Plugin 
+/**
+ * Plugin class for adding RSSCloud capabilities to StatusNet
+ *
+ * @category Plugin
+ * @package  StatusNet
+ * @author   Zach Copley <zach@status.net>
+ * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link     http://status.net/
+ **/
+
+class RSSCloudPlugin extends Plugin
 {
+    /**
+     * Our friend, the constructor
+     *
+     * @return void
+     */
     function __construct()
     {
         parent::__construct();
     }
-    
-    function onInitializePlugin(){
+
+    /**
+     * Setup the info for the subscription handler. Allow overriding
+     * to point at another cloud hub (not currently used).
+     *
+     * @return void
+     */
+
+    function onInitializePlugin()
+    {
         $this->domain   = common_config('rsscloud', 'domain');
         $this->port     = common_config('rsscloud', 'port');
         $this->path     = common_config('rsscloud', 'path');
         $this->funct    = common_config('rsscloud', 'function');
         $this->protocol = common_config('rsscloud', 'protocol');
-        
+
         // set defaults
-        
+
+        $local_server = parse_url(common_path('main/rsscloud/request_notify'));
+
         if (empty($this->domain)) {
-            $this->domain = 'rpc.rsscloud.org';
+            $this->domain = $local_server['host'];
         }
-           
+
         if (empty($this->port)) {
-            $this->port = '5337';
+            $this->port = '80';
         }
-            
+
         if (empty($this->path)) {
-            $this->path = '/rsscloud/pleaseNotify';
+            $this->path = $local_server['path'];
         }
 
         if (empty($this->funct)) {
@@ -69,101 +94,175 @@ class RSSCloudPlugin extends Plugin
             $this->protocol = 'http-post';
         }
     }
-    
-    function onStartApiRss($action){
-        
-        $attrs = array('domain'            => $this->domain,
-                       'port'              => $this->port,
-                       'path'              => $this->path,
-                       'registerProcedure' => $this->funct,
-                       'protocol'          => $this->protocol);
-
-        // Dipping into XMLWriter to avoid a full end element (</cloud>).
-        
-        $action->xw->startElement('cloud');
-        foreach ($attrs as $name => $value) {
-            $action->xw->writeAttribute($name, $value);
-        }
-        $action->xw->endElement('cloud');
-        
+
+    /**
+     * Add RSSCloud-related paths to the router table
+     *
+     * Hook for RouterInitialized event.
+     *
+     * @param Mapper $m URL parser and mapper
+     *
+     * @return boolean hook return
+     */
+
+    function onRouterInitialized($m)
+    {
+        $m->connect('/main/rsscloud/request_notify',
+                    array('action' => 'RSSCloudRequestNotify'));
+
+        // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
+        //      to be a cloud hub for some reason.
+        //$m->connect('/main/rsscloud/notify',
+        //            array('action' => 'LoggingAggregator'));
+
+        return true;
     }
 
-    function onEndNoticeSave($notice){
+    /**
+     * Automatically load the actions and libraries used by
+     * the RSSCloud plugin
+     *
+     * @param Class $cls the class
+     *
+     * @return boolean hook return
+     *
+     */
 
-        $user = User::staticGet('id', $notice->profile_id);
-        $rss  = common_local_url('api', array('apiaction' => 'statuses',
-                                              'method'    => 'user_timeline',
-                                              'argument'  => $user->nickname . '.rss'));
-        $notifier = new CloudNotifier();
-        $notifier->notify($rss);
+    function onAutoload($cls)
+    {
+        switch ($cls)
+        {
+        case 'RSSCloudSubscription':
+            include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php';
+            return false;
+        case 'RSSCloudNotifier':
+            include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php';
+            return false;
+        case 'RSSCloudQueueHandler':
+            include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudQueueHandler.php';
+            return false;
+        case 'RSSCloudRequestNotifyAction':
+        case 'LoggingAggregatorAction':
+            include_once INSTALLDIR . '/plugins/RSSCloud/' .
+              mb_substr($cls, 0, -6) . '.php';
+            return false;
+        default:
+            return true;
+        }
     }
-    
 
-}
+    /**
+     * Add a <cloud> element to the RSS feed (after the rss <channel>
+     * element is started).
+     *
+     * @param Action $action the ApiAction
+     *
+     * @return void
+     */
 
+    function onStartApiRss($action)
+    {
+        if (get_class($action) == 'ApiTimelineUserAction') {
+
+            $attrs = array('domain'            => $this->domain,
+                           'port'              => $this->port,
+                           'path'              => $this->path,
+                           'registerProcedure' => $this->funct,
+                           'protocol'          => $this->protocol);
+
+            // Dipping into XMLWriter to avoid a full end element (</cloud>).
+
+            $action->xw->startElement('cloud');
+            foreach ($attrs as $name => $value) {
+                $action->xw->writeAttribute($name, $value);
+            }
 
-class CloudNotifier {
-
-    function notify($feed) {
-        common_debug("CloudNotifier->notify: $feed");
-        
-        $params = 'url=' . urlencode($feed);
-        
-        $result = $this->httpPost('http://rpc.rsscloud.org:5337/rsscloud/ping', 
-                                  $params);
-        
-        if ($result) {
-            common_debug('success notifying cloud');
-        } else {
-            common_debug('failure notifying cloud');
+            $action->xw->endElement();
         }
+    }
+
+    /**
+     * Add an RSSCloud queue item for each notice
+     *
+     * @param Notice $notice      the notice
+     * @param array  &$transports the list of transports (queues)
+     *
+     * @return boolean hook return
+     */
 
+    function onStartEnqueueNotice($notice, &$transports)
+    {
+        array_push($transports, 'rsscloud');
+        return true;
     }
-    
-    function userAgent()
+
+    /**
+     * Determine whether the notice was locally created
+     *
+     * @param Notice $notice the notice in question
+     *
+     * @return boolean locality
+     */
+
+    function _isLocal($notice)
     {
-        return 'rssCloudPlugin/' . RSSCLOUDPLUGIN_VERSION .
-          ' StatusNet/' . STATUSNET_VERSION;
+        return ($notice->is_local == Notice::LOCAL_PUBLIC ||
+                $notice->is_local == Notice::LOCAL_NONPUBLIC);
     }
-    
-    
-    private function httpPost($url, $params) {
-        
-        
-        common_debug('params: ' . var_export($params, true));
-                
-        $options = array(CURLOPT_URL            => $url,
-                         CURLOPT_POST           => true,
-                         CURLOPT_POSTFIELDS     => $params,
-                         CURLOPT_USERAGENT      => $this->userAgent(),
-                         CURLOPT_RETURNTRANSFER => true,
-                         CURLOPT_FAILONERROR    => true,
-                         CURLOPT_HEADER         => false,
-                         CURLOPT_FOLLOWLOCATION => true,
-                         CURLOPT_CONNECTTIMEOUT => 5,
-                         CURLOPT_TIMEOUT        => 5);
-
-        $ch = curl_init();
-        curl_setopt_array($ch, $options);
-        
-        $response = curl_exec($ch);
-    
-
-    
-        $info = curl_getinfo($ch);
-        
-        curl_close($ch);
-        
-        common_debug('curl response: ' . var_export($response, true));
-        common_debug('curl info: ' . var_export($info, true));
-        
-        if ($info['http_code'] == 200) {
-            return true;
-        } else {
-            return false;
-        }
+
+    /**
+     * Create the rsscloud_subscription table if it's not
+     * already in the DB
+     *
+     * @return boolean hook return
+     */
+
+    function onCheckSchema()
+    {
+        $schema = Schema::get();
+        $schema->ensureTable('rsscloud_subscription',
+                             array(new ColumnDef('subscribed', 'integer',
+                                                 null, false, 'PRI'),
+                                   new ColumnDef('url', 'varchar',
+                                                 '255', false, 'PRI'),
+                                   new ColumnDef('failures', 'integer',
+                                                 null, false, null, 0),
+                                   new ColumnDef('created', 'datetime',
+                                                 null, false),
+                                   new ColumnDef('modified', 'timestamp',
+                                                 null, false, null,
+                                                 'CURRENT_TIMESTAMP',
+                                                 'on update CURRENT_TIMESTAMP')
+                                   ));
+         return true;
+    }
+
+    /**
+     * Register RSSCloud notice queue handler
+     *
+     * @param QueueManager $manager
+     *
+     * @return boolean hook return
+     */
+    function onEndInitializeQueueManager($manager)
+    {
+        $manager->connect('rsscloud', 'RSSCloudQueueHandler');
+        return true;
+    }
+
+    function onPluginVersion(&$versions)
+    {
+        $versions[] = array('name' => 'RSSCloud',
+                            'version' => RSSCLOUDPLUGIN_VERSION,
+                            'author' => 'Zach Copley',
+                            'homepage' => 'http://status.net/wiki/Plugin:RSSCloud',
+                            'rawdescription' =>
+                            _m('The RSSCloud plugin enables your StatusNet instance to publish ' .
+                               'real-time updates for profile RSS feeds using the ' .
+                               '<a href="http://rsscloud.org/">RSSCloud protocol</a>".'));
+
+        return true;
     }
-    
-}
\ No newline at end of file
+
+}
+