"; /* $existing_pagenode = node_load(1254); print_r($existing_pagenode); $existing_pagenode = node_load(1293); print_r($existing_pagenode); die(); */ // Get all books and lookup in the database if they are alreay inserted $books = simplexml_load_file('http://sandbox.raamatumaailm.ee/raamatumaailmale_naidis.xml'); $processed = 0; foreach($books->xpath('//raamatukoi/raamat') as $book) { if ($processed >= $BOOKS_PER_BATCH) break; $isbn = (string) $book->isbn; $title = (string) $book->pealkiri; $authorterm = (string) $book->autor; $frontpage = (string) $book->pilt; if (array_key_exists($isbn, $isbns)) { echo "already exists: $isbn \n"; } else { $newid = addbook($isbn, $title, $authorterm, $frontpage); echo "added book with node id = $newid \n"; } $processed++; } function addbook($isbn, $title, $authorterm, $frontpage) { global $user; // Get extended information about the book $bookinfo = simplexml_load_file('http://sandbox.raamatumaailm.ee/raamatumaailmale_naidis.xml'); if (!bookinfo) return -1; // Get the user - should make Drupal adding a correct author for a node $user = user_load(array(uid => 1)); //Prepare new node for book - standard fields $node = new stdClass(); $node->type = 'raamat'; $node->field_esmalugemiseks[0]['value'] = 0; $node->uid = 1; $node->status = 0; $node->promote = 0; //Prepare new node for book - CCK fields that have only one value //list($width, $height) = split('x', (string) $bookinfo->measures, 2); //$node->field_raamatu_laius[0]['value'] = str_replace(',', '', trim($width)); //$node->field_raamatu_korgus[0]['value'] = str_replace(',', '', trim($height)); //$node->field_raamatu_lehekylgi[0]['value'] = (string) $bookinfo->pages; $node->field_raamatu_isbn[0]['value'] = $isbn; //$node->field_kirjeldus_value[0]['value'] = (string) $bookinfo->abstract; //$year = str_ireplace('Ilmunud ', '', (string) $bookinfo->published); //$node->field_raamatu_ilmumisaeg[0]['value'] = mktime(1, 1, 1, 1, 1, $year); // Prepare book titles - taxonomy to be added to already existing node! $titleterm = (string) $title; $title_tid = lookup_taxonomy_term(trim($titleterm), 2); if ($title_tid > 0) { $node->field_raamatu_pealkiri[0]['value'] = $title_tid; echo $title_tid; } // Prepare book authors - taxonomy to be added to already existing node! $i = 0; //foreach($bookinfo->xpath('//raamatukoi/raamat/autor') as $author) { $author_tid = lookup_taxonomy_term(trim($authorterm), 3); if ($author_tid > 0) { $node->field_raamatu_autor[$i]['value'] = $author_tid; } $i++; //} // Prepare format (soft/hard) //$printinfo = (string) $bookinfo->printinfo; //$printinfoterm = (strpos($printinfo, 'pehme') !== false) ? 'PK' : 'KK'; //$printinfo_tid = lookup_taxonomy_term(trim($printinfoterm), 12); // if ($printinfo_tid > 0) { // $node->field_raamatu_formaat[0]['value'] = $printinfo_tid; // } // Prepare the frontpage picture //$frontpage = (string) $bookinfo->xpath('//raamatukoi/raamat/pilt'); $remotefilename = trim($frontpage); $localfilename = 'sites/default/files/isbn'.$isbn.'.jpg'; if (file_exists($localfilename)) { $result = db_query("select * from files where filepath='$localfilename'"); if ($result) { $fileInfo = db_fetch_array($result); if (count($fileInfo) > 0) { $node->field_kaanepilt[0] = $fileInfo; } } } else if (copy($remotefilename, $localfilename)) { $currenttime = mktime(); $imagesizes = getimagesize($localfilename); $filename = basename($localfilename); $filemime = $imagesizes['mime']; $filesize = filesize($localfilename); db_query("insert into files (uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (1, '$filename', '$localfilename', '$filemime', $filesize, 1, '$currenttime')"); $fid = db_last_insert_id('files', 'fid'); $result = db_query("select * from files where fid=$fid"); if ($result) { $fileInfo = db_fetch_array($result); if (count($fileInfo) > 0) { $node->field_kaanepilt[0] = $fileInfo; } } } // Save the node to Drupal DB node_save($node); $nid = $node->nid; // Debug if ($nid > 0) { $node = node_load($node->nid); $node->vid = $node->title = $node->nid; node_save($node); } else { echo "Problems with adding node?"; return -1; } // Delete - remove this line when put to production //node_delete($nid); return $nid; } // Looks up existing term of given vocabulary, or creates a new one function lookup_taxonomy_term($typed_term, $vocabulary) { $possibilities = taxonomy_get_term_by_name($typed_term); $typed_term_tid = 0; foreach ($possibilities as $possibility) { if ($possibility->vid == $vocabulary) { $typed_term_tid = $possibility->tid; } } if ($typed_term_tid == 0) { $edit = array('vid' => $vocabulary, 'name' => $typed_term); $status = taxonomy_save_term($edit); $typed_term_tid = $edit['tid']; } return $typed_term_tid; } // Get contents of any url function geturl($url) { // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); // grab URL and pass it to the browser $info = curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); return $info; } ?>