Drupal 6 Site Structure Script.
I continue to find useful the script I posted some time ago that creates a basic site structure in Drupal. Its not uncommon that a customer will want a leg up on migrating their content into Drupal, or will have a basic structure in mind even before they fill it with content.
I recently started working in Drupal 6, now that many of the modules I typically use are at least in beta for Drupal 6. So, I found that I needed to update the script as follows:
#!/usr/bin/php
<?php
error_reporting(E_ERROR);
// check the command line args, provide help
if ($argc != 1 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
?>
This is a command line PHP script.
Use it as indicated to create the structure of a site, complete with placeholder
pages, custom paths and proper placement in the menu structure without the
tedium of the Drupal GUI. Then the content of each page can be customized.
Usage: <?php echo $argv[0]; ?>
When run from the root of the Drupal install, it looks for a file in that
directory called structure.import with the following format:
<url path> | <menu label> | <page title>
- <url path> | <menu label> | <page title>
-- <url path> | <menu label> | <page title>
The lines in this file are in order, optionally with an '-' character at the
beginning to indicate placement in the hierarchy.
Only one or two elements may be specified on each line, the remaining fields
will be deduced. Capitalization is normalized and whitespace trimmed.
With --help, -help, -h, or -? options, you can get this help.
<?php
}
// load in necessary Drupal classes, database connection information
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
// file format configuration
$levelDelim = '-';
$elementDelim = '|';
// track levels
// for each level, map parent ids to current weight for an item on that parent
// 1=navigation, 2=primary links
$levels[] = array(1,0);
$import = "structure.import";
if (file_exists($import)) {
$lines = file($import);
foreach ($lines as $line_num => $line) {
if (trim($line) != '') {
$elements = explode($elementDelim, $line);
$level = substr_count($elements[0], $levelDelim) + 1;
$path = $elements[0];
$path = str_replace($levelDelim, '', $path);
$path = trim($path);
$path = strtolower($path);
$label=ucwords($path);
$title=$label;
if (isset($elements[1])) {
$label = trim($elements[1]);
}
if (substr_count($path, ' ')) {
$path = str_replace(' ', '_', $path);
}
if (isset($elements[2])) {
$title = trim($elements[2]);
}
// create the page
$node = new StdClass();
$node->uid = 1;
$node->type = 'page';
$node->status = 1; // published
$node->promote = 0; // don't promote to front page
$node->path = $path; // ?q=path
$node->format=3; // full HTML
$node->title = $title;
$node->body = ''; // add later
node_save($node);
$parentLevel = $level-1;
$parentLevelInfo =& $levels[$parentLevel];
// create the menu item
$menuItem = array();
$menuItem['plid'] = $parentLevelInfo[0];
$parentLevelInfo[1]++;
$menuItem['weight']=$parentLevelInfo[1];
$menuItem['link_path']='node/' . $node->nid;
$menuItem['link_title']=$label;
$menuItem['type']=118; // see includes/menu.inc
menu_link_save($menuItem);
$levels[$level] = array($menuItem['mlid'],0);
}
}
} else {
echo "\n\nNo import file: $import found.\n";
}
?>
Basically, its mostly the same as before except that the menu API has changed somewhat. This seems to recreate the version 5 results.

Thanks for the post. As a newbie, I don’t need the functionality just yet, but I appreciate just how useful it’s going to be for client install #n+1.
Very thoughtful of you to do this.
JB
[...] found one, possibly useful post about how to approach automating import into Drupal. On his blog, Adam Smith proposes a two step process to import pages into Drupal [...]
Hi,
Thanks for the post,
I’m a newbie, and I wanted to import a sql dump file.
Can you give me the sample of the “structure.import” please.
Thank you very much
Sure, just check out the comments in the code block above for the format of the file.
I am a newbie to Drupal. Where do I run this script from? My home directory? Module directory?
Joe,
If you read both this post and the previous post that this one updates, these basic questions should all be answered for you. If you have a question that I haven’t already addressed in these posts, let me know, and I will be glad to help further.
link back to original article would be nice. thanks.
Hey Bob, its there in the first sentence:
http://www.stonemind.net/blog/2007/11/18/a-possible-approach-to-importing-static-content-into-drupal/
ah well, you’re right . blinded by the light i guess. think i’ll get some lunch. sorry,
Not a problem Bob, I hope you find the posts useful.
I want to know a bit more about Drupal, how it can be useful in good web designing and web site management. If you can post some of its advantages and uses or can provide some link to the material.
From Asset Managemnet.
Amazing. Wonderful. I’ve been looking for something like this for ages.
I’ve developed a website for a non-profit organization, in PHP, and was dreading the addition of every single page, and menu styling.
You’ve just made my life much, much easier.
Thank You,
Brandon McGinty
Hi Adam,
This is great - just what I needed - and works much better than any of the node / menu import modules I was looking into.
I think you should package it as a module as I’m sure it’ll get more exposure being in the modules directory.
Thanks for sharing.
Cheers,
Alex