]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/PubSubHubBub/PubSubHubBubPlugin.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / PubSubHubBub / PubSubHubBubPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Plugin to push RSS/Atom updates to a PubSubHubBub hub
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    Craig Andrews <candrews@integralblue.com>
25  * @copyright 2009 Craig Andrews http://candrews.integralblue.com
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('DEFAULT_HUB', 'http://pubsubhubbub.appspot.com');
35
36 require_once INSTALLDIR.'/plugins/PubSubHubBub/publisher.php';
37
38 /**
39  * Plugin to provide publisher side of PubSubHubBub (PuSH)
40  * relationship.
41  *
42  * PuSH is a real-time or near-real-time protocol for Atom
43  * and RSS feeds. More information here:
44  *
45  * http://code.google.com/p/pubsubhubbub/
46  *
47  * To enable, add the following line to your config.php:
48  *
49  * addPlugin('PubSubHubBub');
50  *
51  * This will use the Google default hub. If you'd like to use
52  * another, try:
53  *
54  * addPlugin('PubSubHubBub',
55  *           array('hub' => 'http://yourhub.example.net/'));
56  *
57  * @category  Plugin
58  * @package   StatusNet
59  * @author    Craig Andrews <candrews@integralblue.com>
60  * @copyright 2009 Craig Andrews http://candrews.integralblue.com
61  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
62  * @link      http://status.net/
63  */
64
65 class PubSubHubBubPlugin extends Plugin
66 {
67     /**
68      * URL of the hub to advertise and publish to.
69      */
70
71     public $hub = DEFAULT_HUB;
72
73     /**
74      * Default constructor.
75      */
76
77     function __construct()
78     {
79         parent::__construct();
80     }
81
82     /**
83      * Hooks the StartApiAtom event
84      *
85      * Adds the necessary bits to advertise PubSubHubBub
86      * for the Atom feed.
87      *
88      * @param Action $action The API action being shown.
89      *
90      * @return boolean hook value
91      */
92
93     function onStartApiAtom($action)
94     {
95         $action->element('link', array('rel' => 'hub', 'href' => $this->hub), null);
96
97         return true;
98     }
99
100     /**
101      * Hooks the StartApiRss event
102      *
103      * Adds the necessary bits to advertise PubSubHubBub
104      * for the RSS 2.0 feeds.
105      *
106      * @param Action $action The API action being shown.
107      *
108      * @return boolean hook value
109      */
110
111     function onStartApiRss($action)
112     {
113         $action->element('atom:link', array('rel' => 'hub',
114                                             'href' => $this->hub),
115                          null);
116         return true;
117     }
118
119     /**
120      * Hook for a queued notice.
121      *
122      * When a notice has been queued, will ping the
123      * PuSH hub for each Atom and RSS feed in which
124      * the notice appears.
125      *
126      * @param Notice $notice The notice that's been queued
127      *
128      * @return boolean hook value
129      */
130
131     function onHandleQueuedNotice($notice)
132     {
133         $publisher = new Publisher($this->hub);
134
135         $feeds = array();
136
137         //public timeline feeds
138         $feeds[] = common_local_url('ApiTimelinePublic', array('format' => 'rss'));
139         $feeds[] = common_local_url('ApiTimelinePublic', array('format' => 'atom'));
140
141         //author's own feeds
142         $user = User::staticGet('id', $notice->profile_id);
143
144         $feeds[] = common_local_url('ApiTimelineUser',
145                                     array('id' => $user->nickname,
146                                           'format' => 'rss'));
147         $feeds[] = common_local_url('ApiTimelineUser',
148                                     array('id' => $user->nickname,
149                                           'format' => 'atom'));
150
151         //tag feeds
152         $tag = new Notice_tag();
153
154         $tag->notice_id = $notice->id;
155         if ($tag->find()) {
156             while ($tag->fetch()) {
157                 $feeds[] = common_local_url('ApiTimelineTag',
158                                             array('tag' => $tag->tag,
159                                                   'format' => 'rss'));
160                 $feeds[] = common_local_url('ApiTimelineTag',
161                                             array('tag' => $tag->tag,
162                                                   'format' => 'atom'));
163             }
164         }
165
166         //group feeds
167         $group_inbox = new Group_inbox();
168
169         $group_inbox->notice_id = $notice->id;
170         if ($group_inbox->find()) {
171             while ($group_inbox->fetch()) {
172                 $group = User_group::staticGet('id', $group_inbox->group_id);
173
174                 $feeds[] = common_local_url('ApiTimelineGroup',
175                                             array('id' => $group->nickname,
176                                                   'format' => 'rss'));
177                 $feeds[] = common_local_url('ApiTimelineGroup',
178                                             array('id' => $group->nickname,
179                                                   'format' => 'atom'));
180             }
181         }
182
183         //feed of each user that subscribes to the notice's author
184
185         $ni = $notice->whoGets();
186
187         foreach (array_keys($ni) as $user_id) {
188             $user = User::staticGet('id', $user_id);
189             if (empty($user)) {
190                 continue;
191             }
192             $feeds[] = common_local_url('ApiTimelineFriends',
193                                         array('id' => $user->nickname,
194                                               'format' => 'rss'));
195             $feeds[] = common_local_url('ApiTimelineFriends',
196                                         array('id' => $user->nickname,
197                                               'format' => 'atom'));
198         }
199
200         $replies = $notice->getReplies();
201
202         //feed of user replied to
203         foreach ($replies as $recipient) {
204                 $user = User::staticGet('id', $recipient);
205             if (!empty($user)) {
206                 $feeds[] = common_local_url('ApiTimelineMentions',
207                                             array('id' => $user->nickname,
208                                                   'format' => 'rss'));
209                 $feeds[] = common_local_url('ApiTimelineMentions',
210                                             array('id' => $user->nickname,
211                                                   'format' => 'atom'));
212             }
213         }
214
215         foreach (array_unique($feeds) as $feed) {
216             if (!$publisher->publish_update($feed)) {
217                 common_log_line(LOG_WARNING,
218                                 $feed.' was not published to hub at '.
219                                 $this->hub.':'.$publisher->last_response());
220             }
221         }
222
223         return true;
224     }
225
226     /**
227      * Provide version information
228      *
229      * Adds this plugin's version data to the global
230      * version array, for e.g. displaying on the version page.
231      *
232      * @param array &$versions array of array of versions
233      *
234      * @return boolean hook value
235      */
236
237     function onPluginVersion(&$versions)
238     {
239         $versions[] = array('name' => 'PubSubHubBub',
240                             'version' => STATUSNET_VERSION,
241                             'author' => 'Craig Andrews',
242                             'homepage' =>
243                             'http://status.net/wiki/Plugin:PubSubHubBub',
244                             'rawdescription' =>
245                             _m('The PubSubHubBub plugin pushes RSS/Atom updates '.
246                                'to a <a href = "'.
247                                'http://pubsubhubbub.googlecode.com/'.
248                                '">PubSubHubBub</a> hub.'));
249
250         return true;
251     }
252 }