Record and keep old links working
Once the node is saved you know where the content has gone. You know where it came from. Now it's time to record what you have done. Essential for checking that everything really did get migrated, essential for checking if anything got duplicated, essential for redirecting people from the old links, that no doubt weren't nice clean descriptive urls, to the new location of the content.
The observant will have noticed that when making the original node object the source id and table were stored in the node object. When node_save is called the nid is added to the node object. This makes recording this easy.
The files saved also want to be recorded. Redirecting from the original location to the new Drupal one. Here I think I may have made life more difficult for myself than I needed too. Checking the filefield takes the &$file by reference so adds the fid. Anyway there or not the code works.
if ($node->nid == NULL) {
print '<p>Error node_saved with no nid.</p>';
print_r ($node);
die();
}
if ($node->xc_xtable == NULL && $node->xc_xid == NULL) {
print '<p>Node source not recorded</p>';
print_r ($node);
return;
}
db_query("INSERT INTO xoops_content (xtable, xid, nid) VALUES ('%s', %d, %d)", $node->xc_xtable, $node->xc_xid, $node->nid);
if (is_array($node->field_picture)) {
foreach ($node->field_picture as $file) {
if (array_key_exists('debug', $_GET)) { print_r($file); }
_x_node_record_file($node->nid, $file);
}
}
if (is_array($node->field_attachment)) {
foreach ($node->field_attachment as $file) {
_x_node_record_file($node->nid, $file);
}
}
}
function _x_node_record_file($node, $file) {
$fid = db_result(db_query("SELECT fid FROM files WHERE nid = %d AND filename = '%s'", $node->nid, $file['filename']));
if ($fid) {
db_result(db_query("INSERT INTO xoops_files (file, fid) VALUES ('%s', %d)", $file['xoops_filepath'], $fid));
}
else {
print "Node $node->nid: Failed to find fid for ". $file['filename'] ." previously ". $file['xoops_filepath'] ."\n";
}
}