Commit ab65ff4b authored by Dave Reid's avatar Dave Reid

#768124: Fixed regenerating a specific sitemap deleted all sitemap cache files.

parent c31a4d93
......@@ -76,6 +76,9 @@ function xmlsitemap_sitemap_list_form() {
$options[$smid]['links'] = $sitemap['updated'] ? $sitemap['links'] : '-';
$options[$smid]['chunks'] = $sitemap['updated'] ? $sitemap['chunks'] : '-';
// @todo Highlight sitemaps that need updating.
//$options[$smid]['#attributes']['class'][] = 'warning';
$operations = array();
$operations['edit'] = array(
'title' => t('Edit'),
......@@ -241,6 +244,7 @@ function xmlsitemap_sitemap_edit_form_submit($form, &$form_state) {
xmlsitemap_sitemap_save($form_state['values']);
drupal_set_message(t('The sitemap has been saved.'));
$form_state['redirect'] = 'admin/config/search/xmlsitemap';
// @todo If context was changed, needs to be regenerated.
}
function xmlsitemap_sitemap_delete_form(&$form, $form_state, array $sitemap) {
......@@ -268,8 +272,8 @@ function xmlsitemap_sitemap_delete_form(&$form, $form_state, array $sitemap) {
}
function xmlsitemap_sitemap_delete_form_submit($form, $form_state) {
xmlsitemap_sitemap_delete($form_state['values']['smid']);
drupal_set_message(t('The sitemap %sitemap has been deleted.', array('%sitemap' => $form['#sitemap']['url'])));
xmlsitemap_sitemap_delete($form['#sitemap']);
drupal_set_message(t('The sitemap has been deleted.'));
$form_state['redirect'] = 'admin/config/search/xmlsitemap';
}
......
......@@ -67,10 +67,6 @@ function _xmlsitemap_regenerate_before() {
// Get the current memory usage so we can track how much memory is used.
_xmlsitemap_get_memory_usage(TRUE);
// Clear all cached sitemap files.
xmlsitemap_clear_directory();
xmlsitemap_check_directory();
// Clear the maximum chunk and file size variables.
variable_set('xmlsitemap_max_chunks', 0);
variable_set('xmlsitemap_max_filesize', 0);
......@@ -154,7 +150,7 @@ function xmlsitemap_generate(array $sitemap, $chunk) {
return FALSE;
}
$file = xmlsitemap_get_sitemap_file($sitemap, $chunk);
$file = xmlsitemap_sitemap_get_file($sitemap, $chunk);
if (!$handle = fopen($file, 'wb')) {
trigger_error(t('Could not open file @file for writing.', array('@file' => $file)));
......@@ -397,6 +393,10 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
$context['sandbox']['sitemap']['chunks'] = 1;
$context['sandbox']['sitemap']['links'] = 0;
$context['sandbox']['max'] = XMLSITEMAP_MAX_SITEMAP_LINKS;
// Clear the cache directory for this sitemap before generating any files.
xmlsitemap_check_directory($context['sandbox']['sitemap']);
xmlsitemap_clear_directory($context['sandbox']['sitemap']);
}
$sitemap = &$context['sandbox']['sitemap'];
......@@ -408,9 +408,11 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
$sitemap['chunks']++;
}
else {
// Account for the 'extra' chunk here.
// @todo Delete the extra file?
// Cleanup the 'extra' empty file.
$file = xmlsitemap_sitemap_get_file($sitemap, $sitemap['chunks']);
file_unmanaged_delete($file);
$sitemap['chunks']--;
// Save the updated chunks and links values.
$context['sandbox']['max'] = $sitemap['chunks'];
$sitemap['updated'] = REQUEST_TIME;
......
......@@ -120,7 +120,7 @@ function xmlsitemap_permission() {
}
function xmlsitemap_sitemap_load_multiple(array $smids) {
$sitemaps = db_query("SELECT * FROM {xmlsitemap_sitemap} WHERE smid IN (:smids)", array(':smids' => $smids))->fetchAllAssoc('smid', PDO::FETCH_ASSOC);
$sitemaps = !empty($smids) ? db_query("SELECT * FROM {xmlsitemap_sitemap} WHERE smid IN (:smids)", array(':smids' => $smids))->fetchAllAssoc('smid', PDO::FETCH_ASSOC) : array();
foreach ($sitemaps as $smid => $sitemap) {
$sitemaps[$smid]['context'] = unserialize($sitemap['context']);
$sitemaps[$smid]['uri'] = xmlsitemap_sitemap_uri($sitemaps[$smid]);
......@@ -142,6 +142,18 @@ function xmlsitemap_sitemap_load_by_context(array $context = NULL) {
return xmlsitemap_sitemap_load($smid);
}
/**
* 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_sitemap_get_file(array $sitemap, $chunk = 'index') {
return xmlsitemap_get_directory($sitemap) . "/{$chunk}.xml";
}
/**
* Returns the uri elements of an XML sitemap.
*
......@@ -177,11 +189,11 @@ function xmlsitemap_sitemap_save(array $sitemap) {
}
}
function xmlsitemap_sitemap_delete($smid) {
function xmlsitemap_sitemap_delete(array $sitemap) {
db_delete('xmlsitemap_sitemap')
->condition('smid', $smid)
->condition('smid', $sitemap['smid'])
->execute();
// @todo Delete context files.
xmlsitemap_clear_directory($sitemap, TRUE);
}
/**
......@@ -272,6 +284,9 @@ function xmlsitemap_menu() {
/**
* Implements hook_cron().
*
* @todo Use new Queue system. Need to add {sitemap}.queued.
* @todo Regenerate one at a time?
*/
function xmlsitemap_cron() {
// If there were no new or changed links, skip.
......@@ -874,33 +889,21 @@ function xmlsitemap_get_link_info($type = NULL, $reset = FALSE) {
return $link_info;
}
/**
* 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() {
function xmlsitemap_get_directory(array $sitemap = NULL) {
$directory = &drupal_static(__FUNCTION__);
if (!isset($directory)) {
$directory = file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap');
}
return $directory;
return $directory . (!empty($sitemap) ? '/' . $sitemap['context_hash'] : '');
}
/**
* Check that the sitemap files directory exists and is writable.
*/
function xmlsitemap_check_directory() {
$directory = xmlsitemap_get_directory();
function xmlsitemap_check_directory(array $sitemap = NULL) {
$directory = xmlsitemap_get_directory($sitemap);
$result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if (!$result) {
watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $directory), WATCHDOG_ERROR);
......@@ -908,8 +911,8 @@ function xmlsitemap_check_directory() {
return $result;
}
function xmlsitemap_clear_directory($delete = FALSE) {
$directory = xmlsitemap_get_directory();
function xmlsitemap_clear_directory(array $sitemap = NULL, $delete = FALSE) {
$directory = xmlsitemap_get_directory($sitemap);
return _xmlsitemap_delete_recursive($directory, $delete);
}
......
......@@ -31,7 +31,7 @@ function xmlsitemap_get_current_chunk(array $sitemap) {
*
* @see xmlsitemap_sitemap_load_by_context()
* @see xmlsitemap_get_current_chunk()
* @see xmlsitemap_get_sitemap_file()
* @see xmlsitemap_sitemap_get_file()
* @see xmlsitemap_output_file()
*/
function xmlsitemap_output_chunk() {
......@@ -41,7 +41,7 @@ function xmlsitemap_output_chunk() {
}
$chunk = xmlsitemap_get_current_chunk($sitemap);
$file = xmlsitemap_get_sitemap_file($sitemap, $chunk);
$file = xmlsitemap_sitemap_get_file($sitemap, $chunk);
// Provide debugging information if enabled.
if (variable_get('xmlsitemap_developer_mode', 0) || isset($_GET['debug'])) {
......
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