You are here

function _xmlsitemap_update_cache in XML sitemap 5

Update the cached site map files.

Return value

TRUE if the update was successful, FALSE otherwise.

Related topics

2 calls to _xmlsitemap_update_cache()
_xmlsitemap_output in ./xmlsitemap.module
Menu callback; display the site map.
_xmlsitemap_ping in ./xmlsitemap.module
Submit the site map to search engines.

File

./xmlsitemap.module, line 391
Creates a site map compatible with the sitemaps.org schema.

Code

function _xmlsitemap_update_cache($page = NULL) {
  global $user;
  $current_user = $user;
  $user = user_load(array(
    'uid' => 0,
  ));
  $path = file_directory_path() . '/xmlsitemap';
  file_check_directory($path, FILE_CREATE_DIRECTORY);
  $node_count = db_result(db_query("SELECT COUNT(*) FROM {node}"));
  $dest = $path . '/sitemap.xml.gz';
  $link_count = _xmlsitemap_link_count();
  $chunk_size = variable_get('xmlsitemap_chunk_size', 50000);
  $status = TRUE;
  if ($link_count > $chunk_size) {
    $data = gzencode(_xmlsitemap_output_index($link_count));
    if (file_save_data($data, $dest, FILE_EXISTS_REPLACE) === 0 && ($page == 'index' || !isset($page))) {
      $status = FALSE;
    }
    for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) {
      $dest = $path . "/sitemap{$chunk}.xml.gz";
      $data = gzencode(_xmlsitemap_output_chunk($chunk));
      if (file_save_data($data, $dest, FILE_EXISTS_REPLACE) === 0 && ($page == $chunk || !isset($page))) {
        $status = FALSE;
      }
    }
  }
  else {
    $chunk = 0;
    $data = gzencode(_xmlsitemap_output_chunk($chunk));
    if (file_save_data($data, $dest, FILE_EXISTS_REPLACE) === 0 && ($page == 'index' || !isset($page))) {
      $status = FALSE;
    }
  }
  variable_set('xmlsitemap_chunk_count', $chunk);
  variable_set('xmlsitemap_update', FALSE);
  if (!$status) {
    drupal_set_message(t('Unable to load site map. Make sure that there is an xmlsitemap directory in your files directory and that it is writable by Drupal.'), 'error');
  }
  $user = $current_user;
  return $status;
}