Hello everyone.
I am attempting to parse NITF articles and then create a node in Drupal with the parsed information plus the corresponding images. I've got the bare minimum down when it comes to parsing the XML files and creating nodes from them. However, I can't seem to figure out how to add images to the nodes.
Ideally, I'm trying to create a script to do the following: (1) automatically traverse a directory containing .XML files and a sub-directory with the articles corresponding images; (2) parse the .XML files and match the images to the articles; (3) and finally generate a node containing the parsed article information and also the images arbitrarily placed within the newly created node.
I will also want to figure out how to categorize each node based on meta tags in the articles, but that will come after I get images to show up.
Right now, when I run the script below, it imports the headline, body, and half-asses the image. By that, I mean after I run the script and view my site you see the new node with a headline and body, but then under the body all you see is Image:. No image, just the title of the field. However, if you go to edit the node, you will see a thumbnail of the image.
I guess I should also note that I'm importing these articles into a CCK type called "article." The image field is a custom field called "field_image." The only CCK extensions I have enabled are Content, ImageField, FileField, and Text. To make the image field, I made the field type "file" and the
widget "image."
Any help is appreciated.
Thanks.
error_reporting(E_ALL);
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// Includes Pear NITF parser
require_once 'XML/NITF.php';
// File path
$sourcedir = "./path/to/my/article.xml";
// Wraps paragraph tags around the content of the article
function tag_wrap(&$p) {
$p = "<p>$p</p>";
}
// Image information
$mime = 'image/jpeg';
$image_file = "./path/to/my/image.jpg";
$nitf = new XML_NITF();
$nitf->setInputFile($sourcedir);
$xResult = $nitf->parse();
if (PEAR::isError($xResult)) {
die("Parsing failed: ".$xResult->getMessage());
}
// Grab some stuff out of the parsed XML file
$content=$nitf->getContent();
array_walk($content, 'tag_wrap');
$content_wrapped = implode('', $content);
$headline1=$nitf->getHeadline(1);
if (!empty($headline1)) {
$header = $headline1;
} else {
$header = 'Headlines are so 1990';
}
// Create a new file record
$file = new stdClass();
$file->filename = basename($image_file);
$file->filepath = $image_file;
$file->filemime = $mime;
$file->filesize = filesize($image_file);
$file->uid = $uid;
$file->status = FILE_STATUS_PERMANENT;
$file->timestamp = time();
drupal_write_record('files', $file);
$file->fid = db_result(db_query("SELECT fid FROM {files} WHERE filepath = '%s'", $file->filepath));
// Add a node object
$node = new stdClass();
$node->type = 'article';
$node->body = $content_wrapped;
$node->title = $headline1;
$node->field_image = array(
array(
'fid' => $file->fid,
'title' => basename($file->filename),
'filename' => $file->filename,
'filepath' => $file->filepath,
'filesize' => $file->filesize,
'mimetype' => $mime,
'description' => basename($file->filename),
'list' => 1,
),
);
$node->uid = 1;
$node->status = 1;
$node->active = 1;
$node->promote = 1;
$node = node_submit($node);
// Save the new node
if ($node->validated) {
node_save($node);
}
Comments
Bump. Still can't seem to
Bump.
Still can't seem to wrap my head around how to add images to the parsed articles.
Like I said above, I need to figure out a way to match the image(s) with the corresponding article and then somehow insert them throughout the article and have text float around them.
I guess I would need to make the script do something like: move images from current dir to /sites/default/files/new_image_dir/.
My brain's on fire.
*_*
You should 'copy' the files from current source to new destination using php 'copy' function. That worked for me.
Cheers!
Regards.
🪷 Beautifulmind
*_*
Hi
Thank you very much for sharing the code, it has saved lot of my time.
Regards
Regards.
🪷 Beautifulmind