5 * Fetches feeds that need an update and updates their feedentries.
12 public function __construct(Dependencies $deps)
15 $this->db = $deps->db;
16 $this->log = $deps->log;
19 public function updateAll()
21 $this->log->info('Updating feeds..');
22 $res = $this->db->query(
24 . ' WHERE ' . $this->sqlNeedsUpdate()
26 while ($feedRow = $res->fetch(\PDO::FETCH_OBJ)) {
27 $this->updateFeed($feedRow);
29 $this->log->info('Finished updating feeds.');
32 public function updateSome($urlOrIds)
35 foreach ($urlOrIds as $urlOrId) {
36 if (is_numeric($urlOrId)) {
37 $options[] = 'f_id = ' . intval($urlOrId);
39 $options[] = 'f_url = ' . $this->db->quote($urlOrId);
43 $this->log->info('Updating %d feeds..', $options);
44 $res = $this->db->query(
47 . $this->sqlNeedsUpdate()
48 . ' AND (' . implode(' OR ', $options) . ')'
52 while ($feedRow = $res->fetch(\PDO::FETCH_OBJ)) {
54 $this->updateFeed($feedRow);
56 $this->log->info('Finished updating %d feeds.', $items);
59 protected function updateFeed($feedRow)
62 'Updating feed #%d: %s', $feedRow->f_id, $feedRow->f_url
65 $req = new \HTTP_Request2($feedRow->f_url);
66 $req->setHeader('User-Agent', 'stapibas');
68 if ($feedRow->f_updated != '0000-00-00 00:00:00') {
71 gmdate('r', strtotime($feedRow->f_updated))
76 if ($res->getStatus() == 304) {
78 $this->setNoUpdate($feedRow);
79 $this->log->info('Not modified');
83 if (intval($res->getStatus() / 100) != 2) {
84 //no 2xx is an error for us
85 $this->log->err('Error fetching feed');
89 $this->updateEntries($feedRow, $res);
92 protected function updateEntries($feedRow, \HTTP_Request2_Response $res)
94 require_once $GLOBALS['stapibas_libdir'] . '/simplepie/autoloader.php';
95 $sp = new \SimplePie();
96 $sp->set_raw_data($res->getBody());
99 $new = $updated = $items = 0;
100 foreach ($sp->get_items() as $item) {
102 $url = $item->get_permalink();
103 $entryRow = $this->db->query(
104 'SELECT fe_id, fe_updated, fe_needs_update FROM feedentries'
105 . ' WHERE fe_url = ' . $this->db->quote($url)
106 . ' AND fe_f_id = ' . $this->db->quote($feedRow->f_id)
107 )->fetch(\PDO::FETCH_OBJ);
109 if ($entryRow === false) {
112 'INSERT INTO feedentries SET'
113 . ' fe_f_id = ' . $this->db->quote($feedRow->f_id)
114 . ', fe_url = ' . $this->db->quote($url)
115 . ', fe_needs_update = 1'
120 if ($entryRow->fe_needs_update == 0
121 && $item->get_updated_gmdate('U') > strtotime($entryRow->fe_updated)
125 'UPDATE feedentries SET'
126 . ' fe_url = ' . $this->db->quote($url)
127 . ', fe_needs_update = 1'
128 . ' WHERE fe_id = ' . $this->db->quote($entryRow->fe_id)
135 'Feed #%d: %d new, %d updated of %d entries',
136 $feedRow->f_id, $new, $updated, $items
138 $this->setUpdated($feedRow, $res);
141 protected function setNoUpdate($feedRow)
144 'UPDATE feeds SET f_needs_update = 0'
145 . ' WHERE f_id = ' . $this->db->quote($feedRow->f_id)
149 protected function setUpdated($feedRow, \HTTP_Request2_Response $res)
153 . ' SET f_needs_update = 0'
154 . ', f_updated = ' . $this->db->quote(
155 gmdate('Y-m-d H:i:s', strtotime($res->getHeader('last-modified')))
157 . ' WHERE f_id = ' . $this->db->quote($feedRow->f_id)
161 protected function sqlNeedsUpdate()
163 if ($this->deps->options['force']) {
166 return ' (f_needs_update = 1 OR f_updated = "0000-00-00 00:00:00")';