]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RSSCloud/RSSCloudPlugin.php
144e0ca57d134d519e1608fba217d099553c3310
[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 class RSSCloudPlugin extends Plugin
46 {
47     /**
48      * Our friend, the constructor
49      *
50      * @return void
51      */
52     function __construct()
53     {
54         parent::__construct();
55     }
56
57     /**
58      * Setup the info for the subscription handler. Allow overriding
59      * to point at another cloud hub (not currently used).
60      *
61      * @return void
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     function onRouterInitialized($m)
106     {
107         $m->connect('/main/rsscloud/request_notify',
108                     array('action' => 'RSSCloudRequestNotify'));
109
110         // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
111         //      to be a cloud hub for some reason.
112         //$m->connect('/main/rsscloud/notify',
113         //            array('action' => 'LoggingAggregator'));
114
115         return true;
116     }
117
118     /**
119      * Automatically load the actions and libraries used by
120      * the RSSCloud plugin
121      *
122      * @param Class $cls the class
123      *
124      * @return boolean hook return
125      *
126      */
127     function onAutoload($cls)
128     {
129         switch ($cls)
130         {
131         case 'RSSCloudSubscription':
132             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudSubscription.php';
133             return false;
134         case 'RSSCloudNotifier':
135             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudNotifier.php';
136             return false;
137         case 'RSSCloudQueueHandler':
138             include_once INSTALLDIR . '/plugins/RSSCloud/RSSCloudQueueHandler.php';
139             return false;
140         case 'RSSCloudRequestNotifyAction':
141         case 'LoggingAggregatorAction':
142             include_once INSTALLDIR . '/plugins/RSSCloud/' .
143               mb_substr($cls, 0, -6) . '.php';
144             return false;
145         default:
146             return true;
147         }
148     }
149
150     /**
151      * Add a <cloud> element to the RSS feed (after the rss <channel>
152      * element is started).
153      *
154      * @param Action $action the ApiAction
155      *
156      * @return void
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         if ($notice->isLocal()) {
191             array_push($transports, 'rsscloud');
192         }
193         return true;
194     }
195
196     /**
197      * Create the rsscloud_subscription table if it's not
198      * already in the DB
199      *
200      * @return boolean hook return
201      */
202
203     function onCheckSchema()
204     {
205         $schema = Schema::get();
206         $schema->ensureTable('rsscloud_subscription',
207             array(
208                 'fields' => array(
209                     'subscribed' => array('type' => 'int', 'not null' => true),
210                     'url' => array('type' => 'varchar', 'length' => '255', 'not null' => true),
211                     'failures' => array('type' => 'int', 'not null' => true, 'default' => 0),
212                     'created' => array('type' => 'datetime', 'not null' => true),
213                     'modified' => array('type' => 'timestamp', 'not null' => true),
214                 ),
215                 'primary key' => array('subscribed', 'url'),
216             ));
217          return true;
218     }
219
220     /**
221      * Register RSSCloud notice queue handler
222      *
223      * @param QueueManager $manager
224      *
225      * @return boolean hook return
226      */
227     function onEndInitializeQueueManager($manager)
228     {
229         $manager->connect('rsscloud', 'RSSCloudQueueHandler');
230         return true;
231     }
232
233     function onPluginVersion(&$versions)
234     {
235         $versions[] = array('name' => 'RSSCloud',
236                             'version' => RSSCLOUDPLUGIN_VERSION,
237                             'author' => 'Zach Copley',
238                             'homepage' => 'http://status.net/wiki/Plugin:RSSCloud',
239                             'rawdescription' =>
240                             // TRANS: Plugin description.
241                             _m('The RSSCloud plugin enables your StatusNet instance to publish ' .
242                                'real-time updates for profile RSS feeds using the ' .
243                                '<a href="http://rsscloud.org/">RSSCloud protocol</a>.'));
244
245         return true;
246     }
247 }