public function TripalEntityCollection::write

3.x TripalEntityCollection.inc public TripalEntityCollection::write($formatter = NULL, TripalJob $job = NULL)

Writes the collection to a file using a given formatter.

Parameters

formatter: The name of the formatter class to use (e.g. TripalTabDownloader). The formatter must be compatible with the data collection. If no formatter is supplied then all file formats supported by this data collection will be created.

$job: If this function is run as a Tripal Job then this argument can be set to the Tripaljob object for keeping track of progress.

Throws

Exception

File

tripal/includes/TripalEntityCollection.inc, line 485

Class

TripalEntityCollection

Code

public function write($formatter = NULL, TripalJob $job = NULL) {

  // Initialize the downloader classes and initialize the files for writing.
  $formatters = array();
  foreach ($this->formatters as $class => $label) {
    if (!$this->isFormatterCompatible($class)) {
      throw new Exception(t('The formatter, "@formatter", is not compatible with this data collection.', array('@formatter' => $formatter)));
    }
    if (!tripal_load_include_downloader_class($class)) {
      throw new Exception(t('Cannot find the formatter named "@formatter".', array('@formatter', $formatter)));
    }
    $outfile = $this->getOutfile($class);
    if (!$formatter or ($formatter == $class)) {
      $formatters[$class] = new $class($this->collection_id, $outfile);
      $formatters[$class]->writeInit($job);
      if ($job) {
        $job->logMessage("Writing " . lcfirst($class::$full_label) . " file.");
      }
    }
  }

  // Count the total number of entities
  $total_entities = 0;
  $bundle_collections = $this->collection_bundles;
  foreach ($this->bundles as $bundle) {
    $bundle_name = $bundle->bundle_name;
    $entity_ids = $this->getEntityIDs($bundle_name);
    $total_entities += count($entity_ids);
  }
  if ($job) {
    $job->setTotalItems($total_entities);
  }

  // Iterate through the bundles in this collection and get the entities.
  foreach ($this->bundles as $bundle) {
    $bundle_name = $bundle->bundle_name;
    $site_id = $bundle->site_id;
    $entity_ids = array_unique($this->getEntityIDs($bundle_name));
    $field_ids = array_unique($this->getFieldIDs($bundle_name));

    // Clear any cached @context or API docs.
    if ($site_id and module_exists('tripal_ws')) {
      tripal_clear_remote_cache($site_id);
    }

    // We want to load entities in batches to speed up performance.
    $num_eids = count($entity_ids);
    $bundle_eids_handled = 0;
    $slice_size = 100;
    while ($bundle_eids_handled < $num_eids) {
      // Get a bantch of $slice_size elements from the entities array.
      $slice = array_slice($entity_ids, $bundle_eids_handled, $slice_size);
      if ($job) {
        $job->logMessage('Getting entities for ids !start to !end of !total', 
        array('!start' => $bundle_eids_handled,
          '!end' => $bundle_eids_handled + count($slice),
          '!total' => $num_eids));
      }
      $bundle_eids_handled += count($slice);

      // If the bundle is from a remote site then call the appropriate
      // function, otherwise, call the local function.
      if ($site_id and module_exists('tripal_ws')) {
        $entities = tripal_load_remote_entities($slice, $site_id, $bundle_name, $field_ids);
      }
      else {
        $entities = tripal_load_entity('TripalEntity', $slice, FALSE, $field_ids, FALSE);
      }
      $job->logMessage('Got !count entities.', array('!count' => count($entities)));

      // Now write each entity one at a time to the files.
      foreach ($entities as $entity_id => $entity) {

        // Write the same entity to all the formatters that are supported.
        foreach ($formatters as $class => $formatter) {
          //if ($class == 'TripalTabDownloader') {
          $formatter->writeEntity($entity, $job);
          //}
        }
      }
      if ($job) {
        $job->setItemsHandled($num_handled + count($slice));
      }
    }
  }

  // Now close up all the files
  foreach ($formatters as $class => $formatter) {
    $formatter->writeDone($job);
  }
}