You are here

function node_export_import in Node export 7.3

Same name and namespace in other branches
  1. 6.3 node_export.module \node_export_import()
  2. 6.2 node_export.module \node_export_import()

Import Function

Parameters

$code_string: The string of export code.

$msg_t: Function used to translate.

$save: When TRUE will save the nodes that are imported.

Return value

An array with keys 'success' which is a boolean value representing whether the import was successful and 'output' which contains an array of translated strings to be shown to the user as messages.

5 calls to node_export_import()
drush_node_export_callback_import in ./node_export.drush.inc
Drush command callback.
FeedsNodeExportParser::parse in modules/node_export_feeds/FeedsNodeExportParser.inc
Implements FeedsParser::parse().
NodeExportXMLTestCase::testNodeExportXMLExportImport in ./node_export.test
Test XML export and import.
node_export_features_features_rebuild in modules/node_export_features/node_export_features.module
Implements hook_features_rebuild().
node_export_import_form_submit in ./node_export.pages.inc
Submit function for import form.

File

./node_export.module, line 490
The Node export module.

Code

function node_export_import($code_string, $msg_t = 't', $save = TRUE) {

  // Early check to avoid letting hooligans and the elderly pass data to the
  // eval() function call.
  if (!node_export_access_import()) {
    $error = $msg_t('You do not have permission to import any nodes.');
    return array(
      'success' => FALSE,
      'output' => array(
        $error,
      ),
    );
  }

  // Allow modules to manipulate the $code_string.
  drupal_alter('node_export_decode', $code_string);

  // Pass the string to each format handler until one returns something useful.
  $format_handlers = node_export_format_handlers();
  $nodes = array();
  $used_format = "";
  foreach ($format_handlers as $format_handler => $format) {
    if (!empty($format['#file']) && is_file($format['#file'])) {
      require_once $format['#file'];
    }
    $nodes = call_user_func($format['#import_callback'], $code_string);
    if (!empty($nodes)) {
      $used_format = $format_handler;
      break;
    }
  }
  if (isset($nodes['success']) && !$nodes['success']) {

    // Instead of returning nodes, the format handler returned an error array.
    // Translate the errors and return them.
    foreach ($nodes['output'] as $key => $output) {
      $nodes['output'][$key] = $msg_t($output);
    }
    return array(
      'success' => FALSE,
      'output' => $nodes['output'],
    );
  }
  if ($used_format == "") {
    $error = $msg_t('Node export was unable to recognize the format of the supplied code.  No nodes imported.');
    return array(
      'success' => FALSE,
      'output' => array(
        $error,
      ),
    );
  }
  $nodes = node_export_restore_recursion($nodes);
  $types_exist = node_export_import_types_check($nodes);
  if ($types_exist !== TRUE) {

    // There was a problem with the content types check.
    $error = $msg_t('Error encountered during import.  Node types unknown on this site: %t.  No nodes imported.', array(
      '%t' => implode(", ", $types_exist),
    ));
    return array(
      'success' => FALSE,
      'output' => array(
        $error,
      ),
    );
  }
  if (!node_export_access_import_nodes($nodes)) {

    // There was a problem with permissions.
    $error = $msg_t('You do not have permission to perform a Node export: import on one or more of these nodes.  No nodes imported.');
    return array(
      'success' => FALSE,
      'output' => array(
        $error,
      ),
    );
  }
  $count = 0;
  $total = count($nodes);

  // Let other modules do special fixing up.
  drupal_alter('node_export_import', $nodes, $used_format, $save);
  $new_nodes = array();
  $messages = array();
  foreach ($nodes as $original_node) {
    $node = node_export_node_clone($original_node);
    $tnid_to_reset = node_export_node_clone_set_tnid($node, $original_node);

    // Import file fields.
    node_export_file_field_import($node, $original_node);

    // Handle existing nodes.
    $save_node = $save;
    $nids = entity_get_id_by_uuid('node', array(
      $node->uuid,
    ));
    if (!empty($nids[$node->uuid])) {
      $existing = variable_get('node_export_existing', 'new');
      switch ($existing) {
        case 'new':
          $node->is_new = TRUE;
          unset($node->uuid);
          break;
        case 'revision':
          $node->nid = $nids[$node->uuid];
          $node->is_new = FALSE;
          $node->revision = 1;
          $tnid_to_reset = 0;
          break;
        case 'skip':
          $save_node = FALSE;
          break;
      }
    }

    // Let other modules do special fixing up.
    drupal_alter('node_export_node_import', $node, $original_node, $save_node);
    if ($save_node) {
      if (!empty($tnid_to_reset) && empty($node->tnid)) {
        node_export_save($node);
        $node->tnid = $node->nid;

        // Update the tnid map for other nodes referencing this tnid.
        $tnid_map =& drupal_static('node_export_import_tnid_map', array());
        $tnid_map[$tnid_to_reset] = $node->tnid;
      }
      node_export_save($node);
      $new_nodes[$node->nid] = $node;
      $messages[] = $msg_t("Imported node !nid: !node", array(
        '!nid' => $node->nid,
        '!node' => l($node->title, 'node/' . $node->nid),
      ));
      $count++;
    }
    else {
      $new_nodes[] = $node;
    }
  }
  if ($count) {
    drupal_alter('node_export_after_import', $new_nodes, $used_format, $save);
    $messages[] = $msg_t("!count of !total nodes were imported.  Some values may have been reset depending on Node export's configuration.", array(
      '!total' => $total,
      '!count' => $count,
    ));

    // Clear the page and block caches.
    cache_clear_all();

    // Nodes were saved, so return the nids.
    return array(
      'success' => TRUE,
      'output' => $messages,
      'nids' => array_keys($new_nodes),
      'format' => $used_format,
    );
  }
  else {

    // We didn't save, so return full nodes.
    return array(
      'success' => TRUE,
      'output' => $messages,
      'nodes' => $new_nodes,
      'format' => $used_format,
    );
  }
}