Linking Images

Once the content had been written to Drupal nodes, the <img> tag paths were all broken so no pictures were visible. This occurred because the Stellent CMS outputs relative path names (e.g., ../../../images/programs/all_ten-1.gif), and there is no corresponding directory in the Drupal directory tree.

I cured this in two steps: 1) Linked all of the image files from both /var/www/old-site/public and /var/www/old-site/members to the newly created /var/www/new-site/files/images using find and cpio -pdmvl. 2) Wrote a Drush script to do a pattern search-and-replace, converting relative path names to absolute paths.

 

<?php

// Script to mass-modify the src attribute of <img> tags
// DJM, 2012-03-20

$result = db_query("SELECT nid FROM node WHERE type IN ('legacy_page', 'news_item');");

foreach ($result as $rec) {

  print $rec->nid . "\n";  // Print the node ID

  $node = node_load($rec->nid); // Grab the node

  // Replace relative paths with absolute

  // The first argument is the regular expression to match. It says,
  // in essence, match any string starting with "src=" plus any number
  // of "../" strings plus the string "images/".
  // The second argument is the replacement string.
  // The third argument is the string on which we're operating.

  $newbod = preg_replace('/src="(\.\.\/){1,}images\//',
     'src="/files/images/',
     $node->body[und][0][value]);

  // Save the result back into the node object
  $node->body[und][0][value] = $newbod;

  node_save($node); // Save the modified node

}

?>