/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) { // Set time zone to GMT for parsing dates with strtotime(). $tz = date_default_timezone_get(); date_default_timezone_set('GMT'); $raw = trim($fetcher_result->getRaw()); if (!strlen($raw)) { throw new EmptyFeedException(); } // Yes, using a DOM parser is a bit inefficient, but will do for now. // @todo XML error handling. $this->startXmlErrorHandling(); $xml = new \SimpleXMLElement($raw); $this->stopXmlErrorHandling(); $result = new ParserResult(); foreach ($xml->url as $url) { $item = new SitemapItem(); $item->set('url', (string) $url->loc); if ($url->lastmod) { $item->set('lastmod', strtotime($url->lastmod)); } if ($url->changefreq) { $item->set('changefreq', (string) $url->changefreq); } if ($url->priority) { $item->set('priority', (string) $url->priority); } $result->addItem($item); } date_default_timezone_set($tz); return $result; }
/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) { $feed_config = $feed->getConfigurationFor($this); if (!filesize($fetcher_result->getFilePath())) { throw new EmptyFeedException(); } // Load and configure parser. $parser = CsvFileParser::createFromFilePath($fetcher_result->getFilePath())->setDelimiter($feed_config['delimiter'] === 'TAB' ? "\t" : $feed_config['delimiter'])->setHasHeader(!$feed_config['no_headers'])->setStartByte((int) $state->pointer); // Wrap parser in a limit iterator. $parser = new \LimitIterator($parser, 0, $this->configuration['line_limit']); $header = !$feed_config['no_headers'] ? $parser->getHeader() : []; $result = new ParserResult(); foreach ($parser as $row) { $item = new DynamicItem(); foreach ($row as $delta => $cell) { $key = isset($header[$delta]) ? $header[$delta] : $delta; $item->set($key, $cell); } $result->addItem($item); } // Report progress. $state->total = filesize($fetcher_result->getFilePath()); $state->pointer = $parser->lastLinePos(); $state->progress($state->total, $state->pointer); return $result; }
public function testOnInitImport() { $fetcher_result = $this->getMock('Drupal\\feeds\\Result\\FetcherResultInterface'); $parser_result = new ParserResult(); $parser_result->addItem(new DynamicItem()); $this->fetcher->expects($this->once())->method('fetch')->with($this->feed, $this->state)->will($this->returnValue($fetcher_result)); $this->parser->expects($this->once())->method('parse')->with($this->feed, $fetcher_result, $this->state)->will($this->returnValue($parser_result)); $this->processor->expects($this->once())->method('process'); $this->feedType->expects($this->once())->method('getFetcher')->will($this->returnValue($this->fetcher)); $this->feedType->expects($this->once())->method('getParser')->will($this->returnValue($this->parser)); $this->feedType->expects($this->once())->method('getProcessor')->will($this->returnValue($this->processor)); $subscriber = new LazySubscriber(); // Fetch. $subscriber->onInitImport(new InitEvent($this->feed, 'fetch'), FeedsEvents::INIT_IMPORT, $this->dispatcher); $fetch_event = $this->dispatcher->dispatch(FeedsEvents::FETCH, new FetchEvent($this->feed)); $this->assertSame($fetcher_result, $fetch_event->getFetcherResult()); // Parse. $subscriber->onInitImport(new InitEvent($this->feed, 'parse'), FeedsEvents::INIT_IMPORT, $this->dispatcher); $parse_event = $this->dispatcher->dispatch(FeedsEvents::PARSE, new ParseEvent($this->feed, $fetcher_result)); $this->assertSame($parser_result, $parse_event->getParserResult()); // Process. $subscriber->onInitImport(new InitEvent($this->feed, 'process'), FeedsEvents::INIT_IMPORT, $this->dispatcher); foreach ($parse_event->getParserResult() as $item) { $this->dispatcher->dispatch(FeedsEvents::PROCESS, new ProcessEvent($this->feed, $item)); } // Call again. $subscriber->onInitImport(new InitEvent($this->feed, 'fetch'), FeedsEvents::INIT_IMPORT, $this->explodingDispatcher); }
public function test() { $this->dispatcher->addListener(FeedsEvents::PARSE, function ($parse_event) { $parser_result = new ParserResult(); $parser_result->addItem(new DynamicItem()); $parse_event->setParserResult($parser_result); }); $fetcher_result = new FetcherResult(''); $this->plugin->processItem([$this->feed, $fetcher_result]); }
/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) { $raw = $fetcher_result->getRaw(); if (!strlen(trim($raw))) { throw new EmptyFeedException(); } $result = new ParserResult(); $parser = new GenericOpmlParser($fetcher_result->getRaw()); $opml = $parser->parse(TRUE); foreach ($this->getItems($opml['outlines']) as $item) { $item->set('feed_title', $opml['head']['#title']); $result->addItem($item); } return $result; }
/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) { $result = new ParserResult(); Reader::setExtensionManager(\Drupal::service('feed.bridge.reader')); Reader::registerExtension('GeoRSS'); $raw = $fetcher_result->getRaw(); if (!strlen(trim($raw))) { throw new EmptyFeedException(); } try { $channel = Reader::importString($raw); } catch (ExceptionInterface $e) { $args = ['%site' => $feed->label(), '%error' => trim($e->getMessage())]; throw new \RuntimeException($this->t('The feed from %site seems to be broken because of error "%error".', $args)); } foreach ($channel as $delta => $entry) { $item = new SyndicationItem(); // Move the values to an array as expected by processors. $item->set('title', $entry->getTitle())->set('guid', $entry->getId())->set('url', $entry->getLink())->set('guid', $entry->getId())->set('url', $entry->getLink())->set('description', $entry->getDescription())->set('tags', $entry->getCategories()->getValues())->set('feed_title', $channel->getTitle())->set('feed_description', $channel->getDescription())->set('feed_url', $channel->getLink()); if ($image = $channel->getImage()) { $item->set('feed_image_uri', $image['uri']); } if ($enclosure = $entry->getEnclosure()) { $item->set('enclosures', [rawurldecode($enclosure->url)]); } if ($author = $entry->getAuthor()) { $author += ['name' => '', 'email' => '']; $item->set('author_name', $author['name'])->set('author_email', $author['email']); } if ($date = $entry->getDateModified()) { $item->set('timestamp', $date->getTimestamp()); } if ($point = $entry->getGeoPoint()) { $item->set('georss_lat', $point['lat'])->set('georss_lon', $point['lon']); } $result->addItem($item); } return $result; }