Skip to main content

Drupal 7 migration: a script for image + image_attach

Inspired by this post:

I thought I'd dig out and contribute a script I wrote for a similar issue I had with a number of old sites.

Specifically - in the old days before imagecache, one way to safely enable your site editors to add pictures to posts was using a combination of the image module with the image attach module. Functionally, most of the sites I've seen it on were doing the same thing as the imagecache, only not as well, and now that D7 is mainstream I can't prolong keeping it going any longer.

The main difference with Laura's post above is trying to deal with the image attachment bit - i.e. getting the images into the fields of the posts to which the original image nodes where being 'attached'.

The other key difference - this script was written for D6, so do this before you do your D7 upgrade. It's making use of the D6 table structure, it's not written using high-level api's! D7's table structure is much more complicated, so trying to do this after the D7 upgrade would be more difficult I suspect.

Also: doing this only with a script would be too general a tool I think. In any case, I simplified the problem by manually creating the new image fields and then hard coding those field names into my script.

Conclusion: feel free to recycle my script below, but make sure you

1. create the new fields first, and then modify my "$convert" array below.
2. try it on a dev copy first. Also, you'll see some commented out code that I uncomment on the first run as a way of checking out what it will do.

Notes: because an image node image can be attached to multiple nodes, i'm copying the original image files, not reusing them in their same location. So when you're done, you can remove the files/image directory where the image module put it's stuff.

// code to convert image module use to imagecache

$convert = array(
  'newsletter_article' => 'field_newsletter_image',
  'page' => 'field_page_image',
  'press' => 'field_press_image',

// you can run this code first to see what sizes were being attached to which node types
/* foreach(node_get_types() as $type => $info) {
  $image_attach = variable_get("image_attach_$type",'');
  if ($image_attach) {
    echo "\n $type";   
    echo db_result(db_query('DESC content_type_'.$type));
} */
$sizes = variable_get('image_sizes','');
// print_r($sizes); die();

foreach($convert as $type => $image_field) {
  //echo $image_field;
  $iff = $image_field.'_fid';
  $config = db_fetch_object(db_query("SELECT * FROM {content_node_field_instance} WHERE field_name = '%s'",$image_field));
  $settings = unserialize($config->widget_settings);
  $dir = file_create_path($settings['file_path']);
  // print_r($config);
  // print_r($settings);
  $status = file_check_directory($dir,(FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS));
  if (!$status) {
    die("Unable to create directory!!!");
    // TODO: for rfc, i don't have directory create permission, need to run initialization as nobody
  // for each image, move the _original one to the new directory, 
  // remove the other ones, and update the db
  $result = db_query("SELECT n.vid,ia.*,i.fid FROM {node} n INNER JOIN {image_attach} ia ON n.nid = ia.nid INNER JOIN {image} i on ia.iid = i.nid AND i.image_size = '_original' WH
ERE n.type = '%s'",$type);
  while ($ia = db_fetch_object($result)) {
    $file = db_fetch_object(db_query("SELECT * FROM files where fid = %d",$ia->fid));
    $name = basename($filepath = $file->filepath);
    // print_r($ia);
    // die("$old_path -> $new_path");
    // get the current fid!
    $good = file_copy($filepath,$settings['file_path'],FILE_EXISTS_REPLACE);
    if (!$good) {
      print_r($file); print_r($ia); die($filepath);
    else {
      db_query("INSERT INTO {files} (uid,filename,filepath,filemime,filesize,status,timestamp) VALUES (%d,'%s','%s','%s',%d,%d,%d)",$file->uid,$name,$filepath,$file->filemime,$fil
      $fid = db_last_insert_id('files','fid');
      if (!db_result(db_query("SELECT vid FROM {content_type_$type} WHERE vid = %d",$ia->vid))) {
        db_query("INSERT INTO {content_type_$type} (nid,vid,$iff) VALUES (%d,%d,%d)",$ia->vid, $ia->nid, $fid);
      else {
        db_query("UPDATE {content_type_$type} SET $iff = %d WHERE vid = %d",$fid, $ia->vid);
    db_query('DELETE FROM {image_attach} WHERE nid = %d AND iid = %d',$ia->nid,$ia->iid);
    // print_r($ia); die("$old_path $new_path");
  // echo $status ? ': good' : ': bad';
// print_r(array_keys($types));

Popular posts from this blog

Me and varnish win against a DDOS attack.

This past month one of my servers experienced her first DDOS - a distributed denial of service attack. A denial of service attack (or DOS) just means an attempt to shut down an internet-based service by overwhelming it with requests. A simple DOS attack is usually relatively easy to deal with using the standard linux firewall called iptables.  The way iptables works is by filtering the traffic based on the incoming request source (i.e., the IP of the attacking machine). The attacking machine's IP can be added into your custom ip tables 'blacklist' to block all traffic from it, and it's quite scalable so the only thing that can be overwhelmed is your actual internet connection, which is hard to do.

The reason a distributed DOS is harder is because the attack is distributed from multiple machines. I first noticed an increase in my traffic about a day after it had started - it wasn't slowing down my machine, but it did show up as a spike in traffic. I quickly saw that…

CiviCRM's invoice_id field and why you should love the hash

I've been banging my head against a distracted cabal of developers who seem to think that a particular CiviCRM core design, which I'm invested in via my contributed code, is bad, and that it's okay to break it.

This post is my attempt to explain why it was a good idea in the first place.

The design in question is the use of a hash function to populate a field called 'invoice_id' in CiviCRM's contribution table. The complaint was that this string is illegible to humans, and not necessary. So a few years ago some code was added to core, that ignores the current value of invoice_id and will overwrite it, when a human-readable invoice is generated.

The complaint about human-readability of course is valid, and the label on the field is misleading, but the solution is terrible for several reasons I've already written about.

In this post, I'd like to explain why the use of the hash value in the invoice_id field is actually a brilliant idea and should be embrac…

Upgrading to Drupal 8 with Varnish, Time to Upgrade your Mental Model as well

I've been using Varnish with my Drupal sites for quite a long while (as a replacement to the Drupal anonymous page cache). I've just started using Drupal 8 and naturally want to use Varnish for those sites as well. If you've been using Varnish with Drupal in the past, you've already wrapped your head around the complexities of front-end anonymous page caching, presumably, and you know that the varnish module was responsible for translating/passing the Drupal page cache-clear requests to Varnish - explicitly from the performance page, but also as a side effect of editing nodes, etc.

But if you've been paying attention to Drupal 8, you'll know that it's much smarter about cache clearing. Rather than relying on explicit calls to clear specific or all cached pages, it uses 'cache tags' which require another layer of abstraction in your brain to understand.

Specifically, the previous mechanism in Drupal 7 and earlier was by design 'conservative' …