feedpoll.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Store last poll time in db, then check if they should be renewed (if so, enqueue).
  18. * Can be called from a queue handler on a per-feed status to poll stuff.
  19. *
  20. * Used as internal feed polling mechanism (atom/rss)
  21. *
  22. * @category OStatus
  23. * @package GNUsocial
  24. * @author Mikael Nordfeldth <mmn@hethane.se>
  25. * @copyright 2015 Free Software Foundation http://fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. defined('GNUSOCIAL') || die();
  29. class FeedPoll
  30. {
  31. const DEFAULT_INTERVAL = 5; // in minutes
  32. const QUEUE_CHECK = 'feedpoll-check';
  33. // TODO: Find some smart way to add feeds only once, so they don't get more than 1 feedpoll in the queue each
  34. // probably through sub_start sub_end trickery.
  35. public static function enqueueNewFeeds(array $args = [])
  36. {
  37. if (!isset($args['interval']) || !is_int($args['interval']) || $args['interval']<=0) {
  38. $args['interval'] = self::DEFAULT_INTERVAL;
  39. }
  40. $feedsub = new FeedSub();
  41. $feedsub->sub_state = 'nohub';
  42. // Find feeds that haven't been polled within the desired interval,
  43. // though perhaps we're abusing the "last_update" field here?
  44. $feedsub->whereAdd(sprintf(
  45. "last_update < CURRENT_TIMESTAMP - INTERVAL '%d' MINUTE",
  46. $args['interval']
  47. ));
  48. $feedsub->find();
  49. $qm = QueueManager::get();
  50. while ($feedsub->fetch()) {
  51. $orig = clone($feedsub);
  52. $item = array('id' => $feedsub->id);
  53. $qm->enqueue($item, self::QUEUE_CHECK);
  54. $feedsub->last_update = common_sql_now();
  55. $feedsub->update($orig);
  56. }
  57. }
  58. public function setupFeedSub(FeedSub $feedsub, $interval=300)
  59. {
  60. $orig = clone($feedsub);
  61. $feedsub->sub_state = 'nohub';
  62. $feedsub->sub_start = common_sql_date(time());
  63. $feedsub->sub_end = '';
  64. $feedsub->last_update = common_sql_date(time()-$interval); // force polling as soon as we can
  65. $feedsub->update($orig);
  66. }
  67. public function checkUpdates(FeedSub $feedsub)
  68. {
  69. $request = new HTTPClient();
  70. $feed = $request->get($feedsub->uri);
  71. if (!$feed->isOk()) {
  72. throw new ServerException('FeedSub could not fetch id='.$feedsub->id.' (Error '.$feed->getStatus().': '.$feed->getBody());
  73. }
  74. $feedsub->receive($feed->getBody(), null);
  75. }
  76. }