/imagefield_migrate.php" title="http:///imagefield_migrate.php" 
rel="nofollow">http:///imagefield_migrate.php in your browser.
* - Remove this script from your site to prevent accidental re-run.
* - Disable and uninstall image and image_attach modules.
*
* AUTHORS
* -------
* spydor (see http://drupal.org/node/201983#comment-828698)
* Moshe Weitzman (http://drupal.org/moshe)
* hobbes_vt
* rkeppner
* mdhooge
*/

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

// ***** CONFIGURATION *******

// The new content type to hold the old image.module nodes
$type_name = 'photo';
$type_has_external_field_table = false;
$field_name = 'field_photo';
$field_path = 'photos';

// The content types with image_attach AND a new CCK imagefield
$ia_types = array( 'story', 'date', 'video' );
$ia_has_external_field_table = true;
$ia_field_name = 'field_photo_opt';

// The image.module derivatives to delete from the 'files' table
$derivatives = array( 'thumbnail', 'preview' );

// Table prefix ... in case you use it - otherwise leave blank
$table_pfx = '';

// Set to true to simulate SQL actions without modifying the DB
$simulate = true;

// ***** END CONFIGURATION *******

// Create tables names with optional prefix
$table_content_field = $table_pfx. 'content_'. $field_name;
$table_content_type  = $table_pfx. 'content_type_'. $type_name;
$table_ia_field      = $table_pfx. 'content_'. $ia_field_name;
$table_files         = $table_pfx. 'files';
$table_image_attach  = $table_pfx. 'image_attach';
$table_node          = $table_pfx. 'node';
$table_upload        = $table_pfx. 'image';
$table_users         = $table_pfx. 'users';
$table_cache_content = $table_pfx. 'cache_content';

// Create table field names
$fid    = $field_name. '_fid';
$ia_fid = $ia_field_name. '_fid';

// Utility function to execute SQL
function sql_stuff( $sql ) {
  echo "$sql;\n\n";
  global $simulate;
  if( ! $simulate ) {
    if( ! db_query( $sql )) {
      echo "ERROR!\n";
      exit(1);
    }
  }
}

if( $simulate )
  echo "This is a test - nothing is changed\n\n";

echo "

$table_content_type

\n"; // Insert all image.module nodes into new content type $sql = $type_has_external_field_table ? "INSERT INTO {$table_content_type} (vid, nid) SELECT n.vid, n.nid" : "INSERT INTO {$table_content_type} (vid, nid, $fid) SELECT n.vid, n.nid, f.fid"; $sql .= " 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'"; sql_stuff( $sql ); // Move file IDs into new content field if( $type_has_external_field_table ) { $sql = "INSERT INTO {$table_content_field} (vid, nid, $fid) SELECT n.vid, n.nid, f.fid 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'"; sql_stuff( $sql ); } // Delete image.module nodes (not really needed but useful to ensure nothing was forgotten) $sql = "DELETE u FROM {$table_upload} u INNER JOIN {$table_node} n ON n.nid = u.nid WHERE n.type = 'image'"; sql_stuff( $sql ); // Change the content type from 'image' to the configured type. $sql = "UPDATE {$table_node} SET type = '{$type_name}' WHERE type = 'image'"; sql_stuff( $sql ); echo "

Delete derivatives

\n"; // Delete derivative images from files table $sql = "SELECT filepath FROM {$table_files} WHERE filename IN ('".implode("','",$derivatives)."')"; $result = db_query($sql); while( $row = db_fetch_object($result) ) { if( $simulate ) { if( file_exists( $row->filepath ) && is_writable( $row->filepath )) echo '-'; elseif( ! file_exists( $row->filepath )) echo 'missing'; else echo 'read-only'; } else { $deleted = @unlink( $row->filepath ); echo $deleted? 'del' : 'ERR'; } echo " $row->filepath\n"; } $sql = "DELETE FROM {$table_files} WHERE filename IN ('".implode("','",$derivatives)."')"; sql_stuff( $sql ); echo "

Move files

\n"; // Update filepath & filename fields of the files table $src_path = file_create_path( variable_get('image_default_path', 'images') ); $dst_path = file_create_path( $field_path ); $length = strlen($src_path) + 1; $sql = "SELECT f.fid, f.filepath, u.name FROM {$table_files} f INNER JOIN {$table_users} u ON u.uid = f.uid WHERE f.filename = '_original'"; echo "$sql;\n"; $result = db_query($sql); while( $row = db_fetch_object($result) ) { $filename = substr( $row->filepath, $length ); $filepath = $dst_path .'/'. $row->name .'/'. $filename; // Update database if( ! $simulate ) { $sql = "UPDATE {$table_files} SET filename = '%s', filepath = '%s' WHERE fid = %d"; db_query( $sql, $filename, $filepath, $row->fid ); } // Create target folder $folder = dirname( $filepath ); if( !( file_exists( $folder ) || @mkdir( $folder, 0777, true ))) echo "Cannot create $folder \n"; // Move file if( $simulate ) { if( file_exists( $row->filepath ) && is_writable( $row->filepath )) echo '-'; elseif( ! file_exists( $row->filepath )) echo 'missing'; else echo 'read-only'; } else { $moved = @rename( $row->filepath, $filepath ); echo $moved? 'ok' : 'KO'; } echo " $filepath\n"; } // Loop over the image_attach records // The optional image_field is connected to the same files.fid of the newly created // content type that replaces the old image.module nodes. if (module_exists('image_attach')) { $sql = sprintf( "SELECT n.nid, n.vid, n.type, f.{$fid} FROM {$table_image_attach} ia INNER JOIN {$table_node} n ON ia.nid=n.nid INNER JOIN %s f ON ia.iid=f.nid", $type_has_external_field_table? $table_content_field : $table_content_type ); echo "\n

Image attach

\n$sql;\n\n"; $result = db_query($sql); while( $row = db_fetch_object( $result )) { if( in_array( $row->type, $ia_types )) { // Check if node already exists in the content_type table $table_ia_type = $table_pfx. 'content_type_'. $row->type; $test = db_query( "SELECT nid FROM {$table_ia_type} WHERE nid=%d", $row->nid ); // Update content type $sql = ''; if( db_fetch_object( $test ) === false ) { // New node if( $ia_has_external_field_table ) $sql = sprintf( "INSERT INTO {$table_ia_type} (vid, nid) VALUES (%d, %d)", $row->vid, $row->nid ); else $sql = sprintf( "INSERT INTO {$table_ia_type} (vid, nid, $ia_fid) VALUES (%d, %d, %d)", $row->vid, $row->nid, $row->$fid ); } else { // Existing node if( ! $ia_has_external_field_table ) $sql = sprintf( "UPDATE {$table_ia_type} SET $ia_fid=%d WHERE nid=%d", $row->$fid, $row->nid ); } if( ! empty( $sql )) sql_stuff( $sql ); // Update content field if( $ia_has_external_field_table ) { $sql = sprintf( "INSERT INTO {$table_ia_field} (vid, nid, $ia_fid) VALUES (%d, %d, %d)", $row->vid, $row->nid, $row->$fid ); sql_stuff( $sql ); } } else { printf( "%-7s not handled, node/%d\n", $row->type, $row->nid ); } } // Delete moved node $sql = "DELETE ia FROM {$table_image_attach} ia INNER JOIN {$table_node} n ON ia.nid=n.nid WHERE n.type IN ('".implode("','",$ia_types)."')"; sql_stuff( $sql ); } // Clear CCK cache. echo "

Clear Cache

\n"; $sql = "DELETE FROM {$table_cache_content}"; echo "$sql\n\n"; db_query($sql); ?>