Hello!

I'm trying to import on Drupal 6 a content type guestbook (German: gaestebuch) with some CCK fields like described at the website http://www.lullabot.com/articles/quick_and_dirty_cck_imports .

Unfortunatly I just get the following error message

Fatal error: Unsupported operand types in /var/www/xxxxx/html/includes/form.inc on line 511

I tried it also before in a different way with node_save, but this didn't save my CCK fields, just standard node fields.

All help is appreciated! Also if you have other ideas how I can solve my requirenment it is welcome, I don't want to enter all the data manually ...

Below the script is attached.

Thanks in advance and best regards,

Chris


$execute_script = true;
$debuglevel = 4; // 0 = no debug; 1 = error; 2 = warning; 3 = info; 4 = debug
$adminuser = 'admin';
$adminpass = 'XXXXXXXXXXXXXXXXXX';
$contenttype = 'gaestebuch_test';
$nodeform = 'gaestebuch_test_node_form';
$nodepromoted = 0;
$nodesticky = 0;

function mymain() {
  global $execute_script;
  global $debuglevel; 
  global $adminuser;
  global $adminpass;
  global $contenttype;
  global $nodepromoted;
  global $nodesticky;

  if ($execute_script){
    
    // initiate script
    init_script();
    html_head();
    echo "Bootstrap sucessfull<br><br>";
    print("debuglevel: ".$debuglevel."<br><br>");
    // Test creation of node
    $nodetitle = 'Test 02';
    $nodebody = 'Test 02 script.';
    $nodeuserid = 0;
    $nodecreated = time(); 
    $gaestebuch_name = 'Chris';
    $gaestebuch_email = 'mail@domain.dom';
    $gaestebuch_homepage = 'http://www.domain.dom/';
    
    create_mynode ($nodetitle,$nodebody,$nodeuserid,$nodecreated, $gaestebuch_name,$gaestebuch_email,$gaestebuch_homepage );

    html_foot();
  }

}

function create_mynode ($nodetitle,$nodebody,$nodeuserid,$nodecreated, $gaestebuch_name,$gaestebuch_email,$gaestebuch_homepage ) {
  global $nodeform;
  global $execute_script;
  global $debuglevel; 
  global $adminuser;
  global $adminpass;
  global $contenttype;
  global $nodepromoted;
  global $nodesticky;

  
  
  //DEBUG Level Debug
  if($debuglevel >= 4) {
    print("Parameters: nodetitle: ".$nodetitle." nodebody: ".$nodebody." nodeuserid: ".$nodeuserid." nodecreated: ".$nodecreated." gaestebuch_name: ". $gaestebuch_name." gaestebuch_email: ".$gaestebuch_email." gaestebuch_homepage: ".$gaestebuch_homepage."<br>");
  }


  // new node
  $node = new StdClass();

  $values = array();
  module_load_include('inc', 'node', 'node.pages'); 
  $node = array('type' => $contenttype);	//Can be any content type you have
  $values['values']['is_new'] = TRUE;  //If this is a new entry, add this.  Otherwise replace it with $node->nid
  $values['values']['status'] = 1;  //Optional if this is an existing node
  $values['values']['name'] = 'Gast'; //$nodeuserid;  //Optional if this is an existing node.
  $values['values']['created'] = $nodecreated; //** Valid unix time stamp
  $values['values']['changed'] = $nodecreated; //** Valid unix time stamp
  $values['values']['promote'] = $nodepromoted; // promote to frontpage
  $values['values']['sticky'] = $nodesticky;
  $values['values']['language'] = 'de';
  $values['values']['op'] = t('Save'); // is mandatory, otherwise the node won't be saved
  $values['values']['title'] = $nodetitle;
  $values['values']['teaser'] = $nodebody;
  $values['values']['body'] = $nodebody;

  // CCK Fields

  // field_gaestebuch_name
  $values['values']['field_gaestebuch_name'][0]['value'] = $gaestebuch_name;  // CCK field for Name
  // field_gaestebuch_email
  $values['values']['field_gaestebuch_email'][0]['value'] = $gaestebuch_email;  // CCK field for email
  // field_gaestebuch_homepage
  //  field_gaestebuch_homepage[0][title]
  //  field_gaestebuch_homepage[0][url]
  $values['values']['field_gaestebuch_homepage'][0]['title'] = $gaestebuch_homepage;  // CCK field for Homepage Title
  $values['values']['field_gaestebuch_homepage'][0]['url'] = $gaestebuch_homepage;  // CCK field for Hompage URL
  $values['values']['field_gaestebuch_homepage'][0]['attributes'] = 'n';


  //DEBUG Level Info
  if($debuglevel >= 3) {
    print("<br>Creating new node with Title: ".$values['values']['title']." from user: ".$values['values']['uid']." Creation Date: ". unixtime2date($values['values']['created'])."<br>");
  }

  // create the node
  drupal_execute($nodeform, $values, (object)$node);

  //DEBUG Level Info
  if($debuglevel >= 3) {
    print("... done: new Node ID: ".$node->nid."<br>");
  }
  

}

