Commit c3e77a09 authored by Dave Reid's avatar Dave Reid

by Dave Reid: Convert the xmlsitemap_max_filesize variable to a max_filesize column.

parent c426b268
......@@ -191,12 +191,6 @@ class XMLSitemapWriter extends XMLWriter {
public function endDocument() {
$return = parent::endDocument();
// Track the maximum filesize.
$filesize = filesize($this->uri);
if ($filesize > variable_get('xmlsitemap_max_filesize', 0)) {
variable_set('xmlsitemap_max_filesize', $filesize);
}
if (!$this->getStatus()) {
trigger_error(t('Unknown error occurred while writing to file @file.', array('@file' => $this->uri)));
return FALSE;
......@@ -265,10 +259,6 @@ function _xmlsitemap_regenerate_before() {
WATCHDOG_DEBUG
);
}
// Clear the maximum chunk and file size variables.
variable_set('xmlsitemap_max_chunks', 0);
variable_set('xmlsitemap_max_filesize', 0);
}
function _xmlsitemap_get_memory_usage($start = FALSE) {
......@@ -529,6 +519,7 @@ function xmlsitemap_regenerate_batch_generate($smid, array &$context) {
// Save the updated chunks and links values.
$context['sandbox']['max'] = $sitemap['chunks'];
$sitemap['updated'] = REQUEST_TIME;
xmlsitemap_sitemap_get_max_filesize($sitemap);
xmlsitemap_sitemap_save($sitemap);
}
......
......@@ -15,6 +15,8 @@ function xmlsitemap_requirements($phase) {
$requirements = array();
$t = get_t();
// If clean URLs are disabled there must not be an actual sitemap.xml in
// the root directory.
if (variable_get('clean_url', 0) && file_exists(DRUPAL_ROOT . '/sitemap.xml')) {
$requirements['xmlsitemap_file'] = array(
'title' => $t('XML sitemap'),
......@@ -33,6 +35,8 @@ function xmlsitemap_requirements($phase) {
'severity' => REQUIREMENT_ERROR,
);
}
// Check that the base directory and all its subdirectories are writable.
$requirements['xmlsitemap_directory'] = array(
'title' => $t('XML sitemap cache directory'),
'value' => $t('Writable'),
......@@ -55,19 +59,25 @@ function xmlsitemap_requirements($phase) {
$requirements['xmlsitemap_directory']['description'] = $t('The following directories were not found or are not writable by the server. See <a href="@docpage">@docpage</a> for more information. !directories', array('!directories' => theme('item_list', array('items' => array_keys($directories))), '@docpage' => 'http://drupal.org/node/34025'));
}
}
$max_links = XMLSITEMAP_MAX_SITEMAP_LINKS * XMLSITEMAP_MAX_SITEMAP_LINKS;
if (xmlsitemap_get_link_count() > $max_links) {
// The maximum number of links in a sitemap.
$max_links = db_query("SELECT MAX(links) FROM {xmlsitemap_sitemap}")->fetchField();
$max_links_limit = XMLSITEMAP_MAX_SITEMAP_LINKS * XMLSITEMAP_MAX_SITEMAP_LINKS;
if ($max_links > $max_links_limit) {
$requirements['xmlsitemap_link_count'] = array(
'title' => $t('XML sitemap link count'),
'value' => xmlsitemap_get_link_count(),
'value' => $max_links,
'description' => $t('You have exceeded the number of links that your sitemap can contain (@num).', array('@num' => number_format($max_links))),
'severity' => REQUIREMENT_ERROR,
);
}
if (xmlsitemap_get_chunk_count() > XMLSITEMAP_MAX_SITEMAP_LINKS) {
// The maximum number of chunks in a sitemap.
$max_chunks = db_query("SELECT MAX(chunks) FROM {xmlsitemap_sitemap}")->fetchField();
if ($max_chunks > XMLSITEMAP_MAX_SITEMAP_LINKS) {
$requirements['xmlsitemap_chunk_count'] = array(
'title' => $t('XML sitemap page count'),
'value' => xmlsitemap_get_chunk_count(),
'value' => $max_chunks,
'description' => $t('You have exceeded the number of sitemap pages (@number).', array('@number' => number_format(XMLSITEMAP_MAX_SITEMAP_LINKS))),
'severity' => REQUIREMENT_ERROR,
);
......@@ -77,7 +87,7 @@ function xmlsitemap_requirements($phase) {
}
// Check maximum file size.
$max_filesize = variable_get('xmlsitemap_max_filesize', 0);
$max_filesize = db_query("SELECT MAX(max_filesize) FROM {xmlsitemap_sitemap}")->fetchField();
$requirements['xmlsitemap_file_size'] = array(
'title' => $t('XML sitemap maximum file size'),
'value' => format_size($max_filesize),
......@@ -114,6 +124,7 @@ function xmlsitemap_requirements($phase) {
}
}
}
return $requirements;
}
......@@ -266,6 +277,12 @@ function xmlsitemap_schema() {
'not null' => TRUE,
'default' => 0,
),
'max_filesize' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
//'queued' => array(
// 'type' => 'int',
// 'unsigned' => TRUE,
......@@ -433,6 +450,39 @@ function xmlsitemap_update_6202() {
variable_set('xmlsitemap_regenerate_needed', TRUE);
}
/**
* Convert the xmlsitemap_max_filesize variable to a max_filesize column
* per-sitemap.
*/
function xmlsitemap_update_6203() {
db_drop_field('xmlsitemap_sitemap', 'max_filesize');
if (!db_field_exists('xmlsitemap_sitemap', 'max_filesize')) {
drupal_load('module', 'xmlsitemap');
// Add the max_filesize column.
$field = array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
);
db_add_field('xmlsitemap_sitemap', 'max_filesize', $field);
// Scan each sitemap directory for the largest file.
$sitemaps = xmlsitemap_sitemap_load_multiple(FALSE);
foreach ($sitemaps as $sitemap) {
xmlsitemap_sitemap_get_max_filesize($sitemap);
db_update('xmlsitemap_sitemap')
->fields(array('max_filesize' => $sitemap['max_filesize']))
->condition('smid', $sitemap['smid'])
->execute();
}
}
variable_del('xmlsitemap_max_filesize');
variable_del('xmlsitemap_max_chunks');
}
/**
* Update empty string languages to LANGUAGE_NONE.
*/
......@@ -449,3 +499,11 @@ function xmlsitemap_update_7200() {
function xmlsitemap_update_7201() {
xmlsitemap_update_6202();
}
/**
* Convert the xmlsitemap_max_filesize variable to a max_filesize column
* per-sitemap.
*/
function xmlsitemap_update_7202() {
xmlsitemap_update_6203();
}
......@@ -284,15 +284,15 @@ function xmlsitemap_variables() {
'xmlsitemap_developer_mode' => 0,
'xmlsitemap_frontpage_priority' => 1.0,
'xmlsitemap_frontpage_changefreq' => XMLSITEMAP_FREQUENCY_DAILY,
'xmlsitemap_max_chunks' => 0,
'xmlsitemap_max_filesize' => 0,
'xmlsitemap_lastmod_format' => XMLSITEMAP_LASTMOD_MEDIUM,
// Removed variables are set to NULL so they can still be deleted.
'xmlsitemap_gz' => FALSE,
// Removed variables are set to NULL so they can still be deleted.
'xmlsitemap_regenerate_last' => NULL,
'xmlsitemap_custom_links' => NULL,
'xmlsitemap_priority_default' => NULL,
'xmlsitemap_languages' => NULL,
'xmlsitemap_max_chunks' => NULL,
'xmlsitemap_max_filesize' => NULL,
);
}
......@@ -440,6 +440,22 @@ function xmlsitemap_sitemap_get_file(array $sitemap, $chunk = 'index') {
return xmlsitemap_get_directory($sitemap) . "/{$chunk}.xml";
}
/**
* Find the maximum file size of all a sitemap's XML files.
*
* @param $sitemap
* The XML sitemap array.
*
*/
function xmlsitemap_sitemap_get_max_filesize(array &$sitemap) {
$dir = xmlsitemap_get_directory($sitemap);
$sitemap['max_filesize'] = 0;
foreach (file_scan_directory($dir, '/\.xml$/') as $file) {
$sitemap['max_filesize'] = max($sitemap['max_filesize'], filesize($file->uri));
}
return $sitemap['max_filesize'];
}
/**
* Returns the uri elements of an XML sitemap.
*
......@@ -1413,4 +1429,3 @@ function xmlsitemap_query_xmlsitemap_link_bundle_access_alter(QueryAlterableInte
}
}
}
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