Skip to content
Snippets Groups Projects
Commit 2b2aaeca authored by Alex Barth's avatar Alex Barth
Browse files

#686462: Move FeedsEnclosure class to FeedsParser.inc.

parent 69cee071
No related branches found
No related tags found
No related merge requests found
......@@ -113,7 +113,7 @@ function _feeds_feeds_plugins() {
'description' => 'Parse RSS and Atom feeds.',
'help' => 'Use <a href="http://simplepie.org">SimplePie</a> to parse XML feeds in RSS 1, RSS 2 and Atom format.',
'handler' => array(
'parent' => 'FeedsSyndicationParser',
'parent' => 'FeedsParser',
'class' => 'FeedsSimplePieParser',
'file' => 'FeedsSimplePieParser.inc',
'path' => $path,
......
......@@ -97,6 +97,67 @@ class FeedsElement {
}
}
/**
* Enclosure element, can be part of the result array.
*/
class FeedsEnclosure extends FeedsElement {
protected $mime_type;
protected $file;
/**
* Constructor, requires MIME type.
*/
public function __construct($value, $mime_type) {
parent::__construct($value);
$this->mime_type = $mime_type;
}
/**
* @return
* MIME type of return value of getValue().
*/
public function getMIMEType() {
return $this->mime_type;
}
/**
* @return
* The content of the referenced resource.
*/
public function getContent() {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this->getValue());
if ($result->code != 200) {
throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->getValue(), '!code' => $result->code)));
}
return $result->data;
}
/**
* @return
* The file path to the downloaded resource referenced by the enclosure.
* Downloads resource if not downloaded yet.
*
* @todo Get file extension from mime_type.
* @todo This is not concurrency safe.
*/
public function getFile() {
if(!isset($this->file)) {
$dest = file_destination(file_directory_temp() .'/'. get_class($this) .'-'. basename($this->getValue()), FILE_EXISTS_RENAME);
if (ini_get('allow_url_fopen')) {
$this->file = copy($this->getValue(), $dest) ? $dest : 0;
}
else {
$this->file = file_save_data($this->getContent(), $dest);
}
if ($this->file === 0) {
throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest)));
}
}
return $this->file;
}
}
/**
* Defines a date element of a parsed result (including ranges, repeat).
*/
......
<?php
// $Id$
/**
* Enclosure element, can be part of the result array.
*/
class FeedsEnclosure extends FeedsElement {
protected $mime_type;
protected $file;
/**
* Constructor, requires MIME type.
*/
public function __construct($value, $mime_type) {
parent::__construct($value);
$this->mime_type = $mime_type;
}
/**
* @return
* MIME type of return value of getValue().
*/
public function getMIMEType() {
return $this->mime_type;
}
/**
* @return
* The content of the referenced resource.
*/
public function getContent() {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this->getValue());
if ($result->code != 200) {
throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->getValue(), '!code' => $result->code)));
}
return $result->data;
}
/**
* @return
* The file path to the downloaded resource referenced by the enclosure.
* Downloads resource if not downloaded yet.
*
* @todo Get file extension from mime_type.
* @todo This is not concurrency safe.
*/
public function getFile() {
if(!isset($this->file)) {
$dest = file_destination(file_directory_temp() .'/'. get_class($this) .'-'. basename($this->getValue()), FILE_EXISTS_RENAME);
if (ini_get('allow_url_fopen')) {
$this->file = copy($this->getValue(), $dest) ? $dest : 0;
}
else {
$this->file = file_save_data($this->getContent(), $dest);
}
if ($this->file === 0) {
throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest)));
}
}
return $this->file;
}
}
/**
* Class definition for Common Syndication Parser.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment