You are here

function _xmlsitemap_create_cache_files in XML sitemap 6

Create the cache files containing the sitemap.

Return value

TRUE if the operation has been successfull, FALSE otherwise.

1 call to _xmlsitemap_create_cache_files()
xmlsitemap_output in ./xmlsitemap.pages.inc
Menu callback; display the sitemap.

File

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

Code

function _xmlsitemap_create_cache_files() {
  $chunk_size = variable_get('xmlsitemap_chunk_size', 1000);
  $link_count = xmlsitemap_link_count();
  $id = xmlsitemap_language_id();
  $parent_directory = variable_get('xmlsitemap_cache_directory', file_directory_path() . '/xmlsitemap');

  // If the directory doesn't exist, then create it.
  if (!file_check_directory($parent_directory, FILE_CREATE_DIRECTORY)) {
    return TRUE;
  }
  if ($link_count > $chunk_size) {
    if (!($fp = fopen($parent_directory . "/xsm-{$id}.xml", 'wb'))) {
      watchdog('xmlsitemap', 'Could not create the cache file @file.', array(
        '@file' => $parent_directory . "/xsm-{$id}.xml",
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    fwrite($fp, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
    if (variable_get('xmlsitemap_use_stylesheet', FALSE)) {
      fwrite($fp, '<?xml-stylesheet type="text/xsl" href="' . url('sitemap.xsl') . '" ?>' . "\n");
    }
    fwrite($fp, '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"' . "\n");
    fwrite($fp, '  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n");
    fwrite($fp, '  xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9' . "\n");
    fwrite($fp, '  http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd">' . "\n");
    for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) {
      fwrite($fp, '  <sitemap>' . "\n");
      fwrite($fp, '    <loc>' . url("sitemap{$chunk}.xml", array(
        'absolute' => TRUE,
      )) . '</loc>' . "\n");
      $from = $chunk * $chunk_size;
      $changed = db_result(db_query_range("SELECT xsm.changed" . xmlsitemap_sitemap_query() . "ORDER BY xsm.priority DESC, xsm.changed DESC", $from, $chunk_size));
      fwrite($fp, '    <lastmod>' . gmdate(DATE_W3C, $changed) . '</lastmod>' . "\n");
      fwrite($fp, '  </sitemap>' . "\n");
    }
    fwrite($fp, '</sitemapindex>');
    fclose($fp);

    // Set standard file permissions for webserver-generated files.
    @chmod($parent_directory . "/xsm-{$id}.xml", 0664);
    for ($chunk = 0; $chunk < $link_count / $chunk_size; ++$chunk) {
      if (!($fp = fopen($parent_directory . "/xsm-{$id}-{$chunk}.xml", 'wb'))) {
        watchdog('xmlsitemap', 'Could not create the cache file @file.', array(
          '@file' => $parent_directory . "/xsm-{$id}-{$chunk}.xml",
        ), WATCHDOG_ERROR);
        return FALSE;
      }
      _xmlsitemap_create_cache_chunk($fp, $chunk_size, $chunk);
      fclose($fp);

      // Set standard file permissions for webserver-generated files.
      @chmod($parent_directory . "/xsm-{$id}-{$chunk}.xml", 0664);
    }
  }
  else {
    if (!($fp = fopen($parent_directory . "/xsm-{$id}.xml", 'wb'))) {
      watchdog('xmlsitemap', 'Could not create the cache file @file.', array(
        '@file' => $parent_directory . "/xsm-{$id}.xml",
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    _xmlsitemap_create_cache_chunk($fp, $chunk_size);
    fclose($fp);

    // Set standard file permissions for webserver-generated files.
    @chmod($parent_directory . "/xsm-{$id}.xml", 0664);
  }
  return TRUE;
}