/** * {@inheritodc} */ public function startBatchClear(FeedInterface $feed) { $feed->lock(); $feed->clearStates(); $batch = ['title' => $this->t('Deleting items from: %title', ['%title' => $feed->label()]), 'init_message' => $this->t('Deleting items from: %title', ['%title' => $feed->label()]), 'operations' => [[[$this, 'clear'], [$feed]]], 'progress_message' => $this->t('Deleting items from: %title', ['%title' => $feed->label()]), 'error_message' => $this->t('An error occored while clearing %title.', ['%title' => $feed->label()])]; batch_set($batch); }
/** * Lists the feed items belonging to a feed. */ public function listItems(FeedInterface $feeds_feed, Request $request) { $processor = $feeds_feed->getType()->getProcessor(); $header = ['title' => $this->t('Label'), 'imported' => $this->t('Imported'), 'guid' => ['data' => $this->t('GUID'), 'class' => [RESPONSIVE_PRIORITY_LOW]], 'url' => ['data' => $this->t('URL'), 'class' => [RESPONSIVE_PRIORITY_LOW]]]; $build = []; $build['table'] = ['#type' => 'table', '#header' => $header, '#rows' => [], '#empty' => $this->t('There are no items yet.')]; // @todo Allow processors to create their own entity listings. if (!$processor instanceof EntityProcessorInterface) { return $build; } $entity_ids = \Drupal::entityQuery($processor->entityType())->condition('feeds_item.target_id', $feeds_feed->id())->pager(50)->sort('feeds_item.imported', 'DESC')->execute(); $storage = $this->entityManager()->getStorage($processor->entityType()); foreach ($storage->loadMultiple($entity_ids) as $entity) { $ago = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $entity->get('feeds_item')->imported); $row = []; // Entity link. $row[] = ['data' => $entity->link(Unicode::truncate($entity->label(), 75, TRUE, TRUE)), 'title' => $entity->label()]; // Imported ago. $row[] = $this->t('@time ago', ['@time' => $ago]); // Item GUID. $row[] = ['data' => SafeMarkup::checkPlain(Unicode::truncate($entity->get('feeds_item')->guid, 30, FALSE, TRUE)), 'title' => $entity->get('feeds_item')->guid]; // Item URL. $row[] = ['data' => SafeMarkup::checkPlain(Unicode::truncate($entity->get('feeds_item')->url, 30, FALSE, TRUE)), 'title' => $entity->get('feeds_item')->url]; $build['table']['#rows'][] = $row; } $build['pager'] = ['#type' => 'pager']; $build['#title'] = $this->t('%title items', ['%title' => $feeds_feed->label()]); return $build; }
/** * {@inheritdoc} */ public function postClear(FeedInterface $feed, StateInterface $state) { $tokens = ['@item' => $this->getItemLabel(), '@items' => $this->getItemLabelPlural(), '%title' => $feed->label()]; if ($state->deleted) { $state->setMessage($this->formatPlural($state->deleted, 'Deleted @count @item from %title.', 'Deleted @count @items from %title.', $tokens)); } else { $state->setMessage($this->t('There are no @items to delete.', $tokens)); } }
/** * {@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; }
/** * {@inheritdoc} */ public function parse(FeedInterface $feed, FetcherResultInterface $fetcher_result) { $result = new ParserResult(); Reader::setExtensionManager(\Drupal::service('feed.bridge.reader')); try { $channel = Reader::importString($fetcher_result->getRaw()); } catch (ExceptionInterface $e) { watchdog_exception('feeds', $e); drupal_set_message($this->t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'error'); return $result; } $result->title = $channel->getTitle(); $result->description = $channel->getDescription(); $result->link = $channel->getLink(); foreach ($channel as $item) { // Reset the parsed item. $parsed_item = array(); // Move the values to an array as expected by processors. $parsed_item['title'] = $item->getTitle(); $parsed_item['guid'] = $item->getId(); $parsed_item['url'] = $item->getLink(); $parsed_item['description'] = $item->getDescription(); if ($enclosure = $item->getEnclosure()) { $parsed_item['enclosures'][] = urldecode($enclosure->url); } if ($author = $item->getAuthor()) { $parsed_item['author_name'] = $author['name']; } if ($date = $item->getDateModified()) { $parsed_item['timestamp'] = $date->getTimestamp(); } $parsed_item['tags'] = $item->getCategories()->getValues(); $result->items[] = $parsed_item; } return $result; }
/** * Starts the process batch. * * @param \Drupal\feeds\FeedInterface $feed * The feed. * @param \Drupal\feeds\Result\ParserResultInterface $parser_result * The parser result. */ protected function startBatchProcess(FeedInterface $feed, ParserResultInterface $parser_result) { $batch = ['title' => $this->t('Processing: %title', ['%title' => $feed->label()]), 'init_message' => $this->t('Processing: %title', ['%title' => $feed->label()]), 'progress_message' => $this->t('Processing: %title', ['%title' => $feed->label()]), 'error_message' => $this->t('An error occored while processing %title.', ['%title' => $feed->label()])]; foreach ($parser_result as $item) { $batch['operations'][] = [[$this, 'batchProcess'], [$feed, $item]]; } $batch['operations'][] = [[$this, 'batchPostProcess'], [$feed]]; batch_set($batch); }
/** * {@inheritdoc} */ public function clear(FeedInterface $feed) { $state = $feed->getState(StateInterface::CLEAR); // 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 = $count_query->count()->execute(); } // Delete a batch of entities. $entity_ids = $query->range(0, $this->importer->getLimit())->execute(); if ($entity_ids) { $this->entityDeleteMultiple($entity_ids); $state->deleted += count($entity_ids); $state->progress($state->total, $state->deleted); } else { $state->progress($state->total, $state->total); } // Report results when done. if ($feed->progressClearing() == StateInterface::BATCH_COMPLETE) { if ($state->deleted) { $message = format_plural($state->deleted, 'Deleted @number @entity from %title.', 'Deleted @number @entities from %title.', array('@number' => $state->deleted, '@entity' => Unicode::strtolower($this->entityLabel()), '@entities' => Unicode::strtolower($this->entityLabelPlural()), '%title' => $feed->label())); $feed->log('clear', $message, array(), WATCHDOG_INFO); drupal_set_message($message); } else { drupal_set_message($this->t('There are no @entities to delete.', array('@entities' => Unicode::strtolower($this->entityLabelPlural())))); } } }
protected function unsubscribe(FeedInterface $feed, SubscriptionInterface $subscription = NULL) { if (!$subscription) { return; } $subscription->unsubscribe(); $batch = ['title' => t('Unsubscribing from: %title', ['%title' => $feed->label()]), 'init_message' => t('Unsubscribing from: %title', ['%title' => $feed->label()]), 'operations' => [['Drupal\\feeds\\EventSubscriber\\PubSubHubbub::runSubscribeBatch', [$subscription]]], 'progress_message' => t('Unsubscribing: %title', ['%title' => $feed->label()]), 'error_message' => t('An error occored while unsubscribing from %title.', ['%title' => $feed->label()])]; batch_set($batch); }