1
votes

I attempted to import images from a custom CMS to Drupal using direct MySQL insert queries to the Drupal database. The images are copied to a path within the drupal site:

sites/default/files/images/import

The nodes show up in lists but the direct links (someurl/node/1234) cause 404 errors. They are all of type media_image and have a valid uid assigned. I also did associated inserts to files and content_type_media_image

Is there an internal Drupal process that needs to happen to finalize this import? Is it at all possible without rewriting as a proper Drupal import module?

Sample node insert:

INSERT INTO node SET
    type='media_image',
    language='en',
title='apicture.jpg',
uid='4',
status=1,
created=1328644135,
changed=1328644135;

// grab nid as last mysql_insert()
INSERT INTO files SET
uid = 4,
filename = 'apicture.jpg',
filepath = 'sites/default/files/images/import',
filemime = 'image/jpeg',
filesize = 40069,
status = 1,
timestamp = 1328644136;

INSERT INTO content_type_media_image SET
vid = 683,
nid = 683,
field_image_fid = 539,
field_image_list = 1,
field_image_data = 'a:3:{s:11:"description";s:0:"";s:3:"alt";s:0:"";s:5:"title";s:0:"";}';

Note: the queries above are the generated queries.

2

2 Answers

2
votes

If you're getting a 404 it's because the return from the internal node_load() function is NULL/FALSE.

If you step through that function you'll see that an INNER JOIN is made from the node table to the node_revisions table...so if there's no matching record in the node_revisions table as far as Drupal's concerned there is no node.

To fix the error you'll just need to populate the node_revisions table with its required columns - most notably the nid and vid (which is the version ID), and title and body. You may also want to set the text format for the body text in the format column (it's related to the key in the filter_formats table).

Incidentally this version ID should also be added to the node table itself, as that's what the INNER JOIN is made on. I think the safest way to get the next available ID is:

SELECT MAX(vid) + 1 FROM node

If you don't have revisions turned on for any content types it's likely the vid will always be the same as the nid though.

1
votes

I think going for direct SQL inserts is a lot too much hassle. Drupal proposes many solutions to import content, my favourite one being Feeds : http://drupal.org/project/feeds. It automates everything, accepts almost any input format and provides very flexible mapping mechanisms.

Going the direct database way means you bypass all the power Drupal provides, and you'll probably reinvent the wheel.