Fix for Fix for Image to imagefield converter

  1. php
  2. /**
  3. * @file
  4. * Migrate all image.module nodes to imagefields in Drupal 6.
  5. *
  6. * PREREQUISITES
  7. * -------------
  8. * - You must create a single imagefield field to which all your images will be migrated. You
  9. *   should create a new content type for that.
  10. * - The imagefield's Image path should be identical to the image.module's configured path (i.e. 'images' by default)
  11. * - The imagefield should be configured for 'single value'.
  12. *
  13. * USAGE
  14. * -----
  15. * - Backup your Drupal database. Really.
  16. * - Edit the 'Configuration' section below.
  17. * - Place this script in the root of your Drupal site.
  18. * - Run this script by requesting http://example.com/imagefield_migrate2.php in your browser.
  19. * - Remove this script from your site to prevent accidental re-run.
  20. * - Disable and uninstall image and image_attach modules.
  21. *
  22. * KNOWN ISSUES
  23. * ------------
  24. * - Rename files that have a '+' in them in the files table and also rename in filesystem
  25. *
  26. * AUTHORS
  27. * -------
  28. * 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>)
  29. * Moshe Weitzman (<a href="http://drupal.org/moshe" title="http://drupal.org/moshe" rel="nofollow">http://drupal.org/moshe</a>)
  30. * hobbes_vt
  31. * rkeppner
  32. * Boobaa
  33. */
  34.  
  35. // ***** CONFIGURATION *******
  36.  
  37. // Table prefix ... in case you use it - otherwise leave blank
  38. $table_pfx = 'cs_';
  39.  
  40. // The content type that you have already created as per Prerequisites.
  41. $type_name = 'imagepage';
  42.  
  43. // The name of the imagefield you have already created as per Prerequisites.
  44. $field_imagefield = 'field_imagefield';
  45.  
  46. // ***** END CONFIGURATION *******
  47.  
  48. require_once './includes/bootstrap.inc';
  49. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  50.  
  51. // Populate the imagefield table for every image node.
  52. $table_node = $table_pfx. 'node'; // add prefix
  53. $table_files = $table_pfx. 'files'; // add prefix
  54. $table_image = $table_pfx. 'image'; // add prefix
  55. $table_imagepage = $table_pfx .'content_type_'. $type_name; // add prefix
  56. $imagefield_data = 'a:3:{s:11:"description";s:0:"";s:3:"alt";s:0:"";s:5:"title";s:0:"";}';
  57.  
  58. echo $sql = "SELECT fid, filepath FROM {$table_files} WHERE filename = '_original'\n";
  59. $result = db_query($sql);
  60. while ($file = db_fetch_object($result)) {
  61.         db_query("UPDATE {$table_files} SET filename = '%s' WHERE fid = %d", basename($file->filepath), $file->fid);
  62. }
  63. 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'";
  64. $result = db_query($sql);
  65. while ($file = db_fetch_object($result)) {
  66.         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);
  67. }
  68. echo $sql = "DELETE FROM {$table_files} WHERE filename = 'thumbnail' OR filename = 'preview'\n";
  69. db_query($sql);
  70. echo $sql = "UPDATE {$table_node} SET type = '%s' WHERE type = 'image'\n";
  71. db_query($sql, $type_name);
  72. $sql = "TRUNCATE TABLE {$table_image}";
  73. db_query($sql);
  74. echo "all done.\n";

Submit Fix

Any tags you'd like to associate with your code, delimitered by commas (example: Views, CCK, Module, etc).
Select the syntax highlighting mode to use.