function init_script() {
  global $adminuser;
  global $adminpass;


  include_once('includes/bootstrap.inc');
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  bootstrap_invoke_all('init');
  ini_set('memory_limit', '512M');
  

  require_once 'modules/node/node.pages.inc';

  //Authenticate as user 1
  user_authenticate($adminuser, $adminpass);
}


function unixtime2date($unixtimestamp){
  return date ('Y-m-d H:i', $unixtimestamp);
}

function html_head() {
  global $contenttype;
  print("<html><head><title>Import ".$contenttype."</title></head><body><h1>Import ".$contenttype."</h1>");

}

function html_foot() {
  print("</body></html>");
}
mymain();

Comments

MidGe48’s picture

I am not sure if you had a look at the node import module which is rc4 for Drupal 6.x!

It may help you and make things easier.

It can be found here: http://drupal.org/project/node_import

www.ZuNOB.com

finke77’s picture

Hello!

I am getting forward with the node_save command. I can create now automatically nodes with old data stored in a database table.
My problem before was that content_insert($node); was missing. Without the CCK fields are not stored.

The problem I still have is, that the nodes are created and visible for administrators, but not for all users.
The workaround is at the moment to do a mass processing at the admin/content/node page. When I publish all nodes (despite of they are already published), they are also visible for other (normal) users. It seems this function is doing some other processing in the background.

Do you have any ideas, why the nodes are not directly visible and what the mass processing function does in addition? How can I change my script so that the nodes are visible directly?

Below you can find the current state of the script.

Best regards,

Christian


$execute_script = true;
$debuglevel = 4; // 0 = no debug; 1 = error; 2 = warning; 3 = info; 4 = debug
$adminuser = 'admin';
$adminpass = 'XXXXXXXXXXXXX';
$contenttype = 'gaestebuch_test';
$nodepromoted = 0;
$nodesticky = 0;




function mymain() {
  global $execute_script;
  global $debuglevel; 
  global $adminuser;
  global $adminpass;
  global $contenttype;
  global $nodepromoted;
  global $nodesticky;

  if ($execute_script){
    
    // initiate script
    init_script();
    html_head();
    echo "Bootstrap sucessfull<br><br>";
    print("debuglevel: ".$debuglevel."<br><br>");

    // Test creation of node
    $testts = $timestamp = mktime('16','59','00','03','01','2009'); // unix timestamp
    print("<br>Test Timestamp: ".$testts ."<br>");
    print("<br>Now: ".time() ."<br>");

    $nodetitle = 'Test 13';
    $nodebody = 'Test 13 script.';
    $nodeuserid = 0;
    $nodeusername = 'Gast';
    $nodecreated = $testts; 
    $gaestebuch_name = 'Christian';
    $gaestebuch_email = 'mail@domain.dom';
    $gaestebuch_homepage = 'http://www.domain.dom/';
    
    //create_mynode ($nodetitle,$nodebody,$nodeuserid,$nodeusername,$nodecreated, $gaestebuch_name,$gaestebuch_email,$gaestebuch_homepage );

    $results = get_old_data ();
    process_old_data ($results);

    html_foot();
  }

}

function create_mynode ($nodetitle,$nodebody,$nodeuserid,$nodeusername,$nodecreated, $gaestebuch_name,$gaestebuch_email,$gaestebuch_homepage ) {
  global $execute_script;
  global $debuglevel; 
  global $adminuser;
  global $adminpass;
  global $contenttype;
  global $nodepromoted;
  global $nodesticky;

  print("debuglevel: ".$debuglevel);
  
  // Construct the new node object.
  $node = new stdClass();

  $node->is_new = TRUE;  //If this is a new entry, add this.  Otherwise replace it with $node->nid
  $node->type = $contenttype; //Can be any content type you have
  node_object_prepare($node); // just filled in default values for uid, status, promote, status, date, created, and revision properties
  // Your script will probably pull this information from a database.
  $node->status = 1;  //Optional if this is an existing node
  $node->uid = $nodeuserid;  //Optional if this is an existing node.
  $node->name = $nodeusername;
  $node->created = $nodecreated; //** Valid unix time stamp
  $node->date = $nodecreated;
  $node->timestamp = $nodecreated;
  $node->changed = $nodecreated;
  $node->promote = $nodepromoted;
  $node->sticky = $nodesticky;
  $node->language = 'de';
  $node->format = '1';
  $node->pathauto_perform_alias = 1;
  $node->title = $nodetitle;  //Optional if this is an existing node
  $node->teaser = $nodebody;
  $node->body = $nodebody;

  // CCK Fields
  //$node->field_whatever[0]['value'] = 'kkkkkkkkkk';  //This is a CCK field
  // If known, the taxonomy TID values can be added as an array.
  //$node->taxonomy = array(2,3,1,);


  // field_gaestebuch_name
  $node->field_gaestebuch_name[0]['value'] = $gaestebuch_name;  // CCK field for Name
  // field_gaestebuch_email
  $node->field_gaestebuch_email[0]['email'] = $gaestebuch_email;  // CCK field for email
  // field_gaestebuch_homepage
  //  field_gaestebuch_homepage[0][title]
  //  field_gaestebuch_homepage[0][url]
  $node->field_gaestebuch_homepage[0]['title'] = $gaestebuch_homepage;  // CCK field for Homepage Title
  $node->field_gaestebuch_homepage[0]['url'] = $gaestebuch_homepage;  // CCK field for Hompage URL
  $node->field_gaestebuch_homepage[0]['attributes'] = 'N;';

  //DEBUG Level Info
  if($debuglevel >= 3) {
    print("<br>Creating new node with Title: ".$node->title." from user: ".$node->uid." Creation Date: ". unixtime2date($node->created)."<br>");
  }

  
  node_validate($newnode);
  $node = node_submit($node);

  $node->created = $nodecreated; // have to be set again, otherwise current servertime is taken
  $node->date = $nodecreated;  // have to be set again, otherwise current servertime is taken
  $node->timestamp = $nodecreated;  // have to be set again, otherwise current servertime is taken

  node_save($node);  //Actually save or edit the node now
  content_insert($node); // necessary, otherwise CCK fields are not stored
  
  $nid = $node->nid;

  //DEBUG Level Info
  if($debuglevel >= 3) {
    print("... done: new Node ID: ".$nid."<br>");
  }

    
  //DEBUG Level Debug
  if($debuglevel >= 4) {
   
    print("<br>========================================<br>");
    print(nl2br('<pre>'.print_r($node,true).'</pre>'));
    //print_r($node) . "<br>";

    print("========================================<br><br>");
  }

}

function init_script() {
  global $adminuser;
  global $adminpass;

  include_once('includes/bootstrap.inc');
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  bootstrap_invoke_all('init');
  ini_set('memory_limit', '512M');
  
  require_once 'modules/node/node.pages.inc';

  //Authenticate as user 1
  user_authenticate($adminuser, $adminpass);
}

function get_old_data () {
  //add here SQL for old data
  return $results = db_query("SELECT * from old_spz_gaestebuch");
}

function process_old_data ($results) {
  while($olddata = db_fetch_object($results)) {
  
  $nodetitle = $olddata->name;
  $nodebody = $olddata->text;
  $nodeuserid = 0;
  $nodeusername = "Gast";

   //converting old fields time and date to unix timestamp
  $tarray = split(':',$olddata->uhrzeit);
  $darray = split('-',$olddata->datum);
  $timestamp = mktime($tarray[0],$tarray[1],'00',$darray[1],$darray[2],$darray[0]);
  

  $nodecreated = $timestamp;
  $gaestebuch_name = $olddata->name;
  $gaestebuch_email = $olddata->email;
  $gaestebuch_homepage = $olddata->homepage;

  create_mynode ($nodetitle,$nodebody,$nodeuserid,$nodeusername,$nodecreated, $gaestebuch_name,$gaestebuch_email,$gaestebuch_homepage );

  }
}

function unixtime2date($unixtimestamp){
  return date ('Y-m-d H:i', $unixtimestamp);
}


function html_head() {
  global $contenttype;
  print("<html><head><title>Import ".$contenttype."</title></head><body><h1>Import ".$contenttype."</h1>");

}

function html_foot() {
  print("</body></html>");
}

mymain();

keve’s picture

Did you get this script right?

Some basic troubleshooting questions: Do you have permssisions enable for users to view this content-type?
Do you use any access-type modules?