]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/FeedPoller/lib/feedpoll.php
Merge branch 'xmpp-fix-1' into 'nightly'
[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             $feedsub->last_update = common_sql_now();
44             $feedsub->update($orig);
45         }
46     }
47
48     public function setupFeedSub(FeedSub $feedsub, $interval=300)
49     {
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);
56     }
57
58     public function checkUpdates(FeedSub $feedsub)
59     {
60         $request = new HTTPClient();
61         $feed = $request->get($feedsub->uri);
62         if (!$feed->isOk()) {
63             throw new ServerException('FeedSub could not fetch id='.$feedsub->id.' (Error '.$feed->getStatus().': '.$feed->getBody());
64         }
65         $feedsub->receive($feed->getBody(), null);
66     }
67 }