/** * Takes a Feed object and updates the Item table with new entries on that feed * Returns number of new records added **/ protected function updateFeed($feedRecord) { $feed = $this->getFeed($feedRecord->getLink()); echo "Feed: ", $feedRecord->getLink(), " (", count($feed->entries), " items)\n"; $itemCriteria = new Criteria(); $newItems = 0; // Short circuit if the feed parsing failed if (empty($feed) || empty($feed->entries)) { echo "ERROR: Returned feed not valid, or empty\n"; return 0; } foreach ($feed->entries as $entry) { //echo $entry->title, "\n"; // Check whether this entry already exists $itemCriteria->add(ItemPeer::ATOMID, $entry->id, Criteria::EQUAL); $num = ItemPeer::doCount($itemCriteria); // Skip the current entry if we already have it if ($num > 0) { //echo "INFO: Duplicate atom id: {$entry->id}. Skipping\n"; continue; } // Create a new Item record, and save $item = new Item(); $item->fromArray(array('Atomid' => $entry->id, 'Title' => $entry->title, 'Link' => $this->getLinkHref($entry->links, 'alternate'), 'Description' => '', 'Published' => $entry->published)); if (!empty($entry->content->text)) { $item->setDescription($entry->content->text); } elseif (!empty($entry->summary)) { $item->setDescription($entry->summary); } $item->setFeed($feedRecord); //print_r($item); $item->save(); $newItems++; } return $newItems; }
/** * Returns the number of related Item objects. * * @param Criteria $criteria * @param boolean $distinct * @param PropelPDO $con * @return int Count of related Item objects. * @throws PropelException */ public function countItems(Criteria $criteria = null, $distinct = false, PropelPDO $con = null) { if ($criteria === null) { $criteria = new Criteria(FeedPeer::DATABASE_NAME); } else { $criteria = clone $criteria; } if ($distinct) { $criteria->setDistinct(); } $count = null; if ($this->collItems === null) { if ($this->isNew()) { $count = 0; } else { $criteria->add(ItemPeer::FEED_ID, $this->id); $count = ItemPeer::doCount($criteria, $con); } } else { // criteria has no effect for a new object if (!$this->isNew()) { // the following code is to determine if a new query is // called for. If the criteria is the same as the last // one, just return count of the collection. $criteria->add(ItemPeer::FEED_ID, $this->id); if (!isset($this->lastItemCriteria) || !$this->lastItemCriteria->equals($criteria)) { $count = ItemPeer::doCount($criteria, $con); } else { $count = count($this->collItems); } } else { $count = count($this->collItems); } } return $count; }