/** * {@inheritdoc} */ public function fetch(FeedInterface $feed, StateInterface $state) { $path = $feed->getSource(); // Just return a file fetcher result if this is a file. Make sure to // re-validate the file extension in case the feed type settings have // changed. if (is_file($path)) { if ($this->validateFilePath($path)) { return new FetcherResult($path); } else { throw new \RuntimeException($this->t('%source has an invalid file extension.', ['%source' => $path])); } } if (!is_dir($path) || !is_readable($path)) { throw new \RuntimeException($this->t('%source is not a readable directory or file.', ['%source' => $path])); } // Batch if this is a directory. if (!isset($state->files)) { $state->files = $this->listFiles($path); $state->total = count($state->files); } if ($state->files) { $file = array_shift($state->files); $state->progress($state->total, $state->total - count($state->files)); return new FetcherResult($file); } throw new EmptyFeedException(); }
/** * {@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; }
/** * {@inheritdoc} */ public function clear(FeedInterface $feed, StateInterface $state) { // Build base select statement. $query = $this->queryFactory->get($this->entityType())->condition('feeds_item.target_id', $feed->id()); // If there is no total, query it. if (!$state->total) { $count_query = clone $query; $state->total = (int) $count_query->count()->execute(); } // Delete a batch of entities. $entity_ids = $query->range(0, 10)->execute(); if ($entity_ids) { $this->entityDeleteMultiple($entity_ids); $state->deleted += count($entity_ids); $state->progress($state->total, $state->deleted); } }