getMessage());
}
//we need to find the uid for the drupal user
$uid = getUID($drupaluser);
//include the field mappings
require_once $contenttype . ".inc";
//open the csv file for reading
$fh = fopen($csvfile, "r");
//loop over the data in the file
// NOTE: the fgetcsv() may need to be modified to reflect your input files setup - especially if it is not using a comma as a delimiter or double quotes to enclose fields.
while (($data = fgetcsv($fh)) !== FALSE) {
echo "\nAdding records for ". $data[0] . "
";
echo "
";
$ids = getNextIDs();
createNode($ids["nid"], $ids["vid"], $contenttype, $data[0], $uid);
createNodeRevision($ids["nid"], $ids["vid"], $uid, $data, $fieldmap);
createCCKRecord($tablename, $ids["nid"], $ids["vid"],$ids["fid"], $data, $fields, $filepath );
//mapToTerm($ids["nid"], 47); // <----- Change this to the term ID that is needed, or comment out this line if a term is not required.
createCommentStats($ids["nid"], $uid);
updateIDs($ids["nid"], $ids["vid"]);
}
//close the csv file
fclose($fh);
echo "\n\n"; //throw out a few new lines just to be sure.
//Exit the script (we're done)
exit;
/* **************************************** */
/* gets mimetype for a filename */
/* **************************************** */
function getMIMEType( $filename ){
$extensions = array();
$extensions['jpg'] = "image/jpeg";
$extensions['png'] = "image/png";
$extensions['gif'] = "image/gif";
$extensions['doc'] = "application/msword";
$extensions['xls'] = "application/excel";
$extensions['pps'] = "application/mspowerpoint";
$extensions['ppt'] = "application/mspowerpoint";
$extensions['pdf'] = "application/pdf";
$file = basename($path);
$fileParts = explode( ".", $filename);
$ext = $fileParts[ ( sizeof($fileParts) - 1 ) ];
if( isset( $extensions[$ext] ) ){
return $extensions[$ext];
}else{
return "";
}
}
/* **************************************** */
/* find the drupal user id for a given username */
/* **************************************** */
function getUID($uname) {
global $db;
$sql = "select uid from users where name = '". addslashes($uname) ."'";
$result = & $db->query($sql);
if (MDB2::isError($db)) {
die($db->getMessage());
}
$d = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
return $d->uid;
}
/* **************************************** */
/* get next nid and vid values */
/* **************************************** */
function getNextIDs() {
global $db;
$out = array();
$sql = "select id from sequences where name = 'node_nid';";
$result = & $db->query($sql);
if (PEAR::isError($db)) {
die($db->getMessage());
}
$d = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
$out[ "nid" ] = $d->id + 1;
$sql = "select id from sequences where name = 'node_revisions_vid';";
$result = & $db->query($sql);
if (PEAR::isError($db)) {
die($db->getMessage());
}
$d = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
$out[ "vid" ] = $d->id +1;
$sql = "select id from sequences where name = 'files_fid';";
$result = & $db->query($sql);
if (PEAR::isError($db)) {
die($db->getMessage());
}
$d = $result->fetchRow(MDB2_FETCHMODE_OBJECT);
$out[ "fid" ] = $d->id +1;
return $out;
}
/* **************************************** */
/* update the ID sequences */
/* **************************************** */
function updateIDs($nid, $vid) {
global $db;
//nid
$sql = "update sequences set id = $nid where name = 'node_nid'";
$db->query($sql);
if (PEAR::isError($db)) {
die($db->getMessage());
}
//vid
$sql = "update sequences set id = $vid where name = 'node_revisions_vid'";
$db->query($sql);
if (PEAR::isError($db)) {
die($db->getMessage());
}
}
/* **************************************** */
/* create a node record */
/* **************************************** */
function createNode($nid, $vid, $type, $title, $uid) {
global $db;
$title = str_replace('#comma#',',', $title );
$sql = "insert into node (nid, vid, type, title, uid, status, created, changed, comment, promote, moderate, sticky) values (";
$sql .= $nid .", ";
$sql .= $vid .", ";
$sql .= "'". addslashes($type) ."', ";
$sql .= "'". addslashes($title) ."', ";
$sql .= $uid .", ";
$sql .= "1, "; //status: 0 = unpublished, 1 = published
$sql .= mktime() .", "; //crated date
$sql .= mktime() .", "; //changed date
$sql .= "0, "; //comments: 0 = disabled, 1 = enabled, but no more can be added, 2 = comments can be added
$sql .= "0, "; //promote: 0 = not promoted to front page, 1 = promoted to front page
$sql .= "0, "; //moderation: 0 = disabled, 1 = enabled
$sql .= "0 "; //sticky: 0 = not sticky, 1 = sticky at top of lists
$sql .= ")";
$db->query($sql);
if (MDB2::isError($db)) {
die($db->getMessage());
}
}
/* **************************************** */
/* create the CCK record */
/* **************************************** */
function createNodeRevision($nid, $vid, $uid, $data, $fieldmap) {
global $db;
$sql = "insert into node_revisions ( nid, vid, uid, title, body, teaser, log, timestamp, format) values (";
$sql .= $nid .", ". $vid .", ". $uid .", '". addslashes($data[$fieldmap["title"]]) ."', ";
if ($fieldmap["body"] != -1) { $sql .= "'". addslashes($data[$fieldmap["body"]]) ."', "; } else { $sql .= "'', "; }
if ($fieldmap["teaser"] != -1) { $sql .= "'". addslashes($data[$fieldmap["teaser"]]) ."', "; } else { $sql .= "'', "; }
$sql .= "'', "; //log message
$sql .= mktime() .", "; //timestamp
$sql .= "0 )";
$db->query($sql);
if (MDB2::isError($db)) {
die($db->getMessage());
}
}
/* **************************************** */
/* create the CCK record */
/* **************************************** */
function createCCKRecord($tblName, $nid, $vid, $fid, $data, $fields, $filepath) {
global $db;
$sql = "insert into $tblName ( ";
foreach ($fields as $f) {
$sql .= $f .", ";
}
$sql .= "nid, vid ) values ( ";
$i = 0;
$dataSize = sizeof($data);
foreach ($data as $d) {
if($d == "NULL"){
$d = "";
}
$inserta = true;
if ($fieldmap["file"] != -1){
if( $dataSize == ( $i + 1 ) ){
$inserta = false;
if( $d != "" ){
$fileandpath = $filepath . $d;
$sqlFile = "INSERT INTO files
SET fid = " . $fid . ", nid = " . $nid . ", filename = '" . $d . "',filepath = '" . $fileandpath . "', filemime = '" . getMIMEType( $fileandpath ) . "', filesize = '" . filesize( $fileandpath ) ."'";
$db->query($sqlFile);
if (MDB2::isError($db)) {
die($db->getMessage());
}
$sqlFile = "INSERT INTO file_revisions
SET fid = " . $fid . ", vid = " . $vid . ", description = '" . $d . "', list = 1";
$db->query($sqlFile);
if (MDB2::isError($db)) {
die($db->getMessage());
}else{
$sqlFile = "UPDATE sequences SET id = id + 1 WHERE name = 'files_fid'";
$db->query($sqlFile);
if (MDB2::isError($db)) {
die($db->getMessage());
}
}
}
}
}
if( $i != 0 AND $inserta ){
$d = str_replace('#comma#',',', $d );
$sql .= "'". addslashes($d) . "', ";
}
$i++;
}
$sql .= $nid .", ". $vid .")";
//echo $sql . "
";
$db->query($sql);
if (MDB2::isError($db)) {
die($db->getMessage());
}
}
/* **************************************** */
/* create the CCK record */
/* **************************************** */
function createCommentStats($nid, $uid) {
global $db;
$sql = "insert into node_comment_statistics ( nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count ) values (";
$sql .= $nid .", ". mktime() .", null, ". $uid .", 0 )";
$db->query($sql);
if (MDB2::isError($db)) {
die($db->getMessage());
}
}
/* **************************************** */
/* show the usage */
/* **************************************** */
function mapToTerm($nid, $tid) {
global $db;
$sql = "insert into term_node (nid, tid) values ( $nid, $tid )";
$db->query($sql);
if (MDB2::isError($db)) {
die($db->getMessage());
}
}
/* **************************************** */
/* show the usage */
/* **************************************** */
function showUsage() {
echo "\nUSAGE: \n";
echo " ./import_csv.php db=DB_URL type=NODE_TYPE csv=CSV_FILE user=DRUPAL_USER path=FILE_PATH\n";
echo "\n";
echo " DB_URL - the URL string for the database (i.e. mysql://username:password@localhost/databasename ) \n";
echo " (should be the same value used for the $db_url variable in the sites/default/settings.php file of your Drupal installation)\n";
echo " NODE_TYPE - the node type in lowercase and using underscore in place of spaces\n";
echo " (i.e. 'Bug Report' becomes 'bug_report')\n";
echo " CSV_FILE - the CSV file to be imported\n";
echo " DRUPAL_USER - the Drupal user to use as the author for the content\n";
echo " FILE_PATH - if your nodes have a file associated you must specified where are located\n";
echo "\n\n";
echo "NOTE: this file will only handle 'simple' datatypes at this time. If your CCK content type can have multiple values for a single field\n";
echo " OR you have 'reused' a field in your content type, then this routine will not work for you. However, the necessary changes shouldn't be\n";
echo " too onerous. See http://drupal.org/node/133705 for more detail.\n\n";
}
?>