]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudPlugin.php
Fix subscription path in link element
[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 /**
35  * Plugin class for adding RSSCloud capabilities to StatusNet
36  *
37  * @category Plugin
38  * @package  StatusNet
39  * @author   Zach Copley <zach@status.net>
40  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41  * @link     http://status.net/
42  **/
43
44 class RSSCloudPlugin extends Plugin
45 {
46     /**
47      * Our friend, the constructor
48      *
49      * @return void
50      */
51     function __construct()
52     {
53         parent::__construct();
54     }
55
56     /**
57      * Setup the info for the subscription handler. Allow overriding
58      * to point at another cloud hub (not currently used).
59      *
60      * @return void
61      */
62
63     function onInitializePlugin()
64     {
65         $this->domain   = common_config('rsscloud', 'domain');
66         $this->port     = common_config('rsscloud', 'port');
67         $this->path     = common_config('rsscloud', 'path');
68         $this->funct    = common_config('rsscloud', 'function');
69         $this->protocol = common_config('rsscloud', 'protocol');
70
71         // set defaults
72
73         $local_server = parse_url(common_path('main/rsscloud/request_notify'));
74
75         if (empty($this->domain)) {
76             $this->domain = $local_server['host'];
77         }
78
79         if (empty($this->port)) {
80             $this->port = '80';
81         }
82
83         if (empty($this->path)) {
84             $this->path = $local_server['path'];
85         }
86
87         if (empty($this->funct)) {
88             $this->funct = '';
89         }
90
91         if (empty($this->protocol)) {
92             $this->protocol = 'http-post';
93         }
94     }
95
96     /**
97      * Add RSSCloud-related paths to the router table
98      *
99      * Hook for RouterInitialized event.
100      *
101      * @param Mapper &$m URL parser and mapper
102      *
103      * @return boolean hook return
104      */
105
106     function onRouterInitialized(&$m)
107     {
108         $m->connect('/main/rsscloud/request_notify',
109                     array('action' => 'RSSCloudRequestNotify'));
110
111         // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
112         //      to be a cloud hub for some reason.
113         //$m->connect('/main/rsscloud/notify',
114         //            array('action' => 'LoggingAggregator'));
115
116         return true;
117     }
118
119     /**
120      * Automatically load the actions and libraries used by
121      * the RSSCloud plugin
122      *
123      * @param Class $cls the class
124      *
125      * @return boolean hook return
126      *
127      */
128
129     function onAutoload($cls)
130     {
131         switch ($cls)
132         {
133         case 'RSSCloudSubscription':
134             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php';
135             return false;
136         case 'RSSCloudNotifier':
137             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php';
138             return false;
139         case 'RSSCloudRequestNotifyAction':
140         case 'LoggingAggregatorAction':
141             include_once INSTALLDIR . '/plugins/RSSCloud/' .
142               mb_substr($cls, 0, -6) . '.php';
143             return false;
144         default:
145             return true;
146         }
147     }
148
149     /**
150      * Add a <cloud> element to the RSS feed (after the rss <channel>
151      * element is started).
152      *
153      * @param Action $action the ApiAction
154      *
155      * @return void
156      */
157
158     function onStartApiRss($action)
159     {
160         if (get_class($action) == 'ApiTimelineUserAction') {
161
162             $attrs = array('domain'            => $this->domain,
163                            'port'              => $this->port,
164                            'path'              => $this->path,
165                            'registerProcedure' => $this->funct,
166                            'protocol'          => $this->protocol);
167
168             // Dipping into XMLWriter to avoid a full end element (</cloud>).
169
170             $action->xw->startElement('cloud');
171             foreach ($attrs as $name => $value) {
172                 $action->xw->writeAttribute($name, $value);
173             }
174
175             $action->xw->endElement();
176         }
177     }
178
179     /**
180      * Add an RSSCloud queue item for each notice
181      *
182      * @param Notice $notice      the notice
183      * @param array  &$transports the list of transports (queues)
184      *
185      * @return boolean hook return
186      */
187
188     function onStartEnqueueNotice($notice, &$transports)
189     {
190         array_push($transports, 'rsscloud');
191         return true;
192     }
193
194     /**
195      * broadcast the message when not using queuehandler
196      *
197      * @param Notice &$notice the notice
198      * @param array  $queue   destination queue
199      *
200      * @return boolean hook return
201      */
202
203     function onUnqueueHandleNotice(&$notice, $queue)
204     {
205         if (($queue == 'rsscloud') && ($this->_isLocal($notice))) {
206
207             common_debug('broadcasting rssCloud bound notice ' . $notice->id);
208
209             $profile = $notice->getProfile();
210
211             $notifier = new RSSCloudNotifier();
212             $notifier->notify($profile);
213
214             return false;
215         }
216
217         return true;
218     }
219
220     /**
221      * Determine whether the notice was locally created
222      *
223      * @param Notice $notice the notice in question
224      *
225      * @return boolean locality
226      */
227
228     function _isLocal($notice)
229     {
230         return ($notice->is_local == Notice::LOCAL_PUBLIC ||
231                 $notice->is_local == Notice::LOCAL_NONPUBLIC);
232     }
233
234     /**
235      * Create the rsscloud_subscription table if it's not
236      * already in the DB
237      *
238      * @return boolean hook return
239      */
240
241     function onCheckSchema()
242     {
243         $schema = Schema::get();
244         $schema->ensureTable('rsscloud_subscription',
245                              array(new ColumnDef('subscribed', 'integer',
246                                                  null, false, 'PRI'),
247                                    new ColumnDef('url', 'varchar',
248                                                  '255', false, 'PRI'),
249                                    new ColumnDef('failures', 'integer',
250                                                  null, false, null, 0),
251                                    new ColumnDef('created', 'datetime',
252                                                  null, false),
253                                    new ColumnDef('modified', 'timestamp',
254                                                  null, false, null,
255                                                  'CURRENT_TIMESTAMP',
256                                                  'on update CURRENT_TIMESTAMP')
257                                    ));
258          return true;
259     }
260
261     /**
262      * Add RSSCloudQueueHandler to the list of valid daemons to
263      * start
264      *
265      * @param array $daemons the list of daemons to run
266      *
267      * @return boolean hook return
268      *
269      */
270
271     function onGetValidDaemons($daemons)
272     {
273         array_push($daemons, INSTALLDIR .
274                    '/plugins/RSSCloud/RSSCloudQueueHandler.php');
275         return true;
276     }
277
278 }
279