php
/**
* @file
* Migrate all image.module nodes to imagefields in Drupal 6.
*
* 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 'single value'.
*
* 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 http://example.com/imagefield_migrate2.php 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
* Boobaa
*/
// ***** CONFIGURATION *******
// Table prefix ... in case you use it - otherwise leave blank
$table_pfx = 'cs_';
// The content type that you have already created as per Prerequisites.
$type_name = 'imagepage';
// The name of the imagefield you have already created as per Prerequisites.
$field_imagefield = 'field_imagefield';
// ***** END CONFIGURATION *******
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Populate the imagefield table for every image node.
$table_node = $table_pfx. 'node'; // add prefix
$table_files = $table_pfx. 'files'; // add prefix
$table_image = $table_pfx. 'image'; // add prefix
$table_imagepage = $table_pfx .'content_type_'. $type_name; // add prefix
$imagefield_data = 'a:3:{s:11:"description";s:0:"";s:3:"alt";s:0:"";s:5:"title";s:0:"";}';
echo $sql = "SELECT fid, filepath FROM {$table_files} WHERE filename = '_original'\n";
$result = db_query($sql);
while ($file = db_fetch_object($result)) {
db_query("UPDATE {$table_files} SET filename = '%s' WHERE fid = %d", basename($file->filepath), $file->fid);
}
echo $sql = "SELECT n.nid, v.vid vid, fid FROM {$table_node} n INNER JOIN {$table_node}_revisions v ON v.nid = n.nid INNER JOIN {$table_image} i ON i.nid = n.nid AND i.image_size = '_original' WHERE n.type = 'image'";
$result = db_query($sql);
while ($file = db_fetch_object($result)) {
db_query("INSERT INTO {$table_imagepage} (vid, nid, {$field_imagefield}_fid, {$field_imagefield}_list, {$field_imagefield}_data) VALUES (%d, %d, %d, 1, '%s')", $file->vid, $file->nid, $file->fid, $imagefield_data);
}
echo $sql = "DELETE FROM {$table_files} WHERE filename = 'thumbnail' OR filename = 'preview'\n";
db_query($sql);
echo $sql = "UPDATE {$table_node} SET type = '%s' WHERE type = 'image'\n";
db_query($sql, $type_name);
$sql = "TRUNCATE TABLE {$table_image}";
db_query($sql);
echo "all done.\n";