Authors, as you remember will be nodes since they will (at some point) contain additional information about the author. The same assumptions are made on file and folder paths. Each author has it's own unique identifier in the data files that needs to be included in the Drupal database. The node ID and author ID are two separate identifiers, we need the author ID in order to retrieve the node ID. This is designed to prevent duplicates and allow synchronization.
We start the same way we did with categories, but you'll notice that this time I am abstracting the data file.
<?php
//Execute without actually writing to the DB
$TEST = false;
//Load the Libraries to abstract the data input file
include ("Parser.class.php");
//Change to the Drupal Directory
chdir("../cart");
//Load and init the Drupal System
require_once 'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
//Create new parser and product data object
$parser = new Parser();
$dataLine = $parser->parseFile();
?>Similar to the terms/categories I will also utilize some convenience functions to help keep my code clean and organized. These two function allow me to retrieve the author's node ID from either the data file's ID or the Author's display name. Obviously it's much more reliable to retrieve it by using the author's ID in the data file.
<?php
function getNodeIdFromTitle($nodeTitle, $nodeType){
$query = "SELECT * FROM node WHERE type = \"$nodeType\" AND title = \"$nodeTitle\" LIMIT 1";
$queryResult = db_query($query);
$nid = false;
echo "Looking up node ID for \"$nodeTitle\"" . "<br />\n";
if ($node = db_fetch_array($queryResult)) {
$nid = $node['nid'];
echo "Getting node ID for \"$nodeTitle\", Found: nid=" . $nid . "<br />\n";
}
return $nid;
}
function getNodeIdFromExtID($extID, $nodeType){
$query = "SELECT * FROM content_type_$nodeType WHERE field_" . $nodeType . "_id_value = \"$extID\" LIMIT 1";
$queryResult = db_query($query);
$nid = false;
echo "Looking up node ID for $nodeType ID: \"$extID\"" . "<br />\n";
if ($node = db_fetch_array($queryResult)) {
$nid = $node['nid'];
echo "Getting node ID for $nodeType ID: \"$extID\", Found: nid=" . $nid . "<br />\n";
}
return $nid;
}
?>Their is one more function, this one is purely optional (meaning you can place the code for this one directly), but it helps keep this script extensible. It's purpose is to build the node data structure according to Drupal's requirements. This function will make more sense to you afterward.
<?php
function buildNodeFields_author($nodeData) {
$values = array();
$values['title'] = $nodeData->displayName;
$values['field_author_id'] = array(array('value' => $nodeData->id));
echo "Node fields built." . "<br />\n";
return $values;
}
?>As you are probably realizing by now, the structure of the import script remains pretty much the same for any type of item. It's greatly simplified by the fact that I am abstracting the data file, but it's the same concept as shown with the vocabulary terms (categories/subjects). Go through each line of the data file and process that line if it's not empty.
<?php
foreach ($dataLine as $aItem) {
if (!empty($aItem->id)) {
?>Check to see if this author already exists in the Drupal database by utilizing the function created earlier. We retrieve the node id using the data files ID (ExtID). The use of the function is pretty simple, the first parameter is the ExtID and the second is the node type.
If a node ID is found then we update this author's information. If no node ID is found, then we create a new node with the minimum information only in the structure as require by Drupal. We will fill in the rest later, for now all we need is a node ID to reference. This design allows us to update or create data with the same code.
<?php
//First see if this already exists in the Database
if (($nid = getNodeIdFromExtID($aItem->id, 'author')) !== false) {
//It Exists, Update it
$author_node = node_load($nid);
$author_node->uid = '1';
$author_node->name = 'admin';
echo "Updating: " . $aItem->displayName . "<br />\n";
}
else {
//Does not Exist, Make new node
$author_node = array(
'uid' => (string) 1,
'name' => (string) 'admin',
'type' => 'author',
'language' => '',
'body' => NULL,
'title' => NULL,
'format' => NULL,
'status' => true,
'promote' => false,
'sticky' => false,
'revision' => false,
'comment' => '0'
);
$author_node = (object) $author_node;
echo "Creating: " . $aItem->displayName . "<br />\n";
}
?>We now construct the author's information from the data file using the function created earlier. The variable "aItem" is an object that represent a line from the data file. Basically I am getting back this line as an associative array with index names as required by Drupal.
<?php
//Construct Node Fields
$node_fields = buildNodeFields_author($aItem);
?>Now that we have the node's data structure properly built, we save it to the Drupal database by utilizing the Drupal API function "drupal_execute". For more information about creating nodes, be sure to visit read the HelpFile Create Drupal Nodes from a Script.
<?php
//Save Node
if (!$TEST){$result = drupal_execute('author_node_form', $node_fields, $author_node);}
//Get node ID
if (!$TEST){$nid = str_replace("node/", "", $result);}
echo "Node: " . $nid . " Saved<br />\n";
?>Just like we did with the vocabulary terms, we tell PHP to give us more time and proceed to the next item on the list.
<?php
//Counter
$row++;
//Reset Timelimit
echo "*************Resetting PHP Execution Time Limit********** <br />\n";
set_time_limit(60);
}
}
?>