private function extractFeed(\DOMElement $element) { $feed = new Feed(); foreach (self::$feedPropertiesMapping as $nodeName => $methodName) { $feed->{$methodName}($this->getNodeValueByTagName($element, $nodeName)); } if ($date = $this->getNodeValueByTagName($element, 'updated')) { $feed->setPublishedDate(\DateTime::createFromFormat(\DateTime::ATOM, $date)); } $nodeList = $element->getElementsByTagName('link'); foreach ($nodeList as $nodeResult) { /** @var \DomElement $nodeResult */ if ($nodeResult->getAttribute('rel') === 'alternate') { $feed->setLink($nodeResult->getAttribute('href')); break; } } if (!$feed->getLink()) { $feed->setLink($this->getNodeValueByTagName($element, 'id')); } $feed->setLastFetched(new \DateTime()); return $feed; }
public function save(Feed $feed) { $this->init(); $updateFeed = true; // if there are no such feed yet. if (!$feed->getId()) { if (!($loadedFeed = $this->loadFeedByUrl($feed->getSource()))) { $updateFeed = false; // sure if there are only mysql it's much more efficient to make through sql variables :) $statement = $this->pdo->prepare('insert into Feed (`id`, `source`, `link`, `description`, `title`, ' . '`published_date`,`last_fetched`, `last_modified`) values (null, ?, ?, ?, ?, ?, ?, ?)'); $statement->execute([$feed->getSource(), $feed->getLink(), $feed->getDescription(), $feed->getTitle(), $this->formatDateTime($feed->getPublishedDate()), $this->formatDateTime($feed->getLastFetched()), $this->formatDateTime($feed->getLastModified())]); $feed->setId($this->pdo->lastInsertId()); } else { $feed->setId($loadedFeed->getId()); } } $this->pdo->beginTransaction(); try { if ($updateFeed) { $statement = $this->pdo->prepare('UPDATE Feed SET `link` = ?, `description` = ?, `title` = ?, ' . '`published_date` = ?, `last_fetched` = ?, `last_modified` = ? WHERE id = ?'); $statement->execute([$feed->getLink(), $feed->getDescription(), $feed->getTitle(), $this->formatDateTime($feed->getPublishedDate()), $this->formatDateTime($feed->getLastFetched()), $this->formatDateTime($feed->getLastModified()), $feed->getId()]); } //TODO: collision with same pubdates $lastRecord = $this->loadLastItem($feed->getId()); $statement = $this->pdo->prepare('insert into Record (`id`, `feed_id`, `title`, `content`, `picture`, ' . '`author`, `link`, `guid`, `publication_date`, `tags`) values (null, ?, ?, ?, ?, ?, ?, ?, ?, ?)'); foreach ($feed->getRecords() as $record) { /** @var Record $record */ if (!$lastRecord || $record->getPublicationDate() > $lastRecord->getPublicationDate()) { $statement->execute([$feed->getId(), $record->getTitle(), $record->getContent(), $record->getPicture(), $record->getAuthor(), $record->getLink(), $record->getGuid(), $this->formatDateTime($record->getPublicationDate()), json_encode($record->getTags())]); } } $this->pdo->commit(); } catch (\PDOException $e) { $this->pdo->rollBack(); } }