/** * 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; }
static function getItems(Feed &$feed) { $fid = $feed->getId(); $text = $feed->getMessage(); static::doParse($text); $assocs = static::$array; $result = array(); foreach ($assocs as $index => $assoc) { $item = new Item(); $item->setId($fid . '_' . $index); $item->setFeed($feed); if (isset($assoc['type'])) { $item->setType($assoc['type']); } else { $item->setType('GLOBAL'); } if (isset($assoc['description'])) { $item->setDescription($assoc['description']); } if (isset($assoc['global'])) { $item->setGlobal($assoc['global']); } if (isset($assoc['name'])) { $item->setName($assoc['name']); } if (isset($assoc['note'])) { $item->setNote($assoc['note']); } if (isset($assoc['price_digit'])) { $item->setPrice($assoc['price_digit']); } if (isset($assoc['price'])) { $item->setPriceStr($assoc['price']); } if (isset($assoc['status'])) { $item->setStatus($assoc['status']); } $result[] = $item; } if (!$result) { $item = new Item(); $item->setId($fid . '_0'); $item->setFeed($feed); $item->setType('GLOBAL'); $item->setGlobal($text); $result[] = $item; } return $result; }