]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudPlugin.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / RSSCloud / RSSCloudPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to support RSSCloud
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18    *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Plugin
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 define('RSSCLOUDPLUGIN_VERSION', '0.1');
35
36 /**
37  * Plugin class for adding RSSCloud capabilities to StatusNet
38  *
39  * @category Plugin
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  **/
45
46 class RSSCloudPlugin extends Plugin
47 {
48     /**
49      * Our friend, the constructor
50      *
51      * @return void
52      */
53     function __construct()
54     {
55         parent::__construct();
56     }
57
58     /**
59      * Setup the info for the subscription handler. Allow overriding
60      * to point at another cloud hub (not currently used).
61      *
62      * @return void
63      */
64
65     function onInitializePlugin()
66     {
67         $this->domain   = common_config('rsscloud', 'domain');
68         $this->port     = common_config('rsscloud', 'port');
69         $this->path     = common_config('rsscloud', 'path');
70         $this->funct    = common_config('rsscloud', 'function');
71         $this->protocol = common_config('rsscloud', 'protocol');
72
73         // set defaults
74
75         $local_server = parse_url(common_path('main/rsscloud/request_notify'));
76
77         if (empty($this->domain)) {
78             $this->domain = $local_server['host'];
79         }
80
81         if (empty($this->port)) {
82             $this->port = '80';
83         }
84
85         if (empty($this->path)) {
86             $this->path = $local_server['path'];
87         }
88
89         if (empty($this->funct)) {
90             $this->funct = '';
91         }
92
93         if (empty($this->protocol)) {
94             $this->protocol = 'http-post';
95         }
96     }
97
98     /**
99      * Add RSSCloud-related paths to the router table
100      *
101      * Hook for RouterInitialized event.
102      *
103      * @param Mapper $m URL parser and mapper
104      *
105      * @return boolean hook return
106      */
107
108     function onRouterInitialized($m)
109     {
110         $m->connect('/main/rsscloud/request_notify',
111                     array('action' => 'RSSCloudRequestNotify'));
112
113         // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
114         //      to be a cloud hub for some reason.
115         //$m->connect('/main/rsscloud/notify',
116         //            array('action' => 'LoggingAggregator'));
117
118         return true;
119     }
120
121     /**
122      * Automatically load the actions and libraries used by
123      * the RSSCloud plugin
124      *
125      * @param Class $cls the class
126      *
127      * @return boolean hook return
128      *
129      */
130
131     function onAutoload($cls)
132     {
133         switch ($cls)
134         {
135         case 'RSSCloudSubscription':
136             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php';
137             return false;
138         case 'RSSCloudNotifier':
139             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php';
140             return false;
141         case 'RSSCloudQueueHandler':
142             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudQueueHandler.php';
143             return false;
144         case 'RSSCloudRequestNotifyAction':
145         case 'LoggingAggregatorAction':
146             include_once INSTALLDIR . '/plugins/RSSCloud/' .
147               mb_substr($cls, 0, -6) . '.php';
148             return false;
149         default:
150             return true;
151         }
152     }
153
154     /**
155      * Add a <cloud> element to the RSS feed (after the rss <channel>
156      * element is started).
157      *
158      * @param Action $action the ApiAction
159      *
160      * @return void
161      */
162
163     function onStartApiRss($action)
164     {
165         if (get_class($action) == 'ApiTimelineUserAction') {
166
167             $attrs = array('domain'            => $this->domain,
168                            'port'              => $this->port,
169                            'path'              => $this->path,
170                            'registerProcedure' => $this->funct,
171                            'protocol'          => $this->protocol);
172
173             // Dipping into XMLWriter to avoid a full end element (</cloud>).
174
175             $action->xw->startElement('cloud');
176             foreach ($attrs as $name => $value) {
177                 $action->xw->writeAttribute($name, $value);
178             }
179
180             $action->xw->endElement();
181         }
182     }
183
184     /**
185      * Add an RSSCloud queue item for each notice
186      *
187      * @param Notice $notice      the notice
188      * @param array  &$transports the list of transports (queues)
189      *
190      * @return boolean hook return
191      */
192
193     function onStartEnqueueNotice($notice, &$transports)
194     {
195         if ($notice->isLocal()) {
196             array_push($transports, 'rsscloud');
197         }
198         return true;
199     }
200
201     /**
202      * Create the rsscloud_subscription table if it's not
203      * already in the DB
204      *
205      * @return boolean hook return
206      */
207
208     function onCheckSchema()
209     {
210         $schema = Schema::get();
211         $schema->ensureTable('rsscloud_subscription',
212             array(
213                 'fields' => array(
214                     'subscribed' => array('type' => 'int', 'not null' => true),
215                     'url' => array('type' => 'varchar', 'length' => '255', 'not null' => true),
216                     'failures' => array('type' => 'int', 'not null' => true, 'default' => 0),
217                     'created' => array('type' => 'datetime', 'not null' => true),
218                     'modified' => array('type' => 'timestamp', 'not null' => true),
219                 ),
220                 'primary key' => array('subscribed', 'url'),
221             ));
222          return true;
223     }
224
225     /**
226      * Register RSSCloud notice queue handler
227      *
228      * @param QueueManager $manager
229      *
230      * @return boolean hook return
231      */
232     function onEndInitializeQueueManager($manager)
233     {
234         $manager->connect('rsscloud', 'RSSCloudQueueHandler');
235         return true;
236     }
237
238     function onPluginVersion(&$versions)
239     {
240         $versions[] = array('name' => 'RSSCloud',
241                             'version' => RSSCLOUDPLUGIN_VERSION,
242                             'author' => 'Zach Copley',
243                             'homepage' => 'http://status.net/wiki/Plugin:RSSCloud',
244                             'rawdescription' =>
245                             _m('The RSSCloud plugin enables your StatusNet instance to publish ' .
246                                'real-time updates for profile RSS feeds using the ' .
247                                '<a href="http://rsscloud.org/">RSSCloud protocol</a>.'));
248
249         return true;
250     }
251
252 }