--- mappers/feedapi_mapper_filefield.inc +++ mappers/feedapi_mapper_filefield.inc @@ -0,0 +1,105 @@ +type); + $fields = array(); + if (@count($info['fields'])) { + foreach ($info['fields'] as $field_name => $field) { + if (in_array($field['type'], array('filefield')) || in_array($field['type'], array('image'))) { + // Images are supported because the imagefield module smartly reuses the filefield module for cck storage. + $fields[$field_name] = isset($field['widget']['label']) ? $field['widget']['label'] : $field_name; + } + } + } + if (count($fields)) { + return $fields; + } + return FALSE; + break; + + case 'map': + /** + * Supported/expected input cases follow. Please feel encouraged to submit patches with additional support for common parsers/feed sources/combinations therein. + * General + * 1) $feed_element is just a string, ie http://mysite.com/myfile.doc. + * 2) $feed_element is an array of strings, all of which will added to the cck field if there's sufficient room. + * Parsed Enclosure as returned by SimplePie or similar. + * 1) $feed_element is an associative array with a 'link' field. + * 2) $feed_element is an array of such associative arrays, all of which will added to the cck field if there's sufficient room. + */ + $items = isset($node->{$field_name}) ? $node->{$field_name} : array(); + if (!is_array($feed_element)) { + // Make a string into an array and then pull it out 8 lines later. Allows non-redundant code. + $feed_element = array($feed_element); + } + $onval = 0; + foreach ($feed_element as $element) { + $href = ''; + if (is_array($element) && valid_url($element['link'])) { + $href = html_entity_decode($element['link']); + } + elseif (is_string($element) && valid_url($element)) { + $href = html_entity_decode($element); + } + else { + continue; + } + $filepath = file_destination(file_directory_temp() . '/' . basename($href), FILE_EXISTS_RENAME); + + $lfile = fopen($filepath, "w"); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $href); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'); + curl_setopt($ch, CURLOPT_FILE, $lfile); + $ok = curl_exec($ch); + $info = curl_getinfo($ch); + + if ($ok === false || $info['http_code'] != 200) { + watchdog("feedapi_mapper", "No cURL data returned for @url [@code]", array('@url' => $href, '@code' => $info['http_code'])); + } + fclose($lfile); + curl_close($ch); + + if ($ok) { + $ctype = content_types($node->type); + if ($ctype['fields'][$field_name]) { + $target_dir = filefield_widget_file_path($ctype['fields'][$field_name], user_load($node->uid)); + if (field_file_check_directory($target_dir, FILE_CREATE_DIRECTORY)) { + $info = field_file_save_file($filepath, array(), $target_dir); + if ($info) { + $items[$onval] = $info; + $onval++; + } + } + else { + watchdog("feedapi_mapper", "Could not save file @file", array('@file' => $filepath)); + } + } + else { + watchdog("feedapi_mapper", "Could not save file @file", array('@file' => $filepath)); + } + // Clean up temporary file + unlink($filepath); + } + else { + watchdog("feedapi_mapper", "Could not fetch @url", array('@url' => $href)); + } + } + + $node->$field_name = $items; + return $node; + break; + } +} --- tests/feedapi_mapper_filefield.test +++ tests/feedapi_mapper_filefield.test @@ -0,0 +1,95 @@ + t('FeedAPI Mapper FileField'), + 'description' => t('Test FeedAPI Mapper support for File Fields (Requires internet connection)'), + 'group' => t('FeedAPI Mapper'), + ); + } + + /** + * SimpleTest core method: code run before each and every test method. + */ + function setUp() { + // Always call the setUp() function from the parent class. + @parent::setUp( + 'feedapi', + 'feedapi_node', + 'parser_simplepie', + 'parser_common_syndication', + 'feedapi_mapper', + 'content', + 'filefield' + ); + + // Create users. + $this->admin_user = $this->drupalCreateUser( + array( + 'administer content types', + 'administer feedapi', + 'administer nodes', + 'administer site configuration', + 'advanced feedapi options', + 'create feed content', + ) + ); + } + + /** + * SimpleTest main test method: runs actual test with significant help from parent class methods. + */ + function testFileFieldMapping() { + $options = array( + 'fields' => array( + 'alpha' => 'filefield', + 'beta' => 'filefield', + ), + 'filename' => 'flickr.xml', // This feed references images online; if they move the test will break. + 'mappers' => array('node'), + 'mapping' => array( + serialize(array('title')) => serialize(array('node', 'title')), + serialize(array('description')) => serialize(array('node', 'body')), + serialize(array('options', 'enclosures', 'image', 'jpeg')) => serialize(array('filefield', 'field_alpha')), + ), + ); + + $this->drupalLogin($this->admin_user); + $this->enableParsers(TRUE, FALSE); + $feed_url = $this->setupFeed($options); + + // Check one of the nodes + $this->drupalGet('admin/content/node/overview'); + $this->clickLink('Tubing is awesome'); + + $node = node_load(array('title' => 'Tubing is awesome'), NULL, TRUE); + $this->assertTrue(isset($node->field_alpha[0]), t("File created, #1")); + $this->assertTrue(file_exists($node->field_alpha[0]['filepath']), t('Image file exists, #1')); + + $this->assertText('Tubing is awesome'); + + // Check one of the nodes + $this->drupalGet('admin/content/node/overview'); + $this->clickLink('Jeff vs Tom'); + + $node = node_load(array('title' => 'Jeff vs Tom'), NULL, TRUE); + $this->assertTrue(isset($node->field_alpha[0]), t("File created, #2")); + $this->assertTrue(file_exists($node->field_alpha[0]['filepath']), t('Image file exists, #2')); + + $this->assertText('Jeff vs Tom'); + + $this->deleteFeed($feed_url); + $this->drupalLogout(); + } +} --- tests/samples/flickr.xml +++ tests/samples/flickr.xml @@ -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/ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +