3 * Store last poll time in db, then check if they should be renewed (if so, enqueue).
4 * Can be called from a queue handler on a per-feed status to poll stuff.
6 * Used as internal feed polling mechanism (atom/rss)
10 * @author Mikael Nordfeldth <mmn@hethane.se>
11 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
12 * @link http://www.gnu.org/software/social/
15 if (!defined('GNUSOCIAL')) { exit(1); }
18 const DEFAULT_INTERVAL = 5; // in minutes
20 const QUEUE_CHECK = 'feedpoll-check';
22 // TODO: Find some smart way to add feeds only once, so they don't get more than 1 feedpoll in the queue each
23 // probably through sub_start sub_end trickery.
24 public static function enqueueNewFeeds(array $args=array()) {
25 if (!isset($args['interval']) || !is_int($args['interval']) || $args['interval']<=0) {
26 $args['interval'] = self::DEFAULT_INTERVAL;
29 $args['interval'] *= 60; // minutes to seconds
31 $feedsub = new FeedSub();
32 $feedsub->sub_state = 'nohub';
33 // Find feeds that haven't been polled within the desired interval,
34 // though perhaps we're abusing the "last_update" field here?
35 $feedsub->whereAdd(sprintf('last_update < "%s"', common_sql_date(time()-$args['interval'])));
38 $qm = QueueManager::get();
39 while ($feedsub->fetch()) {
40 $orig = clone($feedsub);
41 $item = array('id' => $feedsub->id);
42 $qm->enqueue($item, self::QUEUE_CHECK);
43 $feedsub->last_update = common_sql_now();
44 $feedsub->update($orig);
48 public function setupFeedSub(FeedSub $feedsub, $interval=300)
50 $orig = clone($feedsub);
51 $feedsub->sub_state = 'nohub';
52 $feedsub->sub_start = common_sql_date(time());
53 $feedsub->sub_end = '';
54 $feedsub->last_update = common_sql_date(time()-$interval); // force polling as soon as we can
55 $feedsub->update($orig);
58 public function checkUpdates(FeedSub $feedsub)
60 $request = new HTTPClient();
61 $feed = $request->get($feedsub->uri);
63 throw new ServerException('FeedSub could not fetch id='.$feedsub->id.' (Error '.$feed->getStatus().': '.$feed->getBody());
65 $feedsub->receive($feed->getBody(), null);