/** * {@inheritdoc} */ public function fetch(FeedInterface $feed) { if ($feed->label() == 'Do not fetch') { return FALSE; } return parent::fetch($feed); }
/** * {@inheritdoc} */ public function refresh(FeedInterface $feed) { // Store feed URL to track changes. $feed_url = $feed->getUrl(); // Fetch the feed. try { $success = $this->fetcherManager->createInstance($this->config->get('fetcher'))->fetch($feed); } catch (PluginException $e) { $success = FALSE; watchdog_exception('aggregator', $e); } // Store instances in an array so we dont have to instantiate new objects. $processor_instances = array(); foreach ($this->config->get('processors') as $processor) { try { $processor_instances[$processor] = $this->processorManager->createInstance($processor); } catch (PluginException $e) { watchdog_exception('aggregator', $e); } } // We store the hash of feed data in the database. When refreshing a // feed we compare stored hash and new hash calculated from downloaded // data. If both are equal we say that feed is not updated. $hash = hash('sha256', $feed->source_string); $has_new_content = $success && $feed->getHash() != $hash; if ($has_new_content) { // Parse the feed. try { if ($this->parserManager->createInstance($this->config->get('parser'))->parse($feed)) { if (!$feed->getWebsiteUrl()) { $feed->setWebsiteUrl($feed->getUrl()); } $feed->setHash($hash); // Update feed with parsed data. $feed->save(); // Log if feed URL has changed. if ($feed->getUrl() != $feed_url) { $this->logger->notice('Updated URL for feed %title to %url.', array('%title' => $feed->label(), '%url' => $feed->getUrl())); } $this->logger->notice('There is new syndicated content from %site.', array('%site' => $feed->label())); // If there are items on the feed, let enabled processors process them. if (!empty($feed->items)) { foreach ($processor_instances as $instance) { $instance->process($feed); } } } } catch (PluginException $e) { watchdog_exception('aggregator', $e); } } // Processing is done, call postProcess on enabled processors. foreach ($processor_instances as $instance) { $instance->postProcess($feed); } return $has_new_content; }
/** * {@inheritdoc} */ public function getFeedDuplicates(FeedInterface $feed) { $query = \Drupal::entityQuery('aggregator_feed'); $or_condition = $query->orConditionGroup()->condition('title', $feed->label())->condition('url', $feed->getUrl()); $query->condition($or_condition); if ($feed->id()) { $query->condition('fid', $feed->id(), '<>'); } return $this->loadMultiple($query->execute()); }
/** * {@inheritdoc} */ public function fetch(FeedInterface $feed) { $request = $this->httpClient->createRequest('GET', $feed->getUrl()); $feed->source_string = FALSE; // Generate conditional GET headers. if ($feed->getEtag()) { $request->addHeader('If-None-Match', $feed->getEtag()); } if ($feed->getLastModified()) { $request->addHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified())); } try { $response = $this->httpClient->send($request); // In case of a 304 Not Modified, there is no new content, so return // FALSE. if ($response->getStatusCode() == 304) { return FALSE; } $feed->source_string = $response->getBody(TRUE); $feed->setEtag($response->getHeader('ETag')); $feed->setLastModified(strtotime($response->getHeader('Last-Modified'))); $feed->http_headers = $response->getHeaders(); // Update the feed URL in case of a 301 redirect. if ($response->getEffectiveUrl() != $feed->getUrl()) { $feed->setUrl($response->getEffectiveUrl()); } return TRUE; } catch (RequestException $e) { $this->logger->warning('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())); drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'warning'); return FALSE; } }
/** * {@inheritdoc} */ public function parse(FeedInterface $feed) { // Set our bridge extension manager to Zend Feed. Reader::setExtensionManager(\Drupal::service('feed.bridge.reader')); try { $channel = Reader::importString($feed->source_string); } catch (ExceptionInterface $e) { watchdog_exception('aggregator', $e); drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'error'); return FALSE; } $feed->setWebsiteUrl($channel->getLink()); $feed->setDescription($channel->getDescription()); if ($image = $channel->getImage()) { $feed->setImage($image['uri']); } // Initialize items array. $feed->items = array(); 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['link'] = $item->getLink(); $parsed_item['description'] = $item->getDescription(); $parsed_item['author'] = ''; if ($author = $item->getAuthor()) { $parsed_item['author'] = $author['name']; } $parsed_item['timestamp'] = ''; if ($date = $item->getDateModified()) { $parsed_item['timestamp'] = $date->getTimestamp(); } // Store on $feed object. This is where processors will look for parsed items. $feed->items[] = $parsed_item; } return TRUE; }
/** * {@inheritdoc} */ public function fetch(FeedInterface $feed) { $request = new Request('GET', $feed->getUrl()); $feed->source_string = FALSE; // Generate conditional GET headers. if ($feed->getEtag()) { $request = $request->withAddedHeader('If-None-Match', $feed->getEtag()); } if ($feed->getLastModified()) { $request = $request->withAddedHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified())); } try { /** @var \Psr\Http\Message\UriInterface $actual_uri */ $actual_uri = NULL; $response = $this->httpClientFactory->fromOptions(['allow_redirects' => ['on_redirect' => function (RequestInterface $request, ResponseInterface $response, UriInterface $uri) use(&$actual_uri) { $actual_uri = (string) $uri; }]])->send($request); // In case of a 304 Not Modified, there is no new content, so return // FALSE. if ($response->getStatusCode() == 304) { return FALSE; } $feed->source_string = (string) $response->getBody(); if ($response->hasHeader('ETag')) { $feed->setEtag($response->getHeaderLine('ETag')); } if ($response->hasHeader('Last-Modified')) { $feed->setLastModified(strtotime($response->getHeaderLine('Last-Modified'))); } $feed->http_headers = $response->getHeaders(); // Update the feed URL in case of a 301 redirect. if ($actual_uri && $actual_uri !== $feed->getUrl()) { $feed->setUrl($actual_uri); } return TRUE; } catch (RequestException $e) { $this->logger->warning('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())); drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'warning'); return FALSE; } }
/** * Implements \Drupal\aggregator\Plugin\ProcessorInterface::postProcess(). * * Expires items from a feed depending on expiration settings. */ public function postProcess(FeedInterface $feed) { $aggregator_clear = $this->configuration['items']['expire']; if ($aggregator_clear != AGGREGATOR_CLEAR_NEVER) { // Delete all items that are older than flush item timer. $age = REQUEST_TIME - $aggregator_clear; $result = $this->itemQuery->condition('fid', $feed->id())->condition('timestamp', $age, '<')->execute(); if ($result) { $entities = $this->itemStorage->loadMultiple($result); $this->itemStorage->delete($entities); } } }
/** * Confirms an item removal from a feed. * * @param \Drupal\aggregator\FeedInterface $feed * Feed object representing the feed. */ public function deleteFeedItems(FeedInterface $feed) { $this->drupalPostForm('admin/config/services/aggregator/delete/' . $feed->id(), array(), t('Delete items')); $this->assertRaw(t('The news items from %title have been deleted.', array('%title' => $feed->label())), 'Feed items deleted.'); }
/** * Route title callback. * * @param \Drupal\aggregator\FeedInterface $aggregator_feed * The aggregator feed. * * @return string * The feed label. */ public function feedTitle(FeedInterface $aggregator_feed) { return Xss::filter($aggregator_feed->label()); }
/** * {@inheritdoc} */ public function getItemCount(FeedInterface $feed) { $query = \Drupal::entityQuery('aggregator_item')->condition('fid', $feed->id())->count(); return $query->execute(); }
/** * {@inheritdoc} */ public function postProcess(FeedInterface $feed) { // Double the refresh rate. $feed->refresh->value *= 2; $feed->save(); }
/** * Route title callback. * * @param \Drupal\aggregator\FeedInterface $aggregator_feed * The aggregator feed. * * @return array * The feed label as a render array. */ public function feedTitle(FeedInterface $aggregator_feed) { return ['#markup' => $aggregator_feed->label(), '#allowed_tags' => Xss::getHtmlTagList()]; }
/** * Route title callback. * * @param \Drupal\aggregator\FeedInterface $aggregator_feed * The aggregator feed. * * @return string * The feed label. */ public function feedTitle(FeedInterface $aggregator_feed) { return SafeMarkup::xssFilter($aggregator_feed->label()); }