5 * Fetches feeds that need an update and updates their feedentries.
12 public function updateAll()
14 $res = $this->db->query(
16 . ' WHERE f_needs_update = 1 OR f_updated = "0000-00-00"'
18 while ($feedRow = $res->fetch(\PDO::FETCH_OBJ)) {
20 sprintf('Updating feed #%d: %s', $feedRow->f_id, $feedRow->f_url)
22 $this->updateFeed($feedRow);
26 protected function updateFeed($feedRow)
28 $req = new \HTTP_Request2($feedRow->f_url);
29 $req->setHeader('User-Agent', 'stapibas');
31 if ($feedRow->f_updated != '0000-00-00 00:00:00') {
34 gmdate('r', strtotime($feedRow->f_updated))
39 if ($res->getStatus() == 304) {
41 $this->setNoUpdate($feedRow);
42 $this->log->info('Not modified');
46 if (intval($res->getStatus() / 100) != 2) {
47 //no 2xx is an error for us
48 $this->log->info('Error fetching feed');
52 $this->updateEntries($feedRow, $res);
55 protected function updateEntries($feedRow, \HTTP_Request2_Response $res)
57 require_once $GLOBALS['stapibas_libdir'] . '/simplepie/autoloader.php';
58 $sp = new \SimplePie();
59 $sp->set_raw_data($res->getBody());
62 $new = $updated = $items = 0;
63 foreach ($sp->get_items() as $item) {
65 $url = $item->get_permalink();
66 $entryRow = $this->db->query(
67 'SELECT fe_id, fe_updated, fe_needs_update FROM feedentries'
68 . ' WHERE fe_url = ' . $this->db->quote($url)
69 . ' AND fe_f_id = ' . $this->db->quote($feedRow->f_id)
70 )->fetch(\PDO::FETCH_OBJ);
72 if ($entryRow === false) {
75 'INSERT INTO feedentries SET'
76 . ' fe_f_id = ' . $this->db->quote($feedRow->f_id)
77 . ', fe_url = ' . $this->db->quote($url)
78 . ', fe_needs_update = 1'
83 if ($entryRow->fe_needs_update == 0
84 && $item->get_updated_gmdate('U') > strtotime($entryRow->fe_updated)
88 'UPDATE feedentries SET'
89 . ' fe_url = ' . $this->db->quote($url)
90 . ', fe_needs_update = 1'
91 . ' WHERE fe_id = ' . $this->db->quote($entryRow->fe_id)
99 'Feed #%d: %d new, %d updated of %d entries',
100 $feedRow->f_id, $new, $updated, $items
103 $this->setUpdated($feedRow, $res);
106 protected function setNoUpdate($feedRow)
109 'UPDATE feeds SET f_needs_update = 0'
110 . ' WHERE f_id = ' . $this->db->quote($feedRow->f_id)
114 protected function setUpdated($feedRow, \HTTP_Request2_Response $res)
118 . ' SET f_needs_update = 0'
119 . ', f_updated = ' . $this->db->quote(
120 gmdate('Y-m-d H:i:s', strtotime($res->getHeader('last-modified')))
122 . ' WHERE f_id = ' . $this->db->quote($feedRow->f_id)