? .git ? .gitignore ? 641522-8_enclosure_and_results_interfaces.patch ? libraries/simplepie.inc Index: feeds.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/feeds.module,v retrieving revision 1.20 diff -u -p -r1.20 feeds.module --- feeds.module 16 Nov 2009 14:52:38 -0000 1.20 +++ feeds.module 4 Dec 2009 00:20:08 -0000 @@ -257,14 +257,14 @@ function feeds_nodeapi(&$node, $op, $for $source->addConfig($node->feeds); $result = $importer->fetcher->fetch($source); $result = $importer->parser->parse($result, $source); - if (!isset($result->value['title']) || trim($result->value['title']) == '') { - form_set_error('title', t('Could not retrieve title from feed.'), 'error'); - } - else { + if ($title = trim($result->getTitle())) { // Keep the title in a static cache and populate $node->title on // 'presave' as node module looses any changes to $node after // 'validate'. - $last_title = $result->value['title']; + $last_title = $title; + } + else { + form_set_error('title', t('Could not retrieve title from feed.'), 'error'); } } catch (Exception $e) { Index: includes/FeedsImporter.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/includes/FeedsImporter.inc,v retrieving revision 1.4 diff -u -p -r1.4 FeedsImporter.inc --- includes/FeedsImporter.inc 21 Oct 2009 22:49:47 -0000 1.4 +++ includes/FeedsImporter.inc 4 Dec 2009 00:20:08 -0000 @@ -11,54 +11,47 @@ require_once(dirname(__FILE__) .'/FeedsC require_once(dirname(__FILE__) .'/FeedsSource.inc'); /** - * A Feeds result class. + * Abstraction of a file handled by Feeds. * - * @see class FeedsFetcherResult - * @see class FeedsParserResult + * @see FeedsFetcherResult + * @see FeedsEnclosure */ -abstract class FeedsResult { - - // An array of valid values for $type. - protected $valid_types = array(); - // The type of this result. - protected $type; - // The value of this result. - protected $value; +interface FeedsFileInterface { + /** + * @var A string containg the default mime type to use when a + * FeedsFileInterface's mime type cannot be determined. + */ + const DEFAULT_MIME_TYPE = 'application/octet-stream'; /** - * Constructor: create object, validate class variables. + * Get the filename of a file for tthis FeedsFileInterface. If the actual + * file is a remote one, implementation should download it to a temporary + * local file. This allow transparent access to the file by consumers. * - * @param $value - * The value of this result. - * @param $type - * The type of this result. Must be one of $valid_types. + * @return A string containing the filename of a local file. */ - public function __construct($value, $type) { - $this->__set('type', $type); - $this->__set('value', $value); - } + public function getFile(); /** - * Control access to class variables. + * Get the content of the file as a string. When the content of the file + * cannot be represented safely as a string, the implementation should throw + * an exception. + * + * @return A strign containing the content of the file. */ - public function __set($name, $value) { - if ($name == 'valid_types') { - throw new Exception(t('Cannot write FeedsResult::valid_types.')); - } - if ($name == 'type') { - if (!in_array($value, $this->valid_types)) { - throw new Exception(t('Invalid type "!type"', array('!type' => $value))); - } - } - $this->$name = $value; - } + public function getContent(); /** - * Control access to class variables. + * Returns the interne type of the file. The returned value must + * never be empty. Implementations should return + * application/octet-stream when a correct mime type cannot be + * determined. + * + * @return A string containing the internet media type of the file. + * + * @see FeedResultEnclosure::DEFAULT_MIME_TYPE */ - public function __get($name) { - return $this->$name; - } + public function getMimeType(); } /** Index: plugins/FeedsCSVParser.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsCSVParser.inc,v retrieving revision 1.2 diff -u -p -r1.2 FeedsCSVParser.inc --- plugins/FeedsCSVParser.inc 20 Oct 2009 20:59:04 -0000 1.2 +++ plugins/FeedsCSVParser.inc 4 Dec 2009 00:20:08 -0000 @@ -12,13 +12,7 @@ class FeedsCSVParser extends FeedsParser public function parse(FeedsFetcherResult $fetcherResult, FeedsSource $source) { feeds_include_library('ParserCSV.inc', 'ParserCSV'); - if ($fetcherResult->type == 'text/filepath') { - $iterator = new ParserCSVIterator(realpath($fetcherResult->value)); - } - // @todo: write string buffer iterator. - else { - throw new Exception(t('You must use CSV Parser with File Fetcher.')); - } + $iterator = new ParserCSVIterator(realpath($fetcherResult->getFile())); // Parse. $source_config = $source->getConfigFor($this); Index: plugins/FeedsDataProcessor.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsDataProcessor.inc,v retrieving revision 1.5 diff -u -p -r1.5 FeedsDataProcessor.inc --- plugins/FeedsDataProcessor.inc 3 Dec 2009 21:27:40 -0000 1.5 +++ plugins/FeedsDataProcessor.inc 4 Dec 2009 00:20:08 -0000 @@ -19,7 +19,7 @@ class FeedsDataProcessor extends FeedsPr // Count number of created and updated nodes. $inserted = $updated = 0; - foreach ($parserResult->value['items'] as $item) { + foreach ($parserResult->getItems() as $item) { if (!($id = $this->existingItemId($item, $source)) || $this->config['update_existing']) { // Map item to a data record, feed_nid and timestamp are mandatory. $data = array(); Index: plugins/FeedsFeedNodeProcessor.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsFeedNodeProcessor.inc,v retrieving revision 1.4 diff -u -p -r1.4 FeedsFeedNodeProcessor.inc --- plugins/FeedsFeedNodeProcessor.inc 18 Nov 2009 16:53:48 -0000 1.4 +++ plugins/FeedsFeedNodeProcessor.inc 4 Dec 2009 00:20:08 -0000 @@ -20,7 +20,7 @@ class FeedsFeedNodeProcessor extends Fee // Count number of created and updated nodes. $created = $updated = 0; - foreach ($parserResult->value['items'] as $item) { + foreach ($parserResult->getItems() as $item) { // If the target item does not exist OR if update_existing is enabled, // map and save. Index: plugins/FeedsFetcher.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsFetcher.inc,v retrieving revision 1.1 diff -u -p -r1.1 FeedsFetcher.inc --- plugins/FeedsFetcher.inc 20 Oct 2009 21:03:08 -0000 1.1 +++ plugins/FeedsFetcher.inc 4 Dec 2009 00:20:08 -0000 @@ -4,12 +4,54 @@ /** * Defines the object a Fetcher returns on fetch(). */ -class FeedsFetcherResult extends FeedsResult { - // Define valid types. - // @todo: does text/filepath make sense? - // @todo: If convenient, we could expand on this concept and build content - // type negotiation between Fetchers and Parsers. - protected $valid_types = array('text/filepath', 'text/xml'); +class FeedsFetcherResult implements FeedsFileInterface { + + protected $file; + protected $content; + protected $mime_type; + + /** + * Constructor. + * + * @param $file + * Path to the file of the FeedsFetcherResult. + * @param $content + * Full content of the file. + * @param $mime_type + * Mime type of the file. + */ + public function __construct($file, $content, $mime_type) { + $this->file = $file; + $this->content = $content; + $this->mime_type = $mime_type; + } + + /** + * Implement FeedsFetcherResult::getContent(). + * + * @see FeedsFetcherResult::getContent(). + */ + public function getContent() { + return $this->content; + } + + /** + * Implement FeedsFetcherResult::getFile(). + * + * @see FeedsFetcherResult::getFile(). + */ + public function getFile() { + return $this->file; + } + + /** + * Implement FeedsFetcherResult::getMimeType(). + * + * @see FeedsFetcherResult::getMimeType(). + */ + public function getMimeType() { + return $this->mime_type; + } } /** Index: plugins/FeedsFileFetcher.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsFileFetcher.inc,v retrieving revision 1.2 diff -u -p -r1.2 FeedsFileFetcher.inc --- plugins/FeedsFileFetcher.inc 20 Oct 2009 20:59:04 -0000 1.2 +++ plugins/FeedsFileFetcher.inc 4 Dec 2009 00:20:08 -0000 @@ -7,6 +7,27 @@ */ /** + * The result a FeedsHTTPFetcher object returns on fetch(). + */ +class FeedsFileFetcherResult extends FeedsFetcherResult { + + public function __construct($file) { + $this->file = $file; + } + + public function getContent() { + return file_get_contents($this->file); + } + + public function getMimeType() { + if(!isset($this->mime_type)) { + $this->mime_type = file_get_mimetype($this->getFile()); + } + return $this->mime_type; + } +} + +/** * Fetches data via HTTP. */ class FeedsFileFetcher extends FeedsFetcher { @@ -18,7 +39,7 @@ class FeedsFileFetcher extends FeedsFetc $source_config = $source->getConfigFor($this); // Just return path to file, contents can be read easily with // file_get_contents($file_path); - return new FeedsFetcherResult($source_config['source'], 'text/filepath'); + return new FeedsFileFetcherResult($source_config['source']); } /** @@ -61,4 +82,4 @@ class FeedsFileFetcher extends FeedsFetc form_set_error('feeds][source', t('File needs to point to a file in your Drupal file system path.')); } } -} \ No newline at end of file +} Index: plugins/FeedsHTTPFetcher.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsHTTPFetcher.inc,v retrieving revision 1.5 diff -u -p -r1.5 FeedsHTTPFetcher.inc --- plugins/FeedsHTTPFetcher.inc 17 Nov 2009 20:14:30 -0000 1.5 +++ plugins/FeedsHTTPFetcher.inc 4 Dec 2009 00:20:08 -0000 @@ -7,6 +7,29 @@ */ /** + * The result a FeedsHTTPFetcher object returns on fetch(). + */ +class FeedsHTTPFetcherResult extends FeedsFetcherResult { + + public function __construct($content, $mime_type = 'application/octet-stream') { + $this->content = $content; + $this->mime_type = $mime_type; + } + + public function getFile() { + if(!isset($this->file)) { + //@todo get extension from mime_type + $dest = file_destination(file_directory_temp() . '/' . get_class($this). '.tmp', FILE_EXISTS_RENAME); + $file = file_save_data($this->content, $dest); + if($file === 0) { + throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest))); + } + } + return $this->file; + } +} + +/** * Fetches data via HTTP. */ class FeedsHTTPFetcher extends FeedsFetcher { @@ -34,7 +57,7 @@ class FeedsHTTPFetcher extends FeedsFetc if ($result->code != 200) { throw new Exception(t('Download of @url failed with code !code.', array('@url' => $url, '!code' => $result->code))); } - return new FeedsFetcherResult($result->data, 'text/xml'); + return new FeedsHTTPFetcherResult($result->data, 'text/xml'); } /** @@ -86,4 +109,3 @@ class FeedsHTTPFetcher extends FeedsFetc return $form; } } - Index: plugins/FeedsNodeProcessor.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsNodeProcessor.inc,v retrieving revision 1.17 diff -u -p -r1.17 FeedsNodeProcessor.inc --- plugins/FeedsNodeProcessor.inc 3 Dec 2009 20:55:05 -0000 1.17 +++ plugins/FeedsNodeProcessor.inc 4 Dec 2009 00:20:08 -0000 @@ -19,7 +19,7 @@ class FeedsNodeProcessor extends FeedsPr // Count number of created and updated nodes. $created = $updated = 0; - foreach ($parserResult->value['items'] as $item) { + foreach ($parserResult->getItems() as $item) { // Create/update if item does not exist or update existing is enabled. if (!($nid = $this->existingItemId($item, $source)) || $this->config['update_existing']) { Index: plugins/FeedsOPMLParser.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsOPMLParser.inc,v retrieving revision 1.2 diff -u -p -r1.2 FeedsOPMLParser.inc --- plugins/FeedsOPMLParser.inc 2 Nov 2009 20:22:03 -0000 1.2 +++ plugins/FeedsOPMLParser.inc 4 Dec 2009 00:20:08 -0000 @@ -15,12 +15,7 @@ class FeedsOPMLParser extends FeedsParse * Parses a raw string and returns a Feed object from it. */ public function parse(FeedsFetcherResult $fetcherResult, FeedsSource $source) { - if ($fetcherResult->type == 'text/filepath') { - $string = file_get_contents($fetcherResult->value); - } - else { - $string = $fetcherResult->value; - } + $string = $fetcherResult->getContent(); feeds_include_library('opml_parser.inc', 'opml_parser'); return new FeedsParserResult(opml_parser_parse($string), 'syndication'); } Index: plugins/FeedsParser.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsParser.inc,v retrieving revision 1.1 diff -u -p -r1.1 FeedsParser.inc --- plugins/FeedsParser.inc 20 Oct 2009 21:03:08 -0000 1.1 +++ plugins/FeedsParser.inc 4 Dec 2009 00:20:08 -0000 @@ -1,21 +1,79 @@ {$p} = isset($value[$p]) ? $value[$p] : ''; + } + $this->items = isset($value['items']) ? $value['items'] : array(); } + + /** + * Return the feed's title. + */ + public function getTitle() { + return $this->title; + } + + /** + * Return the feed's description. + */ + public function getDescription() { + return $this->description; + } + + /** + * Return the feed's link property (can be different from the source URL). + */ + public function getLink() { + return $this->link; + } + + /** + * Return the feed's items. + */ + public function getItems() { + return $this->items; + } +} + +/** + * Minimalist interface for enclosures (ie. a file attached to a + * FeedsResult item) + */ +interface FeedsEnclosureInterface extends FeedsFileInterface { + + /** + * Returns the URL for the enclosure. Any valid URL is an acceptable result. + * + * @return + * A string that is a valid URL (RFC 1738) for the enclosure. + */ + public function getUrl(); + + /** + * Returns the enclosure description. + * + * @return + * A string that is the description of the enclosure. + */ + public function getDescription(); } /** Index: plugins/FeedsSimplePieParser.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsSimplePieParser.inc,v retrieving revision 1.4 diff -u -p -r1.4 FeedsSimplePieParser.inc --- plugins/FeedsSimplePieParser.inc 2 Nov 2009 20:05:10 -0000 1.4 +++ plugins/FeedsSimplePieParser.inc 4 Dec 2009 00:20:08 -0000 @@ -12,12 +12,8 @@ class FeedsSimplePieParser extends Feeds * Parses a raw string and returns a Feed object from it. */ public function parse(FeedsFetcherResult $fetcherResult, FeedsSource $source) { - if ($fetcherResult->type == 'text/filepath') { - $string = file_get_contents($fetcherResult->value); - } - else { - $string = $fetcherResult->value; - } + $string = $fetcherResult->getContent(); + feeds_include_library('simplepie.inc', 'simplepie'); // Initialize SimplePie. @@ -59,16 +55,8 @@ class FeedsSimplePieParser extends Feeds $item['author_link'] = $author->link; $item['author_email'] = $author->email; // Enclosures - $enclosures = $simplepie_item->get_enclosures(); - if (is_array($enclosures)) { - foreach ($enclosures as $enclosure) { - $mime = $enclosure->get_real_type(); - if ($mime != '') { - list($type, $subtype) = split('/', $mime); - $item['enclosures'][$type][$subtype][] = $enclosure; - } - } - } + //Store the raw enclosures in the item, let getSourceElement handled it later (if needed) + $item['enclosures'] = $simplepie_item->get_enclosures(); // Location $latitude = $simplepie_item->get_latitude(); $longitude = $simplepie_item->get_longitude(); @@ -99,8 +87,14 @@ class FeedsSimplePieParser extends Feeds $feed['items'][] = $item; } // Release parser. + if (version_compare(PHP_VERSION, '5.3.0', '<')) { + // Workaround PHP Bug #33595, + // see http://simplepie.org/wiki/faq/i_m_getting_memory_leaks + $parser->__destruct(); + } unset($parser); - return new FeedsParserResult($feed, 'syndication'); + unset($item); + return new FeedsParserResult($feed); } /** @@ -164,6 +158,31 @@ class FeedsSimplePieParser extends Feeds } /** + * Get an element identified by $element_key of the given item. + * The element key corresponds to the values in the array returned by + * FeedsParser::getMappingSources(). + */ + public function getSourceElement($item, $element_key) { + switch($element_key) { + case 'enclosures': + $enclosures = isset($item[$element_key]) ? $item[$element_key] : FALSE; + $result = array(); + if (is_array($enclosures)) { + foreach ($enclosures as $enclosure) { + $mime = $enclosure->get_real_type(); + if ($mime != '') { + list($type, $subtype) = split('/', $mime); + $result[$type][$subtype][] = new FeedsSimplePieEnclosure($enclosure); + } + } + } + return $result; + default: + return parent::getSourceElement($item, $element_key); + } + } + + /** * Returns cache directory. Creates it if it doesn't exist. */ protected function cacheDirectory() { @@ -181,4 +200,57 @@ class FeedsSimplePieParser extends Feeds $words = array_slice($words, 0, 3); return implode(' ', $words); } +} + +/** + * Adapter to present a SimplePie_Enclosure as a FeedsEnclosureInterface. + * + * @see FeedResultEnclosure + * @see SimplePie_Enclosure + */ +class FeedsSimplePieEnclosure implements FeedsEnclosureInterface { + private $simplepie_enclosure; + + private $file; + + function __construct(SimplePie_Enclosure $enclosure) { + $this->simplepie_enclosure = $enclosure; + } + + + public function getUrl() { + return $this->simplepie_enclosure->get_link(); + } + + + public function getDescription() { + return $this->simplepie_enclosure->get_description(); + } + + + public function getMimeType() { + $type = $this->simplepie_enclosure->get_real_type(); + return !empty($type) ? $type : FeedsFileInterface::DEFAULT_MIME_TYPE; + } + + public function getContent() { + feeds_include_library('http_request.inc', 'http_request'); + $result = http_request_get($url); + if ($result->code != 200) { + throw new Exception(t('Download of @url failed with code !code.', array('@url' => $url, '!code' => $result->code))); + } + return file_get_contents($result->data); + } + + public function getFile() { + if(!isset($this->file)) { + //@todo get extension from mime_type + $dest = file_destination(file_directory_temp() . '/' . get_class($this). '.tmp', FILE_EXISTS_RENAME); + $file = file_save_data($this->getContent(), $dest); + if($file === 0) { + throw new Exception(t('Cannot write content to %dest', array('%dest' => $dest))); + } + } + return $this->file; + } } \ No newline at end of file Index: plugins/FeedsSyndicationParser.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsSyndicationParser.inc,v retrieving revision 1.8 diff -u -p -r1.8 FeedsSyndicationParser.inc --- plugins/FeedsSyndicationParser.inc 2 Nov 2009 19:58:37 -0000 1.8 +++ plugins/FeedsSyndicationParser.inc 4 Dec 2009 00:20:08 -0000 @@ -12,14 +12,9 @@ class FeedsSyndicationParser extends Fee * Parses a raw string and returns a Feed object from it. */ public function parse(FeedsFetcherResult $fetcherResult, FeedsSource $source) { - if ($fetcherResult->type == 'text/filepath') { - $string = file_get_contents($fetcherResult->value); - } - else { - $string = $fetcherResult->value; - } + $string = $fetcherResult->getContent(); feeds_include_library('common_syndication_parser.inc', 'common_syndication_parser'); - return new FeedsParserResult(common_syndication_parser_parse($string), 'syndication'); + return new FeedsParserResult(common_syndication_parser_parse($string)); } /** Index: plugins/FeedsTermProcessor.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsTermProcessor.inc,v retrieving revision 1.2 diff -u -p -r1.2 FeedsTermProcessor.inc --- plugins/FeedsTermProcessor.inc 2 Nov 2009 20:48:52 -0000 1.2 +++ plugins/FeedsTermProcessor.inc 4 Dec 2009 00:20:08 -0000 @@ -23,7 +23,7 @@ class FeedsTermProcessor extends FeedsPr // Count number of created and updated nodes. $created = $updated = $no_name = 0; - foreach ($parserResult->value['items'] as $item) { + foreach ($parserResult->getItems() as $item) { if (!($tid = $this->existingItemId($item, $source)) || $this->config['update_existing']) { Index: plugins/FeedsUserProcessor.inc =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/feeds/plugins/FeedsUserProcessor.inc,v retrieving revision 1.2 diff -u -p -r1.2 FeedsUserProcessor.inc --- plugins/FeedsUserProcessor.inc 2 Nov 2009 20:26:57 -0000 1.2 +++ plugins/FeedsUserProcessor.inc 4 Dec 2009 00:20:08 -0000 @@ -19,7 +19,7 @@ class FeedsUserProcessor extends FeedsPr // Count number of created and updated nodes. $created = $updated = $failed = 0; - foreach ($parserResult->value['items'] as $item) { + foreach ($parserResult->getItems() as $item) { if (!($uid = $this->existingItemId($item, $source)) || $this->config['update_existing']) {