Commit 2d1bf3e3 authored by webchick's avatar webchick

Issue #2028037 by marvin_B8, LinL, jsbalsera, ceng, Berdir: Expand FeedInterface with methods.

parent 3d984d28
......@@ -180,7 +180,7 @@ function aggregator_cron() {
$feed = aggregator_feed_load($fid);
if ($queue->createItem($feed)) {
// Add timestamp to avoid queueing item more than once.
$feed->queued->value = REQUEST_TIME;
$feed->setQueuedTime(REQUEST_TIME);
$feed->save();
}
}
......@@ -214,7 +214,7 @@ function aggregator_queue_info() {
*/
function aggregator_refresh(Feed $feed) {
// Store feed URL to track changes.
$feed_url = $feed->url->value;
$feed_url = $feed->getUrl();
$config = \Drupal::config('aggregator.settings');
// Fetch the feed.
......@@ -245,21 +245,21 @@ function aggregator_refresh(Feed $feed) {
// data. If both are equal we say that feed is not updated.
$hash = hash('sha256', $feed->source_string);
if ($success && ($feed->hash->value != $hash)) {
if ($success && ($feed->getHash() != $hash)) {
// Parse the feed.
$parser_manager = \Drupal::service('plugin.manager.aggregator.parser');
try {
if ($parser_manager->createInstance($config->get('parser'))->parse($feed)) {
if (empty($feed->link->value)) {
$feed->link->value = $feed->url->value;
if ($feed->getWebsiteUrl()) {
$feed->setWebsiteUrl($feed->getUrl());
}
$feed->hash->value = $hash;
$feed->setHash($hash);
// Update feed with parsed data.
$feed->save();
// Log if feed URL has changed.
if ($feed->url->value != $feed_url) {
watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->label(), '%url' => $feed->url->value));
if ($feed->getUrl() != $feed_url) {
watchdog('aggregator', 'Updated URL for feed %title to %url.', array('%title' => $feed->label(), '%url' => $feed->getUrl()));
}
watchdog('aggregator', 'There is new syndicated content from %site.', array('%site' => $feed->label()));
......@@ -282,8 +282,8 @@ function aggregator_refresh(Feed $feed) {
}
// Regardless of successful or not, indicate that this feed has been checked.
$feed->checked->value = REQUEST_TIME;
$feed->queued->value = 0;
$feed->setLastCheckedTime(REQUEST_TIME);
$feed->setQueuedTime(0);
$feed->save();
// Processing is done, call postProcess on enabled processors.
......
......@@ -174,27 +174,27 @@ function template_preprocess_aggregator_feed_source(&$variables) {
$feed_icon = array(
'#theme' => 'feed_icon',
'#url' => $feed->url->value,
'#url' => $feed->getUrl(),
'#title' => t('!title feed', array('!title' => $feed->label())),
);
$variables['source_icon'] = drupal_render($feed_icon);
if (!empty($feed->image->value) && $feed->label() && !empty($feed->link->value)) {
if ($feed->getImage() && $feed->label() && $feed->getWebsiteUrl()) {
$image = array(
'#theme' => 'image',
'#path' => $feed->image->value,
'#alt' => $feed->title->value,
'#path' => $feed->getImage(),
'#alt' => $feed->label(),
);
$variables['source_image'] = l($image, $feed->link->value, array('html' => TRUE, 'attributes' => array('class' => 'feed-image')));
$variables['source_image'] = l($image, $feed->getWebsiteUrl(), array('html' => TRUE, 'attributes' => array('class' => 'feed-image')));
}
else {
$variables['source_image'] = '';
}
$variables['source_description'] = aggregator_filter_xss($feed->description->value);
$variables['source_url'] = check_url(url($feed->link->value, array('absolute' => TRUE)));
$variables['source_description'] = aggregator_filter_xss($feed->getDescription());
$variables['source_url'] = check_url(url($feed->getWebsiteUrl(), array('absolute' => TRUE)));
if ($feed->checked) {
$variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->checked->value)));
$variables['last_checked'] = t('@time ago', array('@time' => format_interval(REQUEST_TIME - $feed->getLastCheckedTime())));
}
else {
$variables['last_checked'] = t('never');
......
......@@ -39,121 +39,6 @@
*/
class Feed extends ContentEntityBase implements FeedInterface {
/**
* The feed ID.
*
* @todo rename to id.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $fid;
/**
* Title of the feed.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $title;
/**
* The feed language code.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $langcode;
/**
* URL to the feed.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $url;
/**
* How often to check for new feed items, in seconds.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $refresh;
/**
* Last time feed was checked for new items, as Unix timestamp.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $checked;
/**
* Time when this feed was queued for refresh, 0 if not queued.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $queued;
/**
* The parent website of the feed; comes from the <link> element in the feed.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $link ;
/**
* The parent website's description;
* comes from the <description> element in the feed.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $description;
/**
* An image representing the feed.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $image;
/**
* Calculated hash of the feed data, used for validating cache.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $hash;
/**
* Entity tag HTTP response header, used for validating cache.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $etag;
/**
* When the feed was last modified, as a Unix timestamp.
*
* @var \Drupal\Core\Field\FieldItemListInterface
*/
public $modified;
/**
* {@inheritdoc}
*/
public function init() {
parent::init();
// We unset all defined properties, so magic getters apply.
unset($this->fid);
unset($this->title);
unset($this->url);
unset($this->refresh);
unset($this->checked);
unset($this->queued);
unset($this->link);
unset($this->description);
unset($this->image);
unset($this->hash);
unset($this->etag);
unset($this->modified);
}
/**
* Implements Drupal\Core\Entity\EntityInterface::id().
*/
......@@ -177,11 +62,13 @@ public function removeItems() {
$manager->createInstance($id)->remove($this);
}
// Reset feed.
$this->checked->value = 0;
$this->hash->value = '';
$this->etag->value = '';
$this->modified->value = 0;
$this->setLastCheckedTime(0);
$this->setHash('');
$this->setEtag('');
$this->setLastModified(0);
$this->save();
return $this;
}
/**
......@@ -293,4 +180,162 @@ public static function baseFieldDefinitions($entity_type) {
return $fields;
}
/**
* {@inheritdoc}
*/
public function getUrl() {
return $this->get('url')->value;
}
/**
* {@inheritdoc}
*/
public function getRefreshRate() {
return $this->get('refresh')->value;
}
/**
* {@inheritdoc}
*/
public function getLastCheckedTime() {
return $this->get('checked')->value;
}
/**
* {@inheritdoc}
*/
public function getQueuedTime() {
return $this->get('queued')->value;
}
/**
* {@inheritdoc}
*/
public function getWebsiteUrl() {
return $this->get('link')->value;
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->get('description')->value;
}
/**
* {@inheritdoc}
*/
public function getImage() {
return $this->get('image')->value;
}
/**
* {@inheritdoc}
*/
public function getHash() {
return $this->get('hash')->value;
}
/**
* {@inheritdoc}
*/
public function getEtag() {
return $this->get('etag')->value;
}
/**
* {@inheritdoc}
*/
public function getLastModified() {
return $this->get('modified')->value;
}
/**
* {@inheritdoc}
*/
public function setTitle($title) {
$this->set('title', $title);
return $this;
}
/**
* {@inheritdoc}
*/
public function setUrl($url) {
$this->set('url', $url);
return $this;
}
/**
* {@inheritdoc}
*/
public function setRefreshRate($refresh) {
$this->set('refresh', $refresh);
return $this;
}
/**
* {@inheritdoc}
*/
public function setLastCheckedTime($checked) {
$this->set('checked', $checked);
return $this;
}
/**
* {@inheritdoc}
*/
public function setQueuedTime($queued) {
$this->set('queued', $queued);
return $this;
}
/**
* {@inheritdoc}
*/
public function setWebsiteUrl($link) {
$this->set('link', $link);
return $this;
}
/**
* {@inheritdoc}
*/
public function setDescription($description) {
$this->set('description', $description);
return $this;
}
/**
* {@inheritdoc}
*/
public function setImage($image) {
$this->set('image', $image);
return $this;
}
/**
* {@inheritdoc}
*/
public function setHash($hash) {
$this->set('hash', $hash);
return $this;
}
/**
* {@inheritdoc}
*/
public function setEtag($etag) {
$this->set('etag', $etag);
return $this;
}
/**
* {@inheritdoc}
*/
public function setLastModified($modified) {
$this->set('modified', $modified);
return $this;
}
}
......@@ -45,14 +45,14 @@ public function form(array $form, array &$form_state) {
$form['url'] = array(
'#type' => 'url',
'#title' => $this->t('URL'),
'#default_value' => $feed->url->value,
'#default_value' => $feed->getUrl(),
'#maxlength' => NULL,
'#description' => $this->t('The fully-qualified URL of the feed.'),
'#required' => TRUE,
);
$form['refresh'] = array('#type' => 'select',
'#title' => $this->t('Update interval'),
'#default_value' => $feed->refresh->value,
'#default_value' => $feed->getRefreshRate(),
'#options' => $period,
'#description' => $this->t('The length of time between feed updates. Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
);
......@@ -72,8 +72,8 @@ public function validate(array $form, array &$form_state) {
if (strcasecmp($item->title, $feed->label()) == 0) {
$this->setFormError('title', $form_state, $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label())));
}
if (strcasecmp($item->url, $feed->url->value) == 0) {
$this->setFormError('url', $form_state, $this->t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $feed->url->value)));
if (strcasecmp($item->url, $feed->getUrl()) == 0) {
$this->setFormError('url', $form_state, $this->t('A feed with this URL %url already exists. Enter a unique URL.', array('%url' => $feed->getUrl())));
}
}
parent::validate($form, $form_state);
......
......@@ -14,8 +14,212 @@
*/
interface FeedInterface extends ContentEntityInterface {
/**
* Sets the title of the feed.
*
* @param string $title
* The short title of the feed.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setTitle($title);
/**
* Returns the url to the feed.
*
* @return string
* The url to the feed.
*/
public function getUrl();
/**
* Sets the url to the feed.
*
* @param string $url
* A string containing the url of the feed.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setUrl($url);
/**
* Returns the refresh rate of the feed in seconds.
*
* @return int
* The refresh rate of the feed in seconds.
*/
public function getRefreshRate();
/**
* Sets the refresh rate of the feed in seconds.
*
* @param int $refresh
* The refresh rate of the feed in seconds.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setRefreshRate($refresh);
/**
* Returns the last time where the feed was checked for new items.
*
* @return int
* The timestamp when new items were last checked for.
*/
public function getLastCheckedTime();
/**
* Sets the time when this feed was queued for refresh, 0 if not queued.
*
* @param int $checked
* The timestamp of the last refresh.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setLastCheckedTime($checked);
/**
* Returns the time when this feed was queued for refresh, 0 if not queued.
*
* @return int
* The timestamp of the last refresh.
*/
public function getQueuedTime();
/**
* Sets the time when this feed was queued for refresh, 0 if not queued.
*
* @param int $queued
* The timestamp of the last refresh.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setQueuedTime($queued);
/**
* Returns the parent website of the feed.
*
* @return string
* The parent website of the feed.
*/
public function getWebsiteUrl();
/**
* Sets the parent website of the feed.
*
* @param string $link
* A string containing the parent website of the feed.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setWebsiteUrl($link);
/**
* Returns the description of the feed.
*
* @return string
* The description of the feed.
*/
public function getDescription();
/**
* Sets the description of the feed.
*
* @param string $description
* The description of the feed.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setDescription($description);
/**
* Returns the primary image attached to the feed.
*
* @return string
* The URL of the primary image attached to the feed.
*/
public function getImage();
/**
* Sets the primary image attached to the feed.
*
* @param string $image
* An image URL.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setImage($image);
/**
* Returns the calculated hash of the feed data, used for validating cache.
*
* @return string
* The calculated hash of the feed data.
*/
public function getHash();
/**
* Sets the calculated hash of the feed data, used for validating cache.
*
* @param string $hash
* A string containing the calculated hash of the feed.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setHash($hash);
/**
* Returns the entity tag HTTP response header, used for validating cache.
*
* @return string
* The entity tag HTTP response header.
*/
public function getEtag();
/**
* Sets the entity tag HTTP response header, used for validating cache.
*
* @param string $etag
* A string containing the entity tag HTTP response header.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setEtag($etag);
/**
* Return when the feed was modified last time.
*
* @return int
* The timestamp of the last time the feed was modified.
*/
public function getLastModified();
/**
* Sets the last modification of the feed.
*
* @param int $modified
* The timestamp when the feed was modified.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function setLastModified($modified);
/**
* Removes all items from a feed.
*
* @return \Drupal\aggregator\FeedInterface
* The class instance that this method is called on.
*/
public function removeItems();
......
......@@ -177,8 +177,8 @@ public function submitForm(array &$form, array &$form_state) {
drupal_set_message($this->t('A feed named %title already exists.', array('%title' => $old->label())), 'warning');
continue 2;
}
if (strcasecmp($old->url->value, $feed['url']) == 0) {
drupal_set_message($this->t('A feed with the URL %url already exists.', array('%url' => $old->url->value)), 'warning');
if (strcasecmp($old->getUrl(), $feed['url']) == 0) {
drupal_set_message($this->t('A feed with the URL %url already exists.', array('%url' => $old->getUrl())), 'warning');
continue 2;
}
}
......
......@@ -25,7 +25,7 @@ interface FetcherInterface {
*
* @param \Drupal\aggregator\Entity\Feed $feed
* A feed object representing the resource to be downloaded.
* $feed->url->value contains the link to the feed.
* $feed->getUrl() contains the link to the feed.
* Download the data at the URL and expose it
* to other modules by attaching it to $feed->source_string.
*
......
......@@ -58,15 +58,15 @@ public static function create(ContainerInterface $container, array $configuratio
* {@inheritdoc}
*/
public function fetch(Feed $feed) {
$request = $this->httpClient->get($feed->url->value);
$request = $this->httpClient->get($feed->getUrl());
$feed->source_string = FALSE;
// Generate conditional GET headers.
if ($feed->etag->value) {
$request->addHeader('If-None-Match', $feed->etag->value);
if ($feed->getEtag()) {
$request->addHeader('If-None-Match', $feed->getEtag());
}
if ($feed->modified->value) {
$request->addHeader('If-Modified-Since', gmdate(DATE_RFC1123, $feed->modified->value));
if ($feed->getLastModified()) {
$request->addHeader('If-Modified-Since', gmdate(DATE_RFC1123, $feed->getLastModified()));
}
try {
......@@ -79,14 +79,14 @@ public function fetch(Feed $feed) {
}
$feed->source_string = $response->getBody(TRUE);
$feed->etag = $response->getEtag();
$feed->modified = strtotime($response->getLastModified());
$feed->setEtag($response->getEtag());
$feed->setLastModified(strtotime($response->getLastModified()));
$feed->http_headers = $response->getHeaders();
// Update the feed URL in case of a 301 redirect.
if ($response->getEffectiveUrl() != $feed->url->value) {
$feed->url->value = $response->getEffectiveUrl();
if ($response->getEffectiveUrl() != $feed->getUrl()) {
$feed->setUrl($response->getEffectiveUrl());
}
return TRUE;
}
......