Converting News Items

After all of the legacy nodes were imported, I recognized that we were going to want the news items stored in a special type of Drupal node. Here are the steps I follows:

  1. Created a 'News Item' content type
  2. Wrote a PHP/Drush script to find all nodes whose "old_url_value" field contained the string "/news/", and clone those nodes as type 'News Item'. Since the title of each news item contained an embedded date, e.g. "2002 - 09/12 - U.S. National Aerobatic...", I set the creation time of the News Item node to that date, and stripped the date from the title.
  3. Manually edited several titles that had problems: "&" in place of a simple ampersand, leading blanks, etc.

Here's the PHP script:

<?php
//
// DJM, 2012-02-28
//
// PHP script that builds a list of 'Legacy Page' nodes that contain news
// items, and creates equivalent 'News Item' nodes.
//
// For safety's sake, this script does not delete the Legacy Page nodes --
// when the time is right, that can easily be done using Drupal's content
// UI screen.

$result = db_query("SELECT nid FROM node, field_data_field_old_url WHERE
  node.type = 'legacy_page' AND field_old_url_value LIKE '%/news/%' AND
  node.nid = field_data_field_old_url.entity_id;");

foreach ($result as $obj) {

  $leg_node = node_load($obj->nid);

  $news_node = new stdClass();
  $news_node->type = 'news_item';
  node_object_prepare($node);

  $news_node->language = LANGUAGE_NONE;
  $news_node->uid = 1; // Webmaster
  $news_node->body  = $leg_node->body;

  // Expecting node titles in the form: YYYY - MM/DD - ACTUAL TITLE
  // Use that to set the node creation time, and copy the non-date
  // portions of the title to the new node

  $t = $leg_node->title;
  $news_node->title = substr($t, 15);
  $news_node->created = mktime(0, 0, 0,
  substr($t, 7, 2), substr($t, 10, 2), substr($t, 0, 4));

  node_save($news_node);

  print($news_node->nid . ": " . $news_node->title . "\n");

}

?>