]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FeedPoller/lib/feedpoll.php
1ac5fad0cfc22243f73ec1e2a287977768c45855
[quix0rs-gnu-social.git] / plugins / FeedPoller / lib / feedpoll.php
1 <?php
2 /**
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.
5  *
6  * Used as internal feed polling mechanism (atom/rss)
7  *
8  * @category OStatus
9  * @package  GNUsocial
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/
13  */
14
15 if (!defined('GNUSOCIAL')) { exit(1); }
16
17 class FeedPoll {
18     const DEFAULT_INTERVAL = 5; // in minutes
19
20     const QUEUE_CHECK = 'feedpoll-check';
21
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;
27         }
28
29         $args['interval'] *= 60;    // minutes to seconds
30
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'])));
36         $feedsub->find();
37
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             common_debug('Enqueueing FeedPoll feeds, currently: '.$feedsub->uri);
44             $feedsub->last_update = common_sql_now();
45             $feedsub->update($orig);
46         }
47     }
48
49     public function setupFeedSub(FeedSub $feedsub, $interval=300)
50     {
51         $orig = clone($feedsub);
52         $feedsub->sub_state = 'nohub';
53         $feedsub->sub_start = common_sql_date(time());
54         $feedsub->sub_end   = '';
55         $feedsub->last_update = common_sql_date(time()-$interval);  // force polling as soon as we can
56         $feedsub->update($orig);
57     }
58
59     public function checkUpdates(FeedSub $feedsub)
60     {
61         $request = new HTTPClient();
62         common_debug('Enqueueing FeedPoll feeds went well, now checking updates for: '.$feedsub->getUri());
63         $feed = $request->get($feedsub->uri);
64         if (!$feed->isOk()) {
65             throw new ServerException('FeedSub could not fetch id='.$feedsub->id.' (Error '.$feed->getStatus().': '.$feed->getBody());
66         }
67         $feedsub->receive($feed->getBody(), null);
68     }
69 }