View source
<?php
namespace Drupal\aggregator\Form;
use Drupal\aggregator\FeedStorageInterface;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\ClientInterface;
class OpmlFeedAdd extends FormBase {
protected $feedStorage;
protected $httpClient;
public function __construct(FeedStorageInterface $feed_storage, ClientInterface $http_client) {
$this->feedStorage = $feed_storage;
$this->httpClient = $http_client;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager')
->getStorage('aggregator_feed'), $container
->get('http_client'));
}
public function getFormId() {
return 'aggregator_opml_add';
}
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;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$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.'));
}
}
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 {
try {
$response = $this->httpClient
->get($form_state
->getValue('remote'));
$data = (string) $response
->getBody();
} catch (RequestException $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;
}
foreach ($feeds as $feed) {
if (!UrlHelper::isValid($feed['url'], TRUE)) {
$this
->messenger()
->addWarning($this
->t('The URL %url is invalid.', [
'%url' => $feed['url'],
]));
continue;
}
$query = $this->feedStorage
->getQuery();
$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');
}
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;
}
}