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

#708228 Scott Reynolds, alex_b: Break FeedsImportBatch into separate classes.

parent 255a25d6
No related branches found
No related tags found
No related merge requests found
......@@ -3,6 +3,7 @@
Feeds 6.x 1.0 xxxxx xx, xxxx-xx-xx
----------------------------------
- #708228 Scott Reynolds, alex_b: Break FeedsImportBatch into separate classes.
- alex_b: Support mapping to OpenID, using OpenID as a unique mapping target.
- alex_b: Handle exceptions outside of Importer/Source facade methods.
- #600584 alex_b: Use Batch API. NOTE: third party plugins/extensions
......
......@@ -20,76 +20,70 @@ class FeedsBatch {
}
/**
* A FeedsImportBatch is the actual content retrieved from a FeedsSource. On
* A FeedsImportBatch wraps the actual content retrieved from a FeedsSource. On
* import, it is created on the fetching stage and passed through the parsing
* and processing stage where it is normalized and consumed.
*
* @see FeedsSource class
* @see FeedsFetcher class
* A Fetcher must return a FeedsImportBatch object on fetch(). To that end it
* must use either one of the existing implementations of FeedsImportBatch
* (FeedsFileBatch or FeedsHTTPBatch) or it must extend FeedsImportBatch and
* implement at least
*
* - getRaw() returning the raw content from the source as a string and
* - getFilePath() returning a path to a file containing the raw content from
* the source.
*
* A Parser must populate a FeedsImportBatch object through the set methods upon
* parse(). For instance:
*
* $batch->setTitle('My imported document');
* $batch->setItems($parsed_rows);
*
* Finally, a processor can work off the information produced on the parsing
* stage by consuming items with $batch->shiftItem().
*
* while ($item = $batch->shiftItem()) {
* $object = $this->map($item);
* $object->save();
* }
*
* Note: Knowledge of the internal structure of a single item in the $items
* array is managed by the mapping API specified in FeedsParser class and
* FeedsProcessor class.
*
* @see FeedsFileBatch
* @see FeedsHTTPBatch
*/
class FeedsImportBatch extends FeedsBatch {
protected $url;
protected $file_path;
protected $raw;
protected $items;
abstract class FeedsImportBatch extends FeedsBatch {
protected $title;
protected $description;
protected $link;
protected $items;
/**
* Constructor.
*
* Either $url or $file_path must be given.
*/
public function __construct($url = NULL, $file_path = NULL) {
$this->url = $url;
$this->file_path = $file_path;
public function __construct() {
$this->title = '';
$this->description = '';
$this->link = '';
$this->items = array();
parent::__construct();
}
/**
* @return
* The raw content of the feed.
*/
public function getRaw() {
if ($this->file_path) {
return file_get_contents(realpath($this->file_path));
}
elseif ($this->url) {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this->url);
if ($result->code != 200) {
throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code)));
}
return $result->data;
}
}
/**
* @return
* Path to the feed. This path is relative to Drupal's root directory.
* If the feed is not local, getFilePath downloads it to file directory.
* The raw content from the source as a string.
*
* @throws Exception
* If an unexpected problem occurred.
*/
public function getFilePath() {
if (!isset($this->file_path)) {
$dest = file_destination(file_directory_path() .'/feeds/'. get_class($this) .'_'. md5($this->url) .'_'. time(), FILE_EXISTS_RENAME);
$this->file_path = file_save_data($this->getRaw(), $dest);
if($this->file_path === 0) {
throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest)));
}
}
return $this->file_path;
}
public abstract function getRaw();
/**
* @return
* URL to the document.
* A path to a file containing the raw content as a source.
*
* @throws Exception
* If an unexpected problem occurred.
*/
public function getURL() {
if (!isset($this->url) && isset($this->file)) {
return $_GLOBALS['base_url'] .'/'. $this->file;
}
}
public abstract function getFilePath();
/**
* @return
......@@ -113,7 +107,7 @@ class FeedsImportBatch extends FeedsBatch {
* feed). Falls back to URL if not available.
*/
public function getLink() {
return isset($this->link) ? $this->link : $this->getURL();
return $this->link;
}
/**
......
......@@ -3,9 +3,39 @@
/**
* @file
* Home of the FeedsFileFetcher.
* Home of the FeedsFileFetcher and related classes.
*/
/**
* Definition of the import batch object created on the fetching stage by
* FeedsFileFetcher.
*/
class FeedsFileBatch extends FeedsImportBatch {
protected $file_path;
/**
* Constructor.
*/
public function __construct($file_path) {
$this->file_path = $file_path;
parent::__construct();
}
/**
* Implementation of FeedsImportBatch::getRaw();
*/
public function getRaw() {
return file_get_contents(realpath($this->file_path));
}
/**
* Implementation of FeedsImportBatch::getFilePath().
*/
public function getFilePath() {
return $this->file_path;
}
}
/**
* Fetches data via HTTP.
*/
......@@ -16,7 +46,7 @@ class FeedsFileFetcher extends FeedsFetcher {
*/
public function fetch(FeedsSource $source) {
$source_config = $source->getConfigFor($this);
return new FeedsImportBatch(NULL, $source_config['source']);
return new FeedsFileBatch($source_config['source']);
}
/**
......
......@@ -3,9 +3,52 @@
/**
* @file
* Home of the FeedsHTTPFetcher.
* Home of the FeedsHTTPFetcher and related classes.
*/
/**
* Definition of the import batch object created on the fetching stage by
* FeedsHTTPFetcher.
*/
class FeedsHTTPBatch extends FeedsImportBatch {
protected $url;
protected $file_path;
/**
* Constructor.
*/
public function __construct($url = NULL) {
$this->url = $url;
parent::__construct();
}
/**
* Implementation of FeedsImportBatch::getRaw();
*/
public function getRaw() {
feeds_include_library('http_request.inc', 'http_request');
$result = http_request_get($this->url);
if ($result->code != 200) {
throw new Exception(t('Download of @url failed with code !code.', array('@url' => $this->url, '!code' => $result->code)));
}
return $result->data;
}
/**
* Implementation of FeedsImportBatch::getFilePath().
*/
public function getFilePath() {
if (!isset($this->file_path)) {
$dest = file_destination(file_directory_path() .'/feeds/'. get_class($this) .'_'. md5($this->url) .'_'. time(), FILE_EXISTS_RENAME);
$this->file_path = file_save_data($this->getRaw(), $dest);
if($this->file_path === 0) {
throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest)));
}
}
return $this->file_path;
}
}
/**
* Fetches data via HTTP.
*/
......@@ -16,7 +59,7 @@ class FeedsHTTPFetcher extends FeedsFetcher {
*/
public function fetch(FeedsSource $source) {
$source_config = $source->getConfigFor($this);
return new FeedsImportBatch($source_config['source']);
return new FeedsHTTPBatch($source_config['source']);
}
/**
......
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