6.x Amazon.module import script by EvanDonovan, 4/17/09 Based on http://www.lullabot.com/articles/quick_and_dirty_cck_imports Cf. the comments, starting with the one titled "D6" */ module_load_include('inc', 'node', 'node.pages'); // Needed for drupal_execute as per API docs $row_start = 0; $items_per_run = 100; // arbitrary number - adjust to fit your needs if (isset($_GET['row_num'])) { $row_start = $_GET['row_num']; $amazon_nodes = db_query_range("SELECT * FROM {amazonnode}", $row_start, $items_per_run); // Get nodes from AAT module up to $items_per_run while ($amazon_node = db_fetch_object($amazon_nodes)) { // Iterate through the nodes // Make an Amazon book node & create an alias for it $node = array('type' => 'amazon_book', 'pathauto_perform_alias' => 1); $amazon_details = db_fetch_object(db_query("SELECT * FROM {amazonitem} WHERE asin = %d", $amazon_node->asin)); // Get the extended info for the old Amazon nodes $form_state = array(); // form_state is needed for drupal_execute // Set Node Basics $form_state['values']['title'] = $amazon_details->title; // Node title $form_state['values']['teaser'] = $amazon_details->editorialreview; // Node teaser $form_state['values']['body'] = $amazon_details->editorialreview; // Node body (the same) // Set CCK Field $form_state['values']['field_amazon_item'][0]['asin'] = $amazon_node->asin; // ASIN (needed to pull data from Amazon API) // Get Taxonomy Terms /* $old_node = new stdClass(); $old_node->vid = db_result(db_query("SELECT MAX(vid) FROM {node_revisions} WHERE nid = %d", $amazon_node->nid)); $terms = taxonomy_node_get_terms($old_node); */ /* This method is too awkward b/c of the way that taxonomy_node_get_terms returns its results. Instead I will go 1 vocabulary at a time & use the helper functions from um_common.module */ $voc_12 = taxonomy_node_list_terms_by_vocabulary($amazon_node->nid, 12); $voc_3 = taxonomy_node_list_terms_by_vocabulary($amazon_node->nid, 3); $voc_7 = taxonomy_node_tid_array_by_vocabulary($amazon_node->nid, 7); $voc_10 = taxonomy_node_get_tid_by_vocabulary($amazon_node->nid, 10); $voc_8 = taxonomy_node_tid_array_by_vocabulary($amazon_node->nid, 8); $voc_43 = taxonomy_node_tid_array_by_vocabulary($amazon_node->nid, 43); $voc_11 = taxonomy_node_tid_array_by_vocabulary($amazon_node->nid, 11); $voc_24 = taxonomy_node_tid_array_by_vocabulary($amazon_node->nid, 24); $voc_4 = taxonomy_node_get_tid_by_vocabulary($amazon_node->nid, 4); $voc_5 = taxonomy_node_tid_array_by_vocabulary($amazon_node->nid, 5); // Set Taxonomy Terms if($voc_12 != '') { $form_state['values']['taxonomy']['tags']['12'] = $voc_12; } // Speaker/Author (free-tagging, terms as comma-delimited string) if($voc_3 != '') {$form_state['values']['taxonomy']['tags']['3'] = $voc_3; } // Other Tags (free-tagging, terms as comma-delimited string) if(count($voc_7) > 0) { $form_state['values']['taxonomy']['7'] = $voc_7; } // Audience (array of tids) if($voc_10 != '') { $form_state['values']['taxonomy']['10'] = $voc_10; } // Category (tid as string) if(count($voc_8) > 0) { $form_state['values']['taxonomy']['8'] = $voc_8; } // Channel/Issue Area (aot) if(count($voc_43) > 0) { $form_state['values']['taxonomy']['43'] = $voc_43; } // Content Display (aot) if(count($voc_11) > 0) { $form_state['values']['taxonomy']['11'] = $voc_11; } // Content Type (aot) if(count($voc_24) > 0) { $form_state['values']['taxonomy']['24'] = $voc_24; } // TM Program Affiliation for Content (aot) if($voc_4 != '') {$form_state['values']['taxonomy']['4'] = $voc_4; } // UrbanResource.net (tid as string) if(count($voc_5) > 0) { $form_state['values']['taxonomy']['5'] = $voc_5; } // Organization Affiliation (aot) // Settings needed to create the node $form_state['values']['is_new'] = TRUE; // New node $form_state['values']['status'] = 1; // Published $form_state['values']['op'] = t('Save'); // Save it! //kprint_r($form_state); drupal_execute('amazon_book_node_form', $form_state, (object)$node); //Programmatically submit this to the Amazon Book form } } $next_row_start = $row_start + $items_per_run; if (isset($_GET['row_num'])) { echo l( 'continue Amazon node import: rows ' . $next_row_start . ' to ' . ( $next_row_start + $items_per_run - 1), $_GET['q'], array('query' => 'row_num' . $next_row_start)); } else { echo l( 'start Amazon node import: rows ' . $row_start . ' to ' . ($next_row_start - 1), $_GET['q'], array('query' => 'row_num=1' )); } ?> vid = db_result(db_query("SELECT MAX(vid) FROM {node_revisions} WHERE nid = %d", $nid)); $terms = taxonomy_node_get_terms_by_vocabulary($node, $voc_id); $term_list = ""; foreach($terms as $term) {$term_list .= $term->name . ", "; } $term_list = rtrim($term_list, ", "); return $term_list; } function taxonomy_node_tid_array_by_vocabulary($nid, $voc_id) { $node = new stdClass(); $node->vid = db_result(db_query("SELECT MAX(vid) FROM {node_revisions} WHERE nid = %d", $nid)); $terms = taxonomy_node_get_terms_by_vocabulary($node, $voc_id); $tid_array = array(); foreach($terms as $term) {$tid_array[$term->tid] = $term->tid; } return $tid_array; } function taxonomy_node_get_tid_by_vocabulary($nid, $voc_id) { $node = new stdClass(); $node->vid = db_result(db_query("SELECT MAX(vid) FROM {node_revisions} WHERE nid = %d", $nid)); $terms = taxonomy_node_get_terms_by_vocabulary($node, $voc_id); foreach($terms as $term) {$tid = $term->tid; } return $tid; } ?>