Commit 76c5f408 authored by Dave Reid's avatar Dave Reid
Browse files

by Dave Reid: Standardize the cache directory functions.

parent 9d6d3d78
...@@ -25,10 +25,12 @@ function xmlsitemap_drush_command() { ...@@ -25,10 +25,12 @@ function xmlsitemap_drush_command() {
/** /**
* Check that the sitemap files directory exists and is writable. * Check that the sitemap files directory exists and is writable.
*
* @todo Remove when Drush intercepts watchdog errors.
*/ */
function drush_xmlsitemap_check_directory() { function drush_xmlsitemap_check_directory() {
if (!xmlsitemap_check_directory()) { if (!xmlsitemap_check_directory()) {
drush_die(dt('The XML sitemap cache directory @directory was not found was not writable.', array('@directory' => $dir))); drush_die(dt('The XML sitemap cache directory @directory was not found was not writable.', array('@directory' => xmlsitemap_get_directory())));
} }
} }
......
...@@ -82,8 +82,7 @@ function _xmlsitemap_regenerate_before() { ...@@ -82,8 +82,7 @@ function _xmlsitemap_regenerate_before() {
_xmlsitemap_get_memory_usage(TRUE); _xmlsitemap_get_memory_usage(TRUE);
// Clear all cached sitemap files. // Clear all cached sitemap files.
$path = file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap'); xmlsitemap_clear_directory();
file_unmanaged_delete_recursive($path);
xmlsitemap_check_directory(); xmlsitemap_check_directory();
} }
......
...@@ -38,7 +38,7 @@ function xmlsitemap_requirements($phase) { ...@@ -38,7 +38,7 @@ function xmlsitemap_requirements($phase) {
'title' => $t('XML sitemap cache directory'), 'title' => $t('XML sitemap cache directory'),
'value' => $t('Not found or not writable'), 'value' => $t('Not found or not writable'),
'severity' => REQUIREMENT_ERROR, 'severity' => REQUIREMENT_ERROR,
'description' => $directory, 'description' => xmlsitemap_get_directory(),
); );
} }
$max_links = XMLSITEMAP_MAX_SITEMAP_LINKS * XMLSITEMAP_MAX_SITEMAP_CHUNKS; $max_links = XMLSITEMAP_MAX_SITEMAP_LINKS * XMLSITEMAP_MAX_SITEMAP_CHUNKS;
...@@ -151,8 +151,7 @@ function xmlsitemap_uninstall() { ...@@ -151,8 +151,7 @@ function xmlsitemap_uninstall() {
} }
// Remove files directory. // Remove files directory.
$path = file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap'); xmlsitemap_clear_directory(TRUE);
file_unmanaged_delete_recursive($path);
} }
/** /**
......
...@@ -547,7 +547,7 @@ function xmlsitemap_delete_link(array $conditions) { ...@@ -547,7 +547,7 @@ function xmlsitemap_delete_link(array $conditions) {
* @todo Move to xmlsitemap.inc * @todo Move to xmlsitemap.inc
*/ */
function xmlsitemap_get_chunk_file($chunk = 0, $language, $compressed = FALSE) { function xmlsitemap_get_chunk_file($chunk = 0, $language, $compressed = FALSE) {
return file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap') . '/xmlsitemap-' . $language->language . '-' . $chunk . ($compressed ? '.gz' : '.xml'); return xmlsitemap_get_directory() . '/xmlsitemap-' . $language->language . '-' . $chunk . ($compressed ? '.gz' : '.xml');
} }
/** /**
...@@ -782,14 +782,47 @@ function xmlsitemap_xmlsitemap_link_info() { ...@@ -782,14 +782,47 @@ function xmlsitemap_xmlsitemap_link_info() {
); );
} }
function xmlsitemap_get_directory() {
$directory = &drupal_static(__FUNCTION__);
if (!isset($directory)) {
$directory = file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap');
}
return $directory;
}
/** /**
* Check that the sitemap files directory exists and is writable. * Check that the sitemap files directory exists and is writable.
*/ */
function xmlsitemap_check_directory() { function xmlsitemap_check_directory() {
$path = file_directory_path() . '/' . variable_get('xmlsitemap_path', 'xmlsitemap'); $directory = xmlsitemap_get_directory();
$result = file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS); $result = file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
if (!$result) { if (!$result) {
watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $path), WATCHDOG_ERROR); watchdog('file system', 'The directory %directory does not exist or is not writable.', array('%directory' => $directory), WATCHDOG_ERROR);
} }
return $result; return $result;
} }
function xmlsitemap_clear_directory($delete = FALSE) {
$directory = xmlsitemap_get_directory();
return _xmlsitemap_delete_recursive($directory, $delete);
}
function _xmlsitemap_delete_recursive($path, $delete_root = FALSE) {
// Resolve streamwrapper URI to local path.
$path = drupal_realpath($path);
if (is_dir($path)) {
$dir = dir($path);
while (($entry = $dir->read()) !== FALSE) {
if ($entry == '.' || $entry == '..') {
continue;
}
$entry_path = $path . '/' . $entry;
file_unmanaged_delete_recursive($entry_path, TRUE);
}
$dir->close();
return $delete_root ? rmdir($path) : TRUE;
}
return file_unmanaged_delete($path);
}
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