How To Migrate from Image.module to ImageField
I just found another really helpful utility script that saved me tons of time and effort. I took over a project that another developer built using the Image module. The Image module is pretty nice but it just does not compare to the Filefield and ImageField combination of modules, along with ImageCache, etc.
So basically what I had was a bunch of blog posts that had image nodes attached to them with image_attach module, part of Image.
I stumbled upon the following little script on Drupal.org at http://drupal.org/node/201983#comment-4483072.
I'm going to post it here in case it ever comes up again.
<?php
/**
* @file
* Migrate all image.module nodes to imagefields in Drupal 6. Any image nodes
* that participate in image_attach will be properly attached the imagefield.
* The script does migrate video_image.module images.
*
* PREREQUISITES
* -------------
* - You must create a single imagefield field to which all your images will be migrated. You
* should create a new content type for that.
* - The imagefield's Image path should be identical to the image.module's configured path (i.e. 'images' by default)
* - The imagefield should be configured for 'multiple values'.
* - If you use image_attach, you should also add this imagefield field to each content type that
* is image_attach enabled.
*
* USAGE
* -----
* - Backup your Drupal database. Really.
* - Edit the 'Configuration' section below.
* - Place this script in the root of your Drupal site.
* - Run this script by requesting <a href="http://<yoursite>/imagefield_migrate.php" title="http://<yoursite>/imagefield_migrate.php" rel="nofollow">http://<yoursite>/imagefield_migrate.php</a> in your browser.
* - Remove this script from your site to prevent accidental re-run.
* - Disable and uninstall image and image_attach modules.
*
* KNOWN ISSUES
* ------------
* - Rename files that have a '+' in them in the files table and also rename in filesystem
*
* AUTHORS
* -------
* spydor (see <a href="http://drupal.org/node/201983#comment-828698" title="http://drupal.org/node/201983#comment-828698" rel="nofollow">http://drupal.org/node/201983#comment-828698</a>)
* Moshe Weitzman (<a href="http://drupal.org/moshe" title="http://drupal.org/moshe" rel="nofollow">http://drupal.org/moshe</a>)
* hobbes_vt
* rkeppner
*/
// ***** CONFIGURATION *******
// Table prefix ... in case you use it - otherwise leave blank
$table_pfx = '';
// The imagefield field that you have already created and configured as per Prerequisites.
$field_name = 'field_image';
// The content type that you have already created as per Prerequisites.
$type_name = 'scoop';
// ***** END CONFIGURATION *******
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Populate the imagefield table for every image node.
$table_content_type = $table_pfx. 'content_type_'. $type_name; // this changed from the original version
$table_content_field = $table_pfx. 'content_'. $field_name;
$fid = $field_name. '_fid';
$list = $field_name. '_list';
$data = $field_name. '_data';
$table_node = $table_pfx. 'node'; // add prefix
$table_files = $table_pfx. 'files'; // add prefix
$table_upload = $table_pfx. 'image'; // add prefix
$sql = "INSERT INTO {$table_content_type} (vid, nid)
SELECT n.vid, n.nid FROM {$table_node} n
INNER JOIN {$table_upload} u ON n.nid = u.nid
INNER JOIN {$table_files} f ON u.fid = f.fid
WHERE n.type = 'image' AND f.filename = '_original'";
if (db_query($sql)) {
echo "- $table_content_type populated.<br />\n";
}
else {
echo "- $table_content_type NOT populated.<br />\n";
}
$sql = "INSERT INTO {$table_content_field} (vid, nid, delta, $fid, $list, $data)
SELECT n.vid, n.nid, 0, f.fid, 1, CONCAT('a:1:{s:11:\"description\";s:', CHAR_LENGTH(n.title), ':\"', n.title, '\";}')
FROM {$table_node} n
INNER JOIN {$table_upload} u ON n.nid = u.nid
INNER JOIN {$table_files} f ON u.fid = f.fid
WHERE n.type = 'image' AND f.filename = '_original'";
if (db_query($sql)) {
echo "- $table_content_field populated.<br />\n";
}
else {
echo "- $table_content_field NOT populated.<br />\n";
}
// Set the needed filename in the files table.
$image_path = file_create_path(variable_get('image_default_path', 'images'));
$length = strlen($image_path)+2;
$sql = "UPDATE {$table_files} SET filename = SUBSTRING(filepath, $length) WHERE filename = '_original'";
if (db_query($sql)) {
echo "- files table updated<br />\n";
}
else {
echo "- files table NOT updated<br />\n";
}
// Change the content type from 'image' to the configured type.
$sql = "UPDATE {$table_node} SET type = '%s' WHERE type = 'image'";
db_query($sql, $type_name);
// Loop over the image_attach records
if (module_exists('image_attach')) {
$table_image_attach = $table_pfx. 'image_attach'; // add prefix
$sql = "SELECT n.nid, n.vid, ia.iid FROM {$table_image_attach} ia INNER JOIN {$table_node} n ON ia.nid=n.nid";
$result = db_query($sql);
$num = 0;
while ($row = db_fetch_object($result)) {
$num++;
// UPDATE the imagefield to point to the attached node, not the standalone node
$sql = "UPDATE $table_content_type SET nid = $row->nid, vid = $row->vid WHERE nid = $row->iid";
if (!db_query($sql)) {
echo "update $table_content_type failed for $row->iid<br />\n";
}
$sql = "UPDATE $table_content_field SET nid = $row->nid, vid = $row->vid WHERE nid = $row->iid";
if (db_query($sql)) {
// Successful update. Now unpublish the standalone node that we just made.
$sql = "UPDATE {$table_node} SET status = 0 WHERE nid = $row->iid";
db_query($sql);
}
else {
echo "update $table_content_field failed for $row->iid<br />\n";
}
}
echo "- $num image_attach relationships were migrated.<br />\n";
}
if (module_exists('video_image')) {
// Loop over the video.module nodes. Migrate video_image thumbnails to imagefield.
$sql = "SELECT nid, vid FROM {$table_node} WHERE type = 'video'";
$result = db_query($sql);
$num = 0;
while ($row = db_fetch_object($result)) {
$num++;
$node = node_load($row->nid);
if ($iid = $node->iid) {
$sql = "UPDATE {$table_content_type} SET nid = $row->nid, vid = $row->vid WHERE nid = $iid";
if (db_query($sql)) {
// Successful update. Now unpublish the standalone node that we just made.
$sql = "UPDATE {$table_node} SET status = 0 WHERE nid = $iid";
db_query($sql);
}
else {
echo "update $table failed for $iid<br />\n";
}
};
}
echo "- $num video.module thumbnails were migrated.<br />\n";
}
// Clear CCK cache.
$table_cache_content = $table_pfx. 'cache_content'; // add prefix
$sql = "DELETE FROM {$table_cache_content}";
db_query($sql);
?>The guys that put it together are fantastic, it even gives step by step usage instructions.