You are here

function data_export_import_import_all_content_types_using_drush in Data export import 7

Same name and namespace in other branches
  1. 6 includes/profiles/all_content_types.inc \data_export_import_import_all_content_types_using_drush()

Will import a dataset file.

This function is specifically set up to be called by drush.

Parameters

string $file: The dataset file which is being imported.

Return value

bool TRUE if the import process ran without errors.

1 call to data_export_import_import_all_content_types_using_drush()
drush_data_export_import_import in includes/data_export_import.drush.inc
A drush command callback. This is where the action takes place.

File

includes/profiles/nodes.inc, line 676
Enables nodes to be exported and imported.

Code

function data_export_import_import_all_content_types_using_drush($file) {

  // Read the first line - decode the object and check that the content types
  // are an exact match.
  $file_path = variable_get('file_public_path', conf_path() . '/files') . "/data_export_import/nodes/" . $file;
  $handle = fopen($file_path, "r");
  if ($handle) {
    $content_type_line_from_file = fgets($handle);
    fclose($handle);
  }
  $node_content_type_object_from_file = unserialize(base64_decode($content_type_line_from_file));

  // Check that the content types exist and match.
  $node_content_type = $node_content_type_object_from_file->type;
  node_types_rebuild();
  $node_types = node_type_get_types();

  // Check that the content type exists in the receiving instance.
  if (is_null($node_types[$node_content_type])) {
    drupal_set_message(t("The content type of the content being imported does not exist in the receiving site for content type of: %content_type", array(
      '%content_type' => $node_content_type,
    )), 'error');
    return FALSE;
  }

  // Here we are going to unset the description fields.  If the
  // description has been changed but everything else about the
  // content type is the same then we will assume that this is just an
  // edit to the description.
  unset($node_content_type_object_from_file->description);
  unset($node_types[$node_content_type]->description);
  if ($node_content_type_object_from_file != $node_types[$node_content_type]) {
    drupal_set_message(t("When the dataset was created the node content type was different from this site's node content type. Please manually compare the content type: %content_type", array(
      '%content_type' => $node_content_type,
    )), 'error');
    return;
  }

  // Loop through all the lines in the file and put the nid's into an array.
  // Then loop through all the current nodes (in the receiving instance)
  // and check the nid value.  If it doesn't match an nid from the dataset file
  // then delete the existing node in the receiving instance.  This will delete
  // test nodes which have been created.
  $line_number = 0;
  $handle = fopen($file_path, "r");
  if ($handle) {
    while (($buffer = fgets($handle)) !== FALSE) {
      $line_number++;
      if ($line_number > 1) {

        // @todo Here we will need to unencode the line extracted.
        $node_from_file = unserialize(base64_decode($buffer));
        $nids_from_file[] = $node_from_file->nid;
      }
    }
    if (!feof($handle)) {
      drupal_set_message(t("Unexpected error when reading file."), 'error');
    }
    fclose($handle);
  }

  // Loop through the existing nodes (for that content type) and if
  // they don't exist in the dataset then delete them. NB - We've
  // used n.nid here as the db_rewrite_sql function will call a
  // link to access/grants tables and this could result in an
  // ambiguous field called 'nid'.
  $results = db_query('SELECT nid FROM {node} WHERE type = :type', array(
    ':type' => $node_content_type,
  ));
  foreach ($results as $row) {
    if (!in_array($row->nid, $nids_from_file)) {

      // Delete node as it does not exist in the dataset being
      // imported.
      node_delete($row->nid);
    }
  }

  // Loop through all the lines in the dataset file and extract each
  // node line. If the receiving instance has a node with a matching
  // nid then update it - or if not then create a new node with the
  // same nid. We will use a batch function to process the lines from
  // the file.
  $fields_of_type_file = array();
  $fields_info = field_info_instances('node', $node_content_type);
  foreach ($fields_info as $field_name => $field_value) {
    $field_info = field_info_field($field_name);
    $type = $field_info['type'];
    if ($type == 'file' || $type == 'image') {
      $fields_of_type_file[] = $field_name;
    }
  }
  $batch = array(
    'operations' => array(
      array(
        'data_export_import_batch_import_dataset_lines',
        array(
          $file_path,
          $fields_of_type_file,
        ),
      ),
    ),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('Batch importing of nodes from file has encountered an error.'),
    'file' => drupal_get_path('module', 'data_export_import') . '/includes/profiles/nodes.inc',
  );
  $batch['progressive'] = FALSE;
  batch_set($batch);

  // Since this is not called from a file then we need to call the
  // batch process function.
  drush_backend_batch_process();
  return TRUE;
}