You are here

function taxonomy_xml_add_term_to_batch_queue in Taxonomy import/export via XML 7

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

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

This is required because the current implementation of batch processing isn't actually as atomic as it looks, and it's easy to hit max_allowed_packets just by adding things to the queue.

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.

4 calls to taxonomy_xml_add_term_to_batch_queue()
taxonomy_xml_add_all_children_to_queue in ./taxonomy_xml.process.inc
Queue up an import action.
taxonomy_xml_batch_requeue_more in ./taxonomy_xml.process.inc
Batch callback action that should happen at the end of each round of processing.
taxonomy_xml_import_form_submit in ./taxonomy_xml.admin.inc
Imports the actual XML.
taxonomy_xml_rdf_process_freebase_vocab in formats/rdf_format.inc
FREEBASE only.

File

./taxonomy_xml.process.inc, line 1002
The workhorse processes for importing taxonomies.

Code

function taxonomy_xml_add_term_to_batch_queue($term = NULL) {
  if ($term) {

    // Terms are added to the list queued by their individual uris.
    // This means that if the same term is queued twice, (as with multi-parents)
    // it only gets processed once (per pageload)
    $_SESSION['taxonomy_xml_batch_queue'][$term->guid] = array(
      'taxonomy_xml_import_from_url',
      array(
        $term,
      ),
    );
    watchdog('taxonomy_xml', "Batch Queued %term for import later...", array(
      '%term' => $term->guid,
    ), WATCHDOG_DEBUG);

    // 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'])) {

      // 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('Processed @current out of @total. (May require further recursion)', array()),
        'error_message' => t('Batch Taxonomy Import has encountered an error.'),
        'file' => drupal_get_path('module', 'taxonomy_xml') . '/taxonomy_xml.process.inc',
      );

      // 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. %operations_count operations in this batch . ", array(
        '%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;
    }
  }
}