RSSCloudPlugin.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. if (!defined('STATUSNET')) {
  30. exit(1);
  31. }
  32. define('RSSCLOUDPLUGIN_VERSION', '0.1.0');
  33. /**
  34. * Plugin class for adding RSSCloud capabilities to StatusNet
  35. *
  36. * @category Plugin
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class RSSCloudPlugin extends Plugin
  43. {
  44. /**
  45. * Our friend, the constructor
  46. *
  47. * @return void
  48. */
  49. function __construct()
  50. {
  51. parent::__construct();
  52. }
  53. /**
  54. * Setup the info for the subscription handler. Allow overriding
  55. * to point at another cloud hub (not currently used).
  56. *
  57. * @return void
  58. */
  59. function onInitializePlugin()
  60. {
  61. $this->domain = common_config('rsscloud', 'domain');
  62. $this->port = common_config('rsscloud', 'port');
  63. $this->path = common_config('rsscloud', 'path');
  64. $this->funct = common_config('rsscloud', 'function');
  65. $this->protocol = common_config('rsscloud', 'protocol');
  66. // set defaults
  67. $local_server = parse_url(common_path('main/rsscloud/request_notify'));
  68. if (empty($this->domain)) {
  69. $this->domain = $local_server['host'];
  70. }
  71. if (empty($this->port)) {
  72. $this->port = '80';
  73. }
  74. if (empty($this->path)) {
  75. $this->path = $local_server['path'];
  76. }
  77. if (empty($this->funct)) {
  78. $this->funct = '';
  79. }
  80. if (empty($this->protocol)) {
  81. $this->protocol = 'http-post';
  82. }
  83. }
  84. /**
  85. * Add RSSCloud-related paths to the router table
  86. *
  87. * Hook for RouterInitialized event.
  88. *
  89. * @param Mapper $m URL parser and mapper
  90. *
  91. * @return boolean hook return
  92. */
  93. function onRouterInitialized($m)
  94. {
  95. $m->connect('/main/rsscloud/request_notify',
  96. ['action' => 'RSSCloudRequestNotify']);
  97. // XXX: This is just for end-to-end testing. Uncomment if you need to pretend
  98. // to be a cloud hub for some reason.
  99. //$m->connect('/main/rsscloud/notify',
  100. // array('action' => 'LoggingAggregator'));
  101. return true;
  102. }
  103. /**
  104. * Add a <cloud> element to the RSS feed (after the rss <channel>
  105. * element is started).
  106. *
  107. * @param Action $action the ApiAction
  108. *
  109. * @return void
  110. */
  111. function onStartApiRss($action)
  112. {
  113. if (get_class($action) == 'ApiTimelineUserAction') {
  114. $attrs = array('domain' => $this->domain,
  115. 'port' => $this->port,
  116. 'path' => $this->path,
  117. 'registerProcedure' => $this->funct,
  118. 'protocol' => $this->protocol);
  119. // Dipping into XMLWriter to avoid a full end element (</cloud>).
  120. $action->xw->startElement('cloud');
  121. foreach ($attrs as $name => $value) {
  122. $action->xw->writeAttribute($name, $value);
  123. }
  124. $action->xw->endElement();
  125. }
  126. }
  127. /**
  128. * Add an RSSCloud queue item for each notice
  129. *
  130. * @param Notice $notice the notice
  131. * @param array &$transports the list of transports (queues)
  132. *
  133. * @return boolean hook return
  134. */
  135. function onStartEnqueueNotice($notice, &$transports)
  136. {
  137. if ($notice->isLocal()) {
  138. array_push($transports, 'rsscloud');
  139. }
  140. return true;
  141. }
  142. /**
  143. * Create the rsscloud_subscription table if it's not
  144. * already in the DB
  145. *
  146. * @return boolean hook return
  147. */
  148. function onCheckSchema()
  149. {
  150. $schema = Schema::get();
  151. $schema->ensureTable('rsscloud_subscription',
  152. array(
  153. 'fields' => array(
  154. 'subscribed' => array('type' => 'int', 'not null' => true),
  155. 'url' => array('type' => 'varchar', 'length' => '191', 'not null' => true),
  156. 'failures' => array('type' => 'int', 'not null' => true, 'default' => 0),
  157. 'created' => array('type' => 'datetime', 'not null' => true),
  158. 'modified' => array('type' => 'timestamp', 'not null' => true),
  159. ),
  160. 'primary key' => array('subscribed', 'url'),
  161. ));
  162. return true;
  163. }
  164. /**
  165. * Register RSSCloud notice queue handler
  166. *
  167. * @param QueueManager $manager
  168. *
  169. * @return boolean hook return
  170. */
  171. function onEndInitializeQueueManager($manager)
  172. {
  173. $manager->connect('rsscloud', 'RSSCloudQueueHandler');
  174. return true;
  175. }
  176. function onPluginVersion(array &$versions)
  177. {
  178. $versions[] = array('name' => 'RSSCloud',
  179. 'version' => RSSCLOUDPLUGIN_VERSION,
  180. 'author' => 'Zach Copley',
  181. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/RSSCloud',
  182. 'rawdescription' =>
  183. // TRANS: Plugin description.
  184. _m('The RSSCloud plugin enables your StatusNet instance to publish ' .
  185. 'real-time updates for profile RSS feeds using the ' .
  186. '<a href="http://rsscloud.org/">RSSCloud protocol</a>.'));
  187. return true;
  188. }
  189. }