You are here

function data_export_import_batch_export_nodes_to_file in Data export import 7

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

Batch function called to export the content type.

Parameters

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

string $file_path_and_name: The path and filename to use for the dataset file created.

Return value

bool TRUE if all ran OK.

2 string references to 'data_export_import_batch_export_nodes_to_file'
data_export_import_export_nodes_to_file in includes/profiles/nodes.inc
Export the required dataset files.
data_export_import_export_nodes_to_file_using_drush in includes/profiles/nodes.inc
Export the required dataset files via drush.

File

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

Code

function data_export_import_batch_export_nodes_to_file($content_type, $file_path_and_name, $fields_of_type_file, &$context) {
  if (!isset($context['sandbox']['progress'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_node'] = 0;
    $context['sandbox']['max'] = db_query("SELECT COUNT(DISTINCT nid) FROM {node} WHERE type = :content_type", array(
      ':content_type' => $content_type,
    ))
      ->fetchField();
  }

  // We will set this to make one pass at a time to limit timeouts.
  $limit = 1;
  $result = db_query_range("SELECT nid FROM {node} WHERE nid > :current_node AND type = :content_type ORDER BY nid ASC", 0, $limit, array(
    ':current_node' => $context['sandbox']['current_node'],
    ':content_type' => $content_type,
  ));

  // The new D7 syntax returns an object not an array.
  foreach ($result as $row) {

    // Output one node to the file.
    $node = node_load($row->nid, NULL, TRUE);
    foreach ($fields_of_type_file as $file_field) {
      $file_field_items = field_get_items('node', $node, $file_field);
      if ($file_field_items) {
        foreach ($file_field_items as $field_item_key => $field_item_value) {
          $handle = fopen($field_item_value['uri'], 'r');
          $file_data = base64_encode(stream_get_contents($handle));
          $node->{$file_field}[LANGUAGE_NONE][$field_item_key]['data_export_import_file_data'] = $file_data;
        }
      }
    }

    // Here we will get the comments for a node and attach them to the node
    // object.
    if (module_exists('comment')) {
      $comments_result = db_query("SELECT cid FROM {comment} WHERE nid = :comment_node ORDER BY cid ASC", array(
        ':comment_node' => $row->nid,
      ));
      foreach ($comments_result as $comment_row) {
        $whole_comment = comment_load($comment_row->cid, TRUE);
        $node->comments[] = $whole_comment;
      }
    }

    // Here we will serialize the array to convert it to a string
    // which can then be output to a file.
    $node_serialized = serialize($node);

    // Encode the string to make sure the data does not contain line
    // endings and other characters which may cause problems when
    // reading the file during import.
    $node_serialized_and_base64_encoded = base64_encode($node_serialized);
    file_put_contents($file_path_and_name, $node_serialized_and_base64_encoded . "\n", FILE_APPEND | LOCK_EX);

    // Update our progress information.
    $context['sandbox']['progress']++;
    $context['sandbox']['current_node'] = $node->nid;
  }

  // Store some result for post-processing in the finished callback.
  // We will use a trick by setting the key to be the file path so
  // this array value will be set again and again.  This way the results
  // array will not get larger and larger for each record which is processed
  // but will hold a single result for this batch.
  $context['results'][$file_path_and_name] = basename($file_path_and_name);

  // Inform the batch engine that we are not finished, and provide an
  // estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}