You are here

class OpmlFeedAdd in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/aggregator/src/Form/OpmlFeedAdd.php \Drupal\aggregator\Form\OpmlFeedAdd

Imports feeds from OPML.

@internal

Hierarchy

Expanded class hierarchy of OpmlFeedAdd

1 string reference to 'OpmlFeedAdd'
aggregator.routing.yml in core/modules/aggregator/aggregator.routing.yml
core/modules/aggregator/aggregator.routing.yml

File

core/modules/aggregator/src/Form/OpmlFeedAdd.php, line 19

Namespace

Drupal\aggregator\Form
View source
class OpmlFeedAdd extends FormBase {

  /**
   * The feed storage.
   *
   * @var \Drupal\aggregator\FeedStorageInterface
   */
  protected $feedStorage;

  /**
   * The HTTP client to fetch the feed data with.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  protected $httpClient;

  /**
   * Constructs a database object.
   *
   * @param \Drupal\aggregator\FeedStorageInterface $feed_storage
   *   The feed storage.
   * @param \GuzzleHttp\ClientInterface $http_client
   *   The Guzzle HTTP client.
   */
  public function __construct(FeedStorageInterface $feed_storage, ClientInterface $http_client) {
    $this->feedStorage = $feed_storage;
    $this->httpClient = $http_client;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager')
      ->getStorage('aggregator_feed'), $container
      ->get('http_client'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'aggregator_opml_add';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $intervals = [
      900,
      1800,
      3600,
      7200,
      10800,
      21600,
      32400,
      43200,
      64800,
      86400,
      172800,
      259200,
      604800,
      1209600,
      2419200,
    ];
    $period = array_map([
      \Drupal::service('date.formatter'),
      'formatInterval',
    ], array_combine($intervals, $intervals));
    $form['upload'] = [
      '#type' => 'file',
      '#title' => $this
        ->t('OPML File'),
      '#description' => $this
        ->t('Upload an OPML file containing a list of feeds to be imported.'),
    ];
    $form['remote'] = [
      '#type' => 'url',
      '#title' => $this
        ->t('OPML Remote URL'),
      '#maxlength' => 1024,
      '#description' => $this
        ->t('Enter the URL of an OPML file. This file will be downloaded and processed only once on submission of the form.'),
    ];
    $form['refresh'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Update interval'),
      '#default_value' => 3600,
      '#options' => $period,
      '#description' => $this
        ->t('The length of time between feed updates. Requires a correctly configured <a href=":cron">cron maintenance task</a>.', [
        ':cron' => Url::fromRoute('system.status')
          ->toString(),
      ]),
    ];
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Import'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // If both fields are empty or filled, cancel.
    $all_files = $this
      ->getRequest()->files
      ->get('files', []);
    if ($form_state
      ->isValueEmpty('remote') == empty($all_files['upload'])) {
      $form_state
        ->setErrorByName('remote', $this
        ->t('<em>Either</em> upload a file or enter a URL.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $validators = [
      'file_validate_extensions' => [
        'opml xml',
      ],
    ];
    if ($file = file_save_upload('upload', $validators, FALSE, 0)) {
      $data = file_get_contents($file
        ->getFileUri());
    }
    else {

      // @todo Move this to a fetcher implementation.
      try {
        $response = $this->httpClient
          ->get($form_state
          ->getValue('remote'));
        $data = (string) $response
          ->getBody();
      } catch (TransferException $e) {
        $this
          ->logger('aggregator')
          ->warning('Failed to download OPML file due to "%error".', [
          '%error' => $e
            ->getMessage(),
        ]);
        $this
          ->messenger()
          ->addStatus($this
          ->t('Failed to download OPML file due to "%error".', [
          '%error' => $e
            ->getMessage(),
        ]));
        return;
      }
    }
    $feeds = $this
      ->parseOpml($data);
    if (empty($feeds)) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('No new feed has been added.'));
      return;
    }

    // @todo Move this functionality to a processor.
    foreach ($feeds as $feed) {

      // Ensure URL is valid.
      if (!UrlHelper::isValid($feed['url'], TRUE)) {
        $this
          ->messenger()
          ->addWarning($this
          ->t('The URL %url is invalid.', [
          '%url' => $feed['url'],
        ]));
        continue;
      }

      // Check for duplicate titles or URLs.
      $query = $this->feedStorage
        ->getQuery()
        ->accessCheck(FALSE);
      $condition = $query
        ->orConditionGroup()
        ->condition('title', $feed['title'])
        ->condition('url', $feed['url']);
      $ids = $query
        ->condition($condition)
        ->execute();
      $result = $this->feedStorage
        ->loadMultiple($ids);
      foreach ($result as $old) {
        if (strcasecmp($old
          ->label(), $feed['title']) == 0) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('A feed named %title already exists.', [
            '%title' => $old
              ->label(),
          ]));
          continue 2;
        }
        if (strcasecmp($old
          ->getUrl(), $feed['url']) == 0) {
          $this
            ->messenger()
            ->addWarning($this
            ->t('A feed with the URL %url already exists.', [
            '%url' => $old
              ->getUrl(),
          ]));
          continue 2;
        }
      }
      $new_feed = $this->feedStorage
        ->create([
        'title' => $feed['title'],
        'url' => $feed['url'],
        'refresh' => $form_state
          ->getValue('refresh'),
      ]);
      $new_feed
        ->save();
    }
    $form_state
      ->setRedirect('aggregator.admin_overview');
  }

  /**
   * Parses an OPML file.
   *
   * Feeds are recognized as <outline> elements with the attributes "text" and
   * "xmlurl" set.
   *
   * @param string $opml
   *   The complete contents of an OPML document.
   *
   * @return array
   *   An array of feeds, each an associative array with a "title" and a "url"
   *   element, or NULL if the OPML document failed to be parsed. An empty array
   *   will be returned if the document is valid but contains no feeds, as some
   *   OPML documents do.
   *
   * @todo Move this to a parser in https://www.drupal.org/node/1963540.
   */
  protected function parseOpml($opml) {
    $feeds = [];
    $xml_parser = xml_parser_create();
    xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
    if (xml_parse_into_struct($xml_parser, $opml, $values)) {
      foreach ($values as $entry) {
        if ($entry['tag'] == 'OUTLINE' && isset($entry['attributes'])) {
          $item = $entry['attributes'];
          if (!empty($item['XMLURL']) && !empty($item['TEXT'])) {
            $feeds[] = [
              'title' => $item['TEXT'],
              'url' => $item['XMLURL'],
            ];
          }
        }
      }
    }
    xml_parser_free($xml_parser);
    return $feeds;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 3
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 3
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route.
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
OpmlFeedAdd::$feedStorage protected property The feed storage.
OpmlFeedAdd::$httpClient protected property The HTTP client to fetch the feed data with.
OpmlFeedAdd::buildForm public function Form constructor. Overrides FormInterface::buildForm
OpmlFeedAdd::create public static function Instantiates a new instance of this class. Overrides FormBase::create
OpmlFeedAdd::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
OpmlFeedAdd::parseOpml protected function Parses an OPML file.
OpmlFeedAdd::submitForm public function Form submission handler. Overrides FormInterface::submitForm
OpmlFeedAdd::validateForm public function Form validation handler. Overrides FormBase::validateForm
OpmlFeedAdd::__construct public function Constructs a database object.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.