You are here

function taxonomy_xml_add_term_to_batch_queue in Taxonomy import/export via XML 6

Same name and namespace in other branches
  1. 6.2 taxonomy_xml.module \taxonomy_xml_add_term_to_batch_queue()
  2. 7 taxonomy_xml.process.inc \taxonomy_xml_add_term_to_batch_queue()

Manage batch queues by dividing them into recursive 'rounds'.

Given a placeholder term item, make a note that it should be retrieved and analysed when possible.

Parameters

$term a template term object, must include at least a URI that: indicates where the rest of the information should be sourced from.

If no $term is given, this function RETURNS the remaining queue so far, in the form of a batch configuration object that can be batch_set().

Retrieving the queue empties the static list held here, so it can only be done once.

The returned queue will include a rider at the end of the operations that will trigger a recursion if it finds that new terms have been added to this list in the most recent round.

3 calls to taxonomy_xml_add_term_to_batch_queue()
taxonomy_xml_add_all_children_to_queue in ./taxonomy_xml.module
If the currently processing term refers to other terms by URI, set up a process to retrieve them recursively later.
taxonomy_xml_batch_requeue_more in ./taxonomy_xml.module
Batch callback action that should happen at the end of each round of processing.
taxonomy_xml_import_form_submit in ./taxonomy_xml.module
Imports the actual XML.

File

./taxonomy_xml.module, line 1202
taxonomy_xml.module This module makes it possible to import and export taxonomies as XML documents.

Code

function taxonomy_xml_add_term_to_batch_queue($term = NULL) {
  if ($term) {
    $_SESSION['taxonomy_xml_batch_queue'][$term->uri] = array(
      'taxonomy_xml_import_from_url',
      array(
        $term,
      ),
    );

    #drupal_set_message(t("Batch Queued %term for import later...", array('%term' => $term->URI)), 'trace');

    // To avoid overruns, ensure that batches are not too big
    if (count($_SESSION['taxonomy_xml_batch_queue']) >= TAXONOMY_XML_MAX_BATCH_SIZE) {
      batch_set(taxonomy_xml_add_term_to_batch_queue());
    }
  }
  else {

    // Called with no arg,
    // this means we want to return the queue so far, and flush it from here.
    if (!empty($_SESSION['taxonomy_xml_batch_queue'])) {
      $_SESSION['taxonomy_xml_batch_count']++;

      // Prepare a batch config
      $batch_settings = array(
        'finished' => 'taxonomy_xml_batch_import_finished',
        'title' => t('Processing all queued import requests.'),
        'init_message' => t('Starting Batch Taxonomy Import.'),
        'progress_message' => t(' Round %round. Processed @current out of @total. (May require further recursion)', array(
          '%round' => $_SESSION['taxonomy_xml_batch_count'],
        )),
        'error_message' => t('Batch Taxonomy Import has encountered an error.'),
      );

      // Queue up our ops, and flush them from here.
      $batch_settings['operations'] = $_SESSION['taxonomy_xml_batch_queue'];
      unset($_SESSION['taxonomy_xml_batch_queue']);
      drupal_set_message(t("Retrieving the next batch queue, batch %batch_number. %operations_count operations in this batch . ", array(
        '%batch_number' => $_SESSION['taxonomy_xml_batch_count'],
        '%operations_count' => count($batch_settings['operations']),
      )));

      // The last thing each round should do is queue up the next round. Add this callback to the operations.
      $batch_settings['operations']['final'] = array(
        'taxonomy_xml_batch_requeue_more',
        array(),
      );
      return $batch_settings;
    }
    else {

      // If the queue is empty, return NULL so the caller won't get confused by an empty batch
      return NULL;
    }
  }
}