Commit 813176dc authored by Dave Reid's avatar Dave Reid

by Dave Reid: Add sitemap 'contexts' for generating and displaying.

parent 70f7a190
......@@ -52,14 +52,13 @@ function xmlsitemap_regenerate() {
// Generate the sitemap pages.
$chunk_count = xmlsitemap_get_chunk_count(TRUE);
if ($chunk_count > 1) {
// If we have more than one chunk, we need to increment this value by one
// since the index page (chunk 0) will also need to be generated.
$chunk_count++;
}
foreach (xmlsitemap_var('languages') as $language) {
for ($i = 0; $i < $chunk_count; $i++) {
xmlsitemap_generate($i, xmlsitemap_language_load($language));
foreach (xmlsitemap_var('languages') as $langcode) {
$language = xmlsitemap_language_load($langcode);
if ($chunk_count > 1) {
xmlsitemap_generate('index', $language);
}
for ($i = 1; $i <= $chunk_count; $i++) {
xmlsitemap_generate($i, $language);
}
}
......@@ -160,8 +159,8 @@ function _xmlsitemap_regenerate_after() {
*
* @todo Revise/simplify or remove the function.
*/
function xmlsitemap_generate($chunk = 0, $language = NULL) {
if (!is_numeric($chunk) || $chunk > xmlsitemap_get_chunk_count()) {
function xmlsitemap_generate($chunk, $language = NULL) {
if (($chunk == 'index' && xmlsitemap_get_chunk_count() < 2) || ($chunk != 'index' && (!is_numeric($chunk) || $chunk > xmlsitemap_get_chunk_count()))) {
// Don't bother translating this string.
trigger_error('Improper condition hit in xmlsitemap_generate(). Chunk: ' . $chunk . ', Chunk Count: ' . xmlsitemap_get_chunk_count());
return FALSE;
......@@ -171,7 +170,11 @@ function xmlsitemap_generate($chunk = 0, $language = NULL) {
$language = language_default();
}
$file = xmlsitemap_get_chunk_file($chunk, $language->language);
$context = array();
if (module_exists('xmlsitemap_i18n')) {
$context['language'] = $language->language;
}
$file = xmlsitemap_get_file_from_context($context, $chunk);
if (!$handle = fopen($file, 'wb')) {
trigger_error(t('Could not open file @file for writing.', array('@file' => $file)));
......@@ -179,7 +182,7 @@ function xmlsitemap_generate($chunk = 0, $language = NULL) {
}
$status = TRUE;
if (xmlsitemap_get_chunk_count() > 1 && !$chunk) {
if ($chunk == 'index') {
xmlsitemap_generate_index($handle, $status, $language);
}
else {
......@@ -446,18 +449,18 @@ function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
*/
function xmlsitemap_rebuild_batch_generate($language, &$context) {
if (!isset($context['sandbox']['progress'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['progress'] = 1;
$context['sandbox']['max'] = xmlsitemap_get_chunk_count(TRUE);
// If we have more than one chunk, we need to increment this value by one
// since the index page (chunk 0) will also need to be generated.
if ($context['sandbox']['max'] > 1) {
$context['sandbox']['max']++;
xmlsitemap_generate('index', $language);
}
// Bump the max number up by one since it gets incremented after generation.
$context['sandbox']['max']++;
}
xmlsitemap_generate($context['sandbox']['progress'], $language);
$context['sandbox']['progress']++;
$context['message'] = t('Now generating @language sitemap page @chunk.', array('@language' => $language->name, '@chunk' => $context['sandbox']['progress']));
$context['sandbox']['progress']++;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
......
......@@ -862,6 +862,10 @@ function xmlsitemap_link_frontpage_settings(&$form) {
return $form;
}
function xmlsitemap_get_file_from_context(array $context, $chunk = 'index') {
return xmlsitemap_get_directory() . '/' . md5(serialize($context)) . "-{$chunk}.xml";
}
function xmlsitemap_get_directory() {
$directory = &drupal_static(__FUNCTION__);
......
......@@ -16,8 +16,26 @@
function xmlsitemap_output_chunk() {
global $language;
$chunk = isset($_GET['page']) && is_numeric($_GET['page']) ? $_GET['page'] : 0;
$file = xmlsitemap_get_chunk_file($chunk, $language->language);
$context = array();
if (module_exists('xmlsitemap_i18n')) {
$context['language'] = $language->language;
}
// Check if we should be displaing the index.
if (!isset($_GET['page'])) {
$index_file = xmlsitemap_get_file_from_context($context, 'index');
if (file_exists($index_file)) {
$chunk = 'index';
}
else {
$chunk = 1;
}
}
else {
$chunk = (int) $_GET['page'];
}
$file = xmlsitemap_get_file_from_context($context, $chunk);
xmlsitemap_output_file($file);
}
......
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