Commit c31a4d93 authored by Dave Reid's avatar Dave Reid

by Dave Reid: Cleaned up the sitemap output code.

parent 0825a45f
......@@ -58,11 +58,13 @@ function xmlsitemap_sitemap_list_form() {
$options = array();
$destination = drupal_get_destination();
foreach ($sitemaps as $smid => $sitemap) {
$sitemap['url'] = url($sitemap['uri']['path'], $sitemap['uri']['options']);
$options[$smid]['url'] = array(
'data' => array(
'#type' => 'link',
'#title' => $sitemap['url'],
'#href' => $sitemap['url'],
'#href' => $sitemap['url'],
),
);
......
......@@ -154,7 +154,7 @@ function xmlsitemap_generate(array $sitemap, $chunk) {
return FALSE;
}
$file = xmlsitemap_get_file_from_context($sitemap['context'], $chunk);
$file = xmlsitemap_get_sitemap_file($sitemap, $chunk);
if (!$handle = fopen($file, 'wb')) {
trigger_error(t('Could not open file @file for writing.', array('@file' => $file)));
......
......@@ -124,7 +124,6 @@ function xmlsitemap_sitemap_load_multiple(array $smids) {
foreach ($sitemaps as $smid => $sitemap) {
$sitemaps[$smid]['context'] = unserialize($sitemap['context']);
$sitemaps[$smid]['uri'] = xmlsitemap_sitemap_uri($sitemaps[$smid]);
$sitemaps[$smid]['url'] = url($sitemaps[$smid]['uri']['path'], $sitemaps[$smid]['uri']['options']);
}
return $sitemaps;
}
......@@ -165,6 +164,7 @@ function xmlsitemap_sitemap_uri(array $sitemap) {
return $uri;
}
// @todo Save $sitemap['url']?
function xmlsitemap_sitemap_save(array $sitemap) {
asort($sitemap['context']);
$sitemap['context_hash'] = md5(serialize($sitemap['context']));
......@@ -295,7 +295,7 @@ function xmlsitemap_cron() {
*/
function xmlsitemap_robotstxt() {
if ($sitemap = xmlsitemap_sitemap_load_by_context()) {
$robotstxt[] = 'Sitemap: ' . $sitemap['url'];
$robotstxt[] = 'Sitemap: ' . url($sitemap['uri']['path'], $sitemap['uri']['options']);
return $robotstxt;
}
}
......@@ -874,8 +874,16 @@ function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
return $link_info;
}
function xmlsitemap_get_file_from_context(array $context, $chunk = 'index') {
return xmlsitemap_get_directory() . '/' . md5(serialize($context)) . "-{$chunk}.xml";
/**
* Return the expected file path for a specific sitemap chunk.
*
* @param $sitemap
* An XML sitemap array.
* @param $chunk
* An optional specific chunk in the sitemap. Defaults to the index page.
*/
function xmlsitemap_get_sitemap_file(array $sitemap, $chunk = 'index') {
return xmlsitemap_get_directory() . '/' . $sitemap['context_hash'] . "-{$chunk}.xml";
}
function xmlsitemap_get_directory() {
......
......@@ -11,11 +11,10 @@
/**
* Get the sitemap chunk/page of the current request.
*/
function xmlsitemap_get_current_chunk(array $context) {
function xmlsitemap_get_current_chunk(array $sitemap) {
// Check if we should be displaing the index.
if (!isset($_GET['page']) || !is_numeric($_GET['page'])) {
$index_file = xmlsitemap_get_file_from_context($context, 'index');
if (file_exists($index_file)) {
if ($sitemap['chunks'] > 1) {
return 'index';
}
else {
......@@ -30,21 +29,25 @@ function xmlsitemap_get_current_chunk(array $context) {
/**
* Output a sitemap page.
*
* @see xmlsitemap_get_current_context()
* @see xmlsitemap_sitemap_load_by_context()
* @see xmlsitemap_get_current_chunk()
* @see xmlsitemap_get_file_from_context()
* @see xmlsitemap_get_sitemap_file()
* @see xmlsitemap_output_file()
*/
function xmlsitemap_output_chunk() {
$context = xmlsitemap_get_current_context();
$chunk = xmlsitemap_get_current_chunk($context);
$file = xmlsitemap_get_file_from_context($context, $chunk);
$sitemap = xmlsitemap_sitemap_load_by_context();
if (!$sitemap) {
return MENU_NOT_FOUND;
}
$chunk = xmlsitemap_get_current_chunk($sitemap);
$file = xmlsitemap_get_sitemap_file($sitemap, $chunk);
// Provide debugging information if enabled.
if (variable_get('xmlsitemap_developer_mode', 0) || isset($_GET['debug'])) {
$output = array();
$output[] = "Sitemap: " . print_r($sitemap, TRUE);
$output[] = "Chunk: $chunk";
$output[] = "Context: " . print_r($context, TRUE);
$output[] = "Cache file location: $file";
$output[] = "Cache file exists: " . (file_exists($file) ? 'Yes' : 'No');
return implode('<br />', $output);
......
......@@ -140,6 +140,7 @@ function xmlsitemap_engines_submit_engines(array $smids = array()) {
*/
function xmlsitemap_engines_submit_sitemaps($url, array $sitemaps) {
foreach ($sitemaps as $sitemap) {
$sitemap['url'] = url($sitemap['uri']['path'], $sitemap['uri']['options']);
$submit_url = xmlsitemap_engines_prepare_url($url, $sitemap['url']);
$request = drupal_http_request($submit_url);
watchdog('xmlsitemap', 'Submitted the sitemap to %url and received response @code.', array('%url' => $submit_url, '@code' => $request->code));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment