Drupal 6 Site Structure Script.

June 23, 2008

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.

13 Comments »

Comment by James Britton
2008-09-05 06:50:56

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

 
2008-11-13 03:08:46

[...] 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 [...]

 
Comment by Andry
2008-11-18 06:25:28

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

Comment by admin
2008-11-18 16:00:20

Sure, just check out the comments in the code block above for the format of the file.

 
 
Comment by Joe
2008-11-23 09:41:30

I am a newbie to Drupal. Where do I run this script from? My home directory? Module directory?

Comment by admin
2008-11-23 10:24:13

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.

 
 
Comment by bob phillips
2009-02-13 11:24:47

link back to original article would be nice. thanks.

Comment by admin
 
 
Comment by bob phillips
2009-02-13 12:30:32

ah well, you’re right . blinded by the light i guess. think i’ll get some lunch. sorry,

Comment by admin
2009-02-13 12:33:01

Not a problem Bob, I hope you find the posts useful.

 
 
Comment by Asset Management
2009-07-05 00:09:05

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.

 
Comment by Brandon McGinty
2009-08-10 19:30:56

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

 
Comment by Alex Bovey
2010-02-22 00:17:09

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

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> in your comment.