Index: mappers/filefield.inc =================================================================== RCS file: mappers/filefield.inc diff -N mappers/filefield.inc --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ mappers/filefield.inc 12 Dec 2009 14:20:35 -0000 @@ -0,0 +1,127 @@ +list subfields (howto provides default value ?) + * @todo Support the data subfileds (or its own subfiedss ?) + */ + +/** + * Implementation of hook_feeds_node_processor_targets_alter() + */ +function filefield_feeds_node_processor_targets_alter($targets, $content_type) { + $info = content_types($content_type); + $fields = array(); + if (isset($info['fields']) && count($info['fields'])) { + foreach ($info['fields'] as $field_name => $field) { + if ($field['type'] == 'filefield') { + $name = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; + $targets[$field_name] = array( + 'name' => $name, + 'callback' => $field['type'].'_feeds_set_target', + 'description' => t('The URL for the CCK !name field of the node.', array('!name' => $name)), + ); + } + } + } +} + +/** + * Implementation of hook_feeds_set_target(). + */ +function filefield_feeds_set_target($node, $field_name, $value) { + $items = isset($node->$field_name) ? $node->$field_name : array(); + $enclosures = array($value); + //$enclosures always contains the enclosures to resolve + while (($enclosure = array_pop($enclosures)) !== NULL) { + if ($file = _enclosure_resolve($enclosure)) { + //$enclosure was resolved to a file + $field = content_fields($field_name, $node->type); + $target_dir = filefield_widget_file_path($field, user_load($node->uid)); + $info = field_file_save_file($file, array(), $target_dir); + if ($info) { + if ($field['list_field']) { + $info['list'] = $field['list_default']; + } + $items[] = $info; + $error = false; + } + } + elseif (is_array($enclosure)) { + //$enclosure is probably an array of enclosures + foreach ($enclosure as $child) { + array_push($enclosures, $child); + } + } + else { + if (is_object($enclosure)) { + $enclosure = t('instance of !class', array('!class' => get_class($enclosure))); + } + watchdog("feeds", "Could not resolve enclosure %enclosure in field %field_name", array('%enclosure' => $enclosure, '%field_name' => $field_name)); + } + } + + $node->$field_name = $items; +} + +/** + * Get the filename for an enclosure URL. If the URL is a valid external URL, + * its content is download as a temporary file. Otherwise, it is considered + * as a filename. + * + * @param $url + * The URL + * @return + * The filename for $url, or 0 on error. + */ +function _enclosure_resolve($enclosure) { + //Get enclosure URL + if (is_object($enclosure)) { + if (method_exists($enclosure, 'get_link')) { + //SimplePie_Enclosure style + $url = $enclosure->get_link(); + } + elseif (method_exists($enclosure, 'getUrl')) { + //Generalized enclusore interface, as in inital patch in http://drupal.org/node/641522 + $url = $enclosure->getUrl(); + } + elseif (isset($enclosure->link)) { + //Fallback to a link property + $url = $enclosure->link; + } + } + elseif (is_array($enclosure) && isset($enclosure['link'])) { + $url = $enclosure['link']; + } + elseif (is_string($enclosure)) { + $url = $enclosure; + } + else { + return 0; + } + //Resolve URL + if (valid_url($url, true)) { + //this enclosure is an absolute URL, download it. + $filepath = file_destination(file_directory_temp().'/'.strtolower(get_class($this)).'/'.basename($url), FILE_EXISTS_RENAME); + if (ini_get('allow_url_fopen')) { + return copy($url, $filepath) ? $filepath : 0; + } + else { + feeds_include_library('http_request.inc', 'http_request'); + $result = http_request_get($url); + if ($result->code != 200) { + watchdog('feeds', 'Download of @url failed with code !code.', array('@url' => $url, '!code' => $result->code)); + return 0; + } + else { + return file_save_data($result->data, $filepath); + } + } + } + else { + return file_exists($url) ? $url : 0; + } +} \ No newline at end of file Index: tests/feeds_mapper_filefield.test =================================================================== RCS file: tests/feeds_mapper_filefield.test diff -N tests/feeds_mapper_filefield.test --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/feeds_mapper_filefield.test 12 Dec 2009 14:20:35 -0000 @@ -0,0 +1,101 @@ + t('Mapper: FileField'), + 'description' => t('Test Feeds Mapper support for FileField CCK fields'), + 'group' => t('Feeds'), + ); + } + + /** + * Set up the + */ + public function setUp() { + // Call parent setup with the required module + parent::setUp( + 'devel', 'feeds', 'feeds_ui', 'ctools', 'content', + 'filefield' + ); + + // Create user and login + $this->drupalLogin($this->drupalCreateUser( + array( + 'administer content types', + 'administer feeds', + 'administer nodes', + 'administer site configuration', + 'access devel information' + ) + )); + } + + /** + * Basic test loading a single entry CSV file. + */ + public function test() { + $static_title = $this->randomName(); + //Create content type + $typename = $this->createContentType(NULL, array( + 'files' => array( + 'type' => 'filefield', + 'settings' => array( + 'multiple' => '1', + 'file_extensions' => 'jpg' + ), + ), + )); + + //Create importer configuration + $this->createFeedConfiguration(); //Create a default importer configuration + $this->setPlugin('syndication', 'FeedsSimplePieParser'); + $this->setSettings('syndication', 'FeedsNodeProcessor', array('content_type' => $typename)); //Processor settings + $this->addMappings('syndication', array( + array( + 'source' => 'title', + 'target' => 'title' + ), + array( + 'source' => 'timestamp', + 'target' => 'created' + ), + array( + 'source' => 'description', + 'target' => 'body' + ), + array( + 'source' => 'enclosures', + 'target' => 'field_files' + ), + )); + + $nid = $this->createFeedNode('syndication', $GLOBALS['base_url'] .'/'. drupal_get_path('module', 'feeds') . '/tests/feeds/flickr.xml'); + $this->assertText('Created 4 '.$typename.' nodes.'); + + $filename = array('3596408735_091162e986_o', '2640019371_6b4fe5966a_o', '3686290986_99b827612b_o', '2640845934_6f1b5eba3c_o'); + for($i = 0; $i < 4; $i++) { + $this->drupalGet('node/'.($i+2).'/edit'); + $this->assertText($filename[$i]); + } + } + + public function selectFieldWidget($fied_name, $field_type) { + if($field_type == 'filefield') { + return 'filefield_widget'; + } else { + return parent::selectFieldWidget($fied_name, $field_type); + } + } +} Index: tests/feeds/flickr.xml =================================================================== RCS file: tests/feeds/flickr.xml diff -N tests/feeds/flickr.xml --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ tests/feeds/flickr.xml 12 Dec 2009 14:20:35 -0000 @@ -0,0 +1,169 @@ + + + + Content from My picks + + + tag:flickr.com,2005:http://www.flickr.com/photos/28242329@N00/sets/72157603970496952 + http://farm1.static.flickr.com/42/86410049_bd6dcdd5f9_s.jpg + Some of my shots I like best in random order. + 2009-07-09T21:48:04Z + Flickr + + + Tubing is awesome + + tag:flickr.com,2005:/photo/3596408735/in/set-72157603970496952 + 2009-07-09T21:48:04Z + 2009-07-09T21:48:04Z + 2009-05-01T00:00:00-08:00 + <p><a href="http://www.flickr.com/people/a-barth/">Alex Barth</a> posted a photo:</p> + +<p><a href="http://www.flickr.com/photos/a-barth/3596408735/" title="Tubing is awesome"><img src="http://farm4.static.flickr.com/3599/3596408735_ce2f0c4824_m.jpg" width="240" height="161" alt="Tubing is awesome" /></a></p> + + +<p>Virginia, 2009</p> + + Alex Barth + http://www.flickr.com/people/a-barth/ + + + + + + + + + + + + + + + + + + Jeff vs Tom + + tag:flickr.com,2005:/photo/2640019371/in/set-72157603970496952 + 2009-07-09T21:45:50Z + 2009-07-09T21:45:50Z + 2008-06-01T00:00:00-08:00 + <p><a href="http://www.flickr.com/people/a-barth/">Alex Barth</a> posted a photo:</p> + +<p><a href="http://www.flickr.com/photos/a-barth/2640019371/" title="Jeff vs Tom"><img src="http://farm4.static.flickr.com/3261/2640019371_495c3f51a2_m.jpg" width="240" height="159" alt="Jeff vs Tom" /></a></p> + + + + + Alex Barth + http://www.flickr.com/people/a-barth/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Attersee 1 + + tag:flickr.com,2005:/photo/3686290986/in/set-72157603970496952 + 2009-07-09T21:42:01Z + 2009-07-09T21:42:01Z + 2009-06-01T00:00:00-08:00 + <p><a href="http://www.flickr.com/people/a-barth/">Alex Barth</a> posted a photo:</p> + +<p><a href="http://www.flickr.com/photos/a-barth/3686290986/" title="Attersee 1"><img src="http://farm4.static.flickr.com/3606/3686290986_334c427e8c_m.jpg" width="240" height="238" alt="Attersee 1" /></a></p> + + +<p>Upper Austria, 2009</p> + + Alex Barth + http://www.flickr.com/people/a-barth/ + + + + + + + + + + + + + + + + + + + + H Street North East + + tag:flickr.com,2005:/photo/2640845934/in/set-72157603970496952 + 2008-09-23T13:26:13Z + 2008-09-23T13:26:13Z + 2008-06-01T00:00:00-08:00 + <p><a href="http://www.flickr.com/people/a-barth/">Alex Barth</a> posted a photo:</p> + +<p><a href="http://www.flickr.com/photos/a-barth/2640845934/" title="H Street North East"><img src="http://farm4.static.flickr.com/3083/2640845934_85c11e5a18_m.jpg" width="240" height="159" alt="H Street North East" /></a></p> + + +<p>Washington DC 2008<br /> +<a href="http://dcist.com/2008/07/07/photo_of_the_day_july_7_2008.php">Photo of the Day July 7 on DCist</a></p> + + Alex Barth + http://www.flickr.com/people/a-barth/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file