Created
March 16, 2010 20:32
-
-
Save mcantelon/334466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
# | |
# Set CCK imagefields from node references | |
# by Mike Cantelon | |
# | |
# run this with Drush... example if named migration_script.php: | |
# drush -r /var/www/drupalroot -u "Mike Cantelon" php-script migration_script | |
# | |
function populate_thumbnail_with_imagenode($content_node_nid, $imagenode_nid) { | |
print 'Node containing reference:'. $content_node_nid .' / Image node:'. $imagenode_nid ."... "; | |
$image_node = node_load($imagenode_nid); | |
if ($image_path = $image_node->images['_original']) { | |
// make copy of image file | |
$command = 'cp "/var/www/drupalroot/'. $image_path .'" /tmp'; | |
system($command); | |
node_delete($imagenode_nid); | |
// attach file to node | |
$filename = basename($image_path); | |
$node = node_load($content_node_nid); | |
$file_temp = file_get_contents('/tmp/'. $filename); | |
if (strlen($file_temp)) { | |
$file_temp = file_save_data($file_temp, file_directory_path() .'/images/thumbnails/'. $filename, FILE_EXISTS_RENAME); | |
if ($file_temp) { | |
$node->field_thumbnail_image = array( | |
array( | |
'fid' => 'upload', | |
'filepath' => $file_temp, | |
'filesize' => filesize('/tmp/'. $filename), | |
'title' => $image_node->body | |
) | |
); | |
// remove association | |
if ($node->field_thumbnail[0]['nid']) { | |
$node->field_thumbnail = array(array('nid' => 0)); | |
} | |
node_save($node); | |
print "Complete.\n"; | |
sleep(1); | |
} | |
else { | |
print "Failed at node:". $content_node_nid; exit(); | |
} | |
} | |
} | |
else { | |
print "No path to image: skipping...\n"; | |
} | |
} | |
$query = "SELECT nid, field_thumbnail_nid FROM {content_field_thumbnail} WHERE field_thumbnail_nid != 0 limit 500"; | |
$result = db_query($query); | |
if ($result) { | |
while ($node_nid = db_fetch_object($result)) { | |
$node = node_load($node_nid->nid); | |
if (!sizeof($node->field_thumbnail_image)) { | |
if ($node_nid->field_thumbnail_nid) { | |
populate_thumbnail_with_imagenode($node->nid, $node_nid->field_thumbnail_nid); | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment