]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudPlugin.php
2de162628ffbbb05fe7fc0be56f4a76f64190c45
[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 'RSSCloudRequestNotifyAction':
142         case 'LoggingAggregatorAction':
143             include_once INSTALLDIR . '/plugins/RSSCloud/' .
144               mb_substr($cls, 0, -6) . '.php';
145             return false;
146         default:
147             return true;
148         }
149     }
150
151     /**
152      * Add a <cloud> element to the RSS feed (after the rss <channel>
153      * element is started).
154      *
155      * @param Action $action the ApiAction
156      *
157      * @return void
158      */
159
160     function onStartApiRss($action)
161     {
162         if (get_class($action) == 'ApiTimelineUserAction') {
163
164             $attrs = array('domain'            => $this->domain,
165                            'port'              => $this->port,
166                            'path'              => $this->path,
167                            'registerProcedure' => $this->funct,
168                            'protocol'          => $this->protocol);
169
170             // Dipping into XMLWriter to avoid a full end element (</cloud>).
171
172             $action->xw->startElement('cloud');
173             foreach ($attrs as $name => $value) {
174                 $action->xw->writeAttribute($name, $value);
175             }
176
177             $action->xw->endElement();
178         }
179     }
180
181     /**
182      * Add an RSSCloud queue item for each notice
183      *
184      * @param Notice $notice      the notice
185      * @param array  &$transports the list of transports (queues)
186      *
187      * @return boolean hook return
188      */
189
190     function onStartEnqueueNotice($notice, &$transports)
191     {
192         array_push($transports, 'rsscloud');
193         return true;
194     }
195
196     /**
197      * broadcast the message when not using queuehandler
198      *
199      * @param Notice &$notice the notice
200      * @param array  $queue   destination queue
201      *
202      * @return boolean hook return
203      */
204
205     function onUnqueueHandleNotice(&$notice, $queue)
206     {
207         if (($queue == 'rsscloud') && ($this->_isLocal($notice))) {
208
209             common_debug('broadcasting rssCloud bound notice ' . $notice->id);
210
211             $profile = $notice->getProfile();
212
213             $notifier = new RSSCloudNotifier();
214             $notifier->notify($profile);
215
216             return false;
217         }
218
219         return true;
220     }
221
222     /**
223      * Determine whether the notice was locally created
224      *
225      * @param Notice $notice the notice in question
226      *
227      * @return boolean locality
228      */
229
230     function _isLocal($notice)
231     {
232         return ($notice->is_local == Notice::LOCAL_PUBLIC ||
233                 $notice->is_local == Notice::LOCAL_NONPUBLIC);
234     }
235
236     /**
237      * Create the rsscloud_subscription table if it's not
238      * already in the DB
239      *
240      * @return boolean hook return
241      */
242
243     function onCheckSchema()
244     {
245         $schema = Schema::get();
246         $schema->ensureTable('rsscloud_subscription',
247                              array(new ColumnDef('subscribed', 'integer',
248                                                  null, false, 'PRI'),
249                                    new ColumnDef('url', 'varchar',
250                                                  '255', false, 'PRI'),
251                                    new ColumnDef('failures', 'integer',
252                                                  null, false, null, 0),
253                                    new ColumnDef('created', 'datetime',
254                                                  null, false),
255                                    new ColumnDef('modified', 'timestamp',
256                                                  null, false, null,
257                                                  'CURRENT_TIMESTAMP',
258                                                  'on update CURRENT_TIMESTAMP')
259                                    ));
260          return true;
261     }
262
263     /**
264      * Add RSSCloudQueueHandler to the list of valid daemons to
265      * start
266      *
267      * @param array $daemons the list of daemons to run
268      *
269      * @return boolean hook return
270      *
271      */
272
273     function onGetValidDaemons($daemons)
274     {
275         array_push($daemons, INSTALLDIR .
276                    '/plugins/RSSCloud/RSSCloudQueueHandler.php');
277         return true;
278     }
279
280     function onPluginVersion(&$versions)
281     {
282         $versions[] = array('name' => 'RSSCloud',
283                             'version' => RSSCLOUDPLUGIN_VERSION,
284                             'author' => 'Zach Copley',
285                             'homepage' => 'http://status.net/wiki/Plugin:RSSCloud',
286                             'rawdescription' =>
287                             _m('The RSSCloud plugin enables your StatusNet instance to publish ' .
288                                'real-time updates for profile RSS feeds using the ' .
289                                '<a href="http://rsscloud.org/">RSSCloud protocol</a>".'));
290
291         return true;
292     }
293
294 }
295