You are here

function xmlsitemap_rebuild_batch_fetch in XML sitemap 6.2

Same name and namespace in other branches
  1. 8 xmlsitemap.module \xmlsitemap_rebuild_batch_fetch()
  2. 7.2 xmlsitemap.generate.inc \xmlsitemap_rebuild_batch_fetch()
  3. 2.x xmlsitemap.module \xmlsitemap_rebuild_batch_fetch()

Batch callback; fetch and add the sitemap links for a specific entity.

1 string reference to 'xmlsitemap_rebuild_batch_fetch'
xmlsitemap_get_link_info in ./xmlsitemap.module
Returns information about supported sitemap link types.

File

./xmlsitemap.generate.inc, line 475
Sitemap generation and rebuilding functions for the xmlsitemap module.

Code

function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
  if (!isset($context['sandbox']['info'])) {
    $context['sandbox']['info'] = xmlsitemap_get_link_info($entity);
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['last_id'] = 0;
  }
  $info = $context['sandbox']['info'];

  // Build the generic query.
  $base_table = db_escape_table($info['base table']);
  $id_key = db_escape_string($info['entity keys']['id']);
  $query = $args = $ids = array();
  $query['SELECT'] = "SELECT {$id_key}";
  $query['FROM'] = "FROM {{$base_table}}";
  $query['WHERE'] = "WHERE {$id_key} > %d";
  $args[] = $context['sandbox']['last_id'];
  if (!empty($info['entity keys']['bundle'])) {
    $bundle_key = db_escape_string($info['entity keys']['bundle']);
    $bundle_type = _xmlsitemap_get_field_type($info['base table'], $info['entity keys']['bundle']);
    $bundles = xmlsitemap_get_link_type_enabled_bundles($entity);
    $query['WHERE'] .= " AND {$bundle_key} IN (" . db_placeholders($bundles, $bundle_type) . ")";
    $args = array_merge($args, $bundles);
  }
  if (!isset($context['sandbox']['max'])) {
    $sql = implode(' ', $query);
    $sql = str_replace("SELECT {$id_key}", "SELECT COUNT({$id_key})", $sql);
    $context['sandbox']['max'] = (int) db_result(db_query($sql, $args));
    if (!$context['sandbox']['max']) {

      // If there are no items to process, skip everything else.
      return;
    }
  }

  // PostgreSQL cannot have the ORDERED BY in the count query.
  $query['ORDER BY'] = "ORDER BY {$id_key}";
  $limit = 20;

  //variable_get('xmlsitemap_batch_limit', 100)
  $sql = implode(' ', $query);
  $query = db_query_range($sql, $args, 0, $limit);
  $ids = xmlsitemap_db_fetch_col($query);
  $info['xmlsitemap']['process callback']($ids);
  $context['sandbox']['last_id'] = end($ids);
  $context['sandbox']['progress'] += count($ids);
  $context['message'] = t('Now processing %entity @last_id (@progress of @count).', array(
    '%entity' => $entity,
    '@last_id' => $context['sandbox']['last_id'],
    '@progress' => $context['sandbox']['progress'],
    '@count' => $context['sandbox']['max'],
  ));
  if ($context['sandbox']['progress'] >= $context['sandbox']['max']) {
    $context['finished'] = 1;
  }
  else {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}