| 1 |
<?php |
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* An example use case for the import API written to import content from an |
| 7 |
|
* existing Drupal installation. Pulls in one cck field. |
| 8 |
|
* |
| 9 |
|
* In lieu of a second data source we will be pulling from an internal test |
| 10 |
|
* table. A common scenario for imports would involve a second database. |
| 11 |
|
* Comments below illustrate where you would normally switch datasources. |
| 12 |
|
*/ |
| 13 |
|
|
| 14 |
|
/** |
| 15 |
|
* Implementation of hook_import_stage() |
| 16 |
|
* |
| 17 |
|
*/ |
| 18 |
|
function import_node_import_stage() { |
| 19 |
|
|
| 20 |
|
// The import_node_staged variable is set to false during the installation of |
| 21 |
|
// this module. |
| 22 |
|
if (variable_get('import_node_staged', FALSE) == FALSE) { |
| 23 |
|
|
| 24 |
|
// select all of the old ids we will be importing. If you were importing |
| 25 |
|
// from an external datasource you would use db_set_active() to switch |
| 26 |
|
// before running the select statement. |
| 27 |
|
|
| 28 |
|
$sql = "SELECT id from {import_example_node}"; |
| 29 |
|
$result = db_query($sql); |
| 30 |
|
|
| 31 |
|
while ($content = db_fetch_object($result)) { |
| 32 |
|
|
| 33 |
|
// create an array containing identifier and type both of which are used in |
| 34 |
|
// this modules process hook. The type is an arbitrary identifer the |
| 35 |
|
// developer can use to easily identify which type of data is referenced. |
| 36 |
|
// The type chosen by the developer will get passed to this modules |
| 37 |
|
// implementation of hook_import_process. |
| 38 |
|
|
| 39 |
|
$attr = array(); |
| 40 |
|
$attr['impid'] = $content->id; |
| 41 |
|
$attr['type'] = 'node'; |
| 42 |
|
|
| 43 |
|
// pass the array to the import_stage method |
| 44 |
|
|
| 45 |
|
import_stage($attr); |
| 46 |
|
} |
| 47 |
|
|
| 48 |
|
// set this variable to true to eliminate restaging the data |
| 49 |
|
variable_set('import_node_staged', TRUE); |
| 50 |
|
} |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
/** |
| 54 |
|
* Implementation hook_import_process() |
| 55 |
|
*/ |
| 56 |
function import_node_import_process($data) { |
function import_node_import_process($data) { |
| 57 |
|
switch ($data->type) { |
| 58 |
|
case 'node': |
| 59 |
|
$new_node = _import_node_map_data($data); |
| 60 |
|
node_save($new_node); |
| 61 |
|
|
| 62 |
|
import_pass($data, 'Node Import passed'. print_r($node, TRUE)); |
| 63 |
|
break; |
| 64 |
|
} |
| 65 |
|
} |
| 66 |
|
|
| 67 |
|
/** |
| 68 |
|
* Helper function to map the old data to a Drupal user object; |
| 69 |
|
*/ |
| 70 |
|
function _import_node_map_data($data) { |
| 71 |
|
global $user; |
| 72 |
|
// First we need to select the old record we are dealing with. Normally this |
| 73 |
|
// would be done by using db_set_active() to switch datasources. |
| 74 |
|
$sql_old_content = "SELECT id, headline, body, author FROM {import_example_node} WHERE id = %d"; |
| 75 |
|
$old_content = db_fetch_object(db_query($sql_old_content, $data->impid)); |
| 76 |
|
|
| 77 |
|
$node->uid = $user->uid; |
| 78 |
|
$node->type = 'example_node'; |
| 79 |
|
$node->status = 1; |
| 80 |
|
$node->comment = 0; |
| 81 |
|
$node->promote = 0; |
| 82 |
|
$node->moderate = 0; |
| 83 |
|
$node->sticky = 0; |
| 84 |
|
$node->title = $old_content->headline; |
| 85 |
|
$node->body = $old_content->body; |
| 86 |
|
$node->field_original_author = array(0 => array('value' => $old_content->author)); |
| 87 |
|
return $node; |
| 88 |
} |
} |