You are here

function xmlsitemap_output in XML sitemap 6

Same name and namespace in other branches
  1. 5.2 xmlsitemap/xmlsitemap.module \xmlsitemap_output()

Menu callback; display the sitemap.

Parameters

$chunk: An integer specifying which chunk of the sitemap is being requested. If not set and there is more than one chunk, display the sitemap index.

1 string reference to 'xmlsitemap_output'
xmlsitemap_menu in ./xmlsitemap.module
Implementation of hook_menu().

File

./xmlsitemap.pages.inc, line 25
XML sitemap page callbacks.

Code

function xmlsitemap_output($chunk = NULL) {
  global $user;
  $chunk_size = variable_get('xmlsitemap_chunk_size', XMLSITEMAP_DEFAULT_SITEMAP_LINKS);
  $link_count = xmlsitemap_link_count();
  $chunk_count = xmlsitemap_chunk_count();

  // A sitemap may only have a set number of index links (links to other sitemap
  // files) and a set number of site links in each of the "other" sitemap files.
  // This code adjusts the number of site links specified by the user if the
  // set number of index links has been reached but will not set that number
  // greater than the set number of site links in a file.
  if ($chunk_count > XMLSITEMAP_MAX_SITEMAP_INDEX_LINKS && $chunk_size < XMLSITEMAP_MAX_SITEMAP_LINKS) {

    // Determine the number of chunks we are over the maximum index links.
    $chunk_adjust = $chunk_count - XMLSITEMAP_MAX_SITEMAP_INDEX_LINKS;

    // Determine the number of links to adjust the chunk size.
    $chunk_adjust = $chunk_adjust * $chunk_size;
    if ($chunk_size + $chunk_adjust <= XMLSITEMAP_MAX_SITEMAP_LINKS) {
      $chunk_size += $chunk_adjust;
    }
    else {
      $chunk_size = XMLSITEMAP_MAX_SITEMAP_LINKS;
    }
    variable_set('xmlsitemap_chunk_size', $chunk_size);
    watchdog('xmlsitemap', 'Chunk size has been updated to @chunk-size.', array(
      '@chunk-size' => $chunk_size,
    ));
  }
  elseif ($chunk_count > XMLSITEMAP_MAX_SITEMAP_INDEX_LINKS) {
    watchdog('xmlsitemap', 'The maximum number of allowed links has been reached.', NULL, WATCHDOG_ERROR);
  }
  if (isset($chunk) && !is_numeric($chunk)) {
    drupal_not_found();
    exit;
  }
  $id = xmlsitemap_language_id();
  if (variable_get('xmlsitemap_sitemap_needs_update', FALSE)) {
    variable_set('xmlsitemap_update_timestamp', REQUEST_TIME);
    db_query("DELETE FROM {xmlsitemap} WHERE type ='frontpage'");
    $row = new stdClass();
    $row->module = 'xmlsitemap';
    $row->type = 'frontpage';
    $changefreq = variable_get('xmlsitemap_front_page_changefreq', 3600);
    $row->changed = REQUEST_TIME - $changefreq;
    $row->changefreq = $changefreq;
    $row->priority = variable_get('xmlsitemap_front_page_priority', 1);
    drupal_write_record('xmlsitemap', $row);
    module_invoke_all('xmlsitemap_links');
    variable_set('xmlsitemap_sitemap_needs_update', FALSE);
    $result = _xmlsitemap_create_cache_files();
    if (variable_get("xmlsitemap_create_cache_result_{$id}", -1) !== $result) {
      variable_set("xmlsitemap_create_cache_result_{$id}", $result);
    }
  }
  elseif (_xmlsitemap_check_cache_files()) {
    $result = _xmlsitemap_create_cache_files();
    if (variable_get("xmlsitemap_create_cache_result_{$id}", -1) !== $result) {
      variable_set("xmlsitemap_create_cache_result_{$id}", $result);
    }
  }
  if (!isset($chunk)) {
    if (($chunks = (int) $link_count / $chunk_size) != variable_get('xmlsitemap_previous_chunks_count', -1)) {
      variable_set('xmlsitemap_previous_chunks_count', $chunks);
      if (!variable_get('menu_rebuild_needed', FALSE)) {
        variable_set('menu_rebuild_needed', TRUE);
      }
    }
  }
  if (isset($result) && !$result) {
    drupal_not_found();
    exit;
  }
  $parent_directory = variable_get('xmlsitemap_cache_directory', file_directory_path() . '/xmlsitemap');
  $headers = array(
    "Content-type: text/xml; charset=utf-8",
  );
  if (isset($chunk)) {
    if ($chunk < $link_count / $chunk_size) {
      file_transfer("{$parent_directory}/xsm-{$id}-{$chunk}.xml", $headers);
    }
  }
  else {
    file_transfer("{$parent_directory}/xsm-{$id}.xml", $headers);
  }
}