Commit 216c174b authored by Dave Reid's avatar Dave Reid

by Dave Reid: Renamed xmlsitemap_run_progressive_batch() to xmlsitemap_run_unprogressive_batch().

by Dave Reid: Cleaned up batch functions.
parent 8ed5530c
......@@ -40,7 +40,7 @@ function drush_xmlsitemap_regenerate() {
module_load_include('generate.inc', 'xmlsitemap');
// Run the batch process.
xmlsitemap_run_progressive_batch('xmlsitemap_regenerate_batch');
xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
$vars = array(
'@timer' => timer_read('xmlsitemap_regenerate'),
......@@ -59,7 +59,7 @@ function drush_xmlsitemap_rebuild() {
$rebuild_types = xmlsitemap_get_rebuildable_link_types();
// Run the batch process.
xmlsitemap_run_progressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
xmlsitemap_run_unprogressive_batch('xmlsitemap_rebuild_batch', $rebuild_types, TRUE);
$vars = array(
'@timer' => timer_read('xmlsitemap_rebuild'),
......
......@@ -48,13 +48,9 @@ function xmlsitemap_get_path_alias($path, $language) {
* Perform operations before rebuilding the sitemap.
*/
function _xmlsitemap_regenerate_before() {
// Attempt to increase the available processing time and memory limit.
drupal_set_time_limit(240);
// Attempt to increase the memory limit.
_xmlsitemap_set_memory_limit();
// Set a timer so we can track how long this takes.
timer_start('xmlsitemap_regenerate');
if (variable_get('xmlsitemap_developer_mode', 0)) {
watchdog('xmlsitemap', 'Starting XML sitemap generation. Memory usage: @memory-peak.', array(
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
......@@ -116,26 +112,6 @@ function _xmlsitemap_set_memory_limit($new_limit = NULL) {
}
}
/**
* Perform operations after rebuilding the sitemap.
*/
function _xmlsitemap_regenerate_after() {
// Show a watchdog message that the sitemap was regenerated.
watchdog('xmlsitemap',
'Finished XML sitemap generation in @timer ms. Memory usage: @memory-peak.',
array(
'@timer' => timer_read('xmlsitemap_regenerate'),
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
),
WATCHDOG_NOTICE
);
// Unset the regenerate flag.
variable_set('xmlsitemap_regenerate_needed', FALSE);
variable_set('xmlsitemap_generated_last', REQUEST_TIME);
}
/**
* Generate one page (chunk) of the sitemap.
*
......@@ -176,10 +152,10 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer,
$query = db_select('xmlsitemap', 'x');
$query->fields('x', array('loc', 'lastmod', 'changefreq', 'changecount', 'priority', 'language', 'access', 'status'));
$query->condition('access', 1);
$query->condition('status', 1);
$query->orderBy('language', 'DESC');
$query->orderBy('loc');
$query->condition('x.access', 1);
$query->condition('x.status', 1);
$query->orderBy('x.language', 'DESC');
$query->orderBy('x.loc');
$query->addTag('xmlsitemap_generate');
$query->addMetaData('sitemap', $sitemap);
......@@ -193,7 +169,13 @@ function xmlsitemap_generate_chunk(stdClass $sitemap, XMLSitemapWriter $writer,
if ($url_options['alias']) {
$link['loc'] = xmlsitemap_get_path_alias($link['loc'], $link['language']->language);
}
$link_url = url($link['loc'], array('language' => $link['language']) + $url_options);
$link_options = array(
'language' => $link['language'],
'xmlsitemap_link' => $link,
'xmlsitemap_sitemap' => $sitemap,
);
// @todo Add a separate hook_xmlsitemap_link_url_alter() here?
$link_url = url($link['loc'], $link_options + $url_options);
// Skip this link if it was a duplicate of the last one.
// @todo Figure out a way to do this before generation so we can report
......@@ -277,13 +259,20 @@ function xmlsitemap_regenerate_batch(array $smids = array()) {
'file' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.generate.inc',
);
// Generate all the sitemap pages for each context.
// Set the regenerate flag in case something fails during file generation.
$batch['operations'][] = array('xmlsitemap_batch_variable_set', array(array('xmlsitemap_regenerate_needed' => TRUE)));
// @todo Get rid of this batch operation.
$batch['operations'][] = array('_xmlsitemap_regenerate_before', array());
// Generate all the sitemap pages for each context.
foreach ($smids as $smid) {
$batch['operations'][] = array('xmlsitemap_regenerate_batch_generate', array($smid));
$batch['operations'][] = array('xmlsitemap_regenerate_batch_generate_index', array($smid));
}
$batch['operations'][] = array('_xmlsitemap_regenerate_after', array());
// Clear the regeneration flag.
$batch['operations'][] = array('xmlsitemap_batch_variable_set', array(array('xmlsitemap_regenerate_needed' => FALSE)));
return $batch;
}
......@@ -345,14 +334,22 @@ function xmlsitemap_regenerate_batch_generate_index($smid, array &$context) {
/**
* Batch callback; sitemap regeneration finished.
*/
function xmlsitemap_regenerate_batch_finished($success, $results, $operations) {
if ($success) {
// Reset the rebuild flag since it was successful.
variable_set('xmlsitemap_regenerate_needed', FALSE);
function xmlsitemap_regenerate_batch_finished($success, $results, $operations, $elapsed) {
if ($success && !variable_get('xmlsitemap_regenerate_needed', FALSE)) {
variable_set('xmlsitemap_generated_last', REQUEST_TIME);
//drupal_set_message(t('The sitemaps were regenerated.'));
// Show a watchdog message that the sitemap was regenerated.
watchdog('xmlsitemap',
'Finished XML sitemap generation in @elapsed. Memory usage: @memory-peak.',
array(
'@elapsed' => $elapsed,
'@memory-peak' => format_size(memory_get_peak_usage(TRUE)),
),
WATCHDOG_NOTICE
);
}
else {
drupal_set_message(t('The sitemaps was not successfully regenerated.'), 'error');
drupal_set_message(t('The sitemaps were not successfully regenerated.'), 'error');
}
}
......@@ -367,7 +364,8 @@ function xmlsitemap_rebuild_batch(array $entities, $save_custom = FALSE) {
'file' => drupal_get_path('module', 'xmlsitemap') . '/xmlsitemap.generate.inc',
);
$batch['operations'][] = array('xmlsitemap_rebuild_batch_prerebuild', array());
// Set the rebuild flag in case something fails during the rebuild.
$batch['operations'][] = array('xmlsitemap_batch_variable_set', array(array('xmlsitemap_rebuild_needed' => TRUE)));
// Purge any links first.
$batch['operations'][] = array('xmlsitemap_rebuild_batch_clear', array($entities, (bool) $save_custom));
......@@ -378,6 +376,9 @@ function xmlsitemap_rebuild_batch(array $entities, $save_custom = FALSE) {
$batch['operations'][] = array($info['xmlsitemap']['rebuild callback'], array($entity));
}
// Clear the rebuild flag.
$batch['operations'][] = array('xmlsitemap_batch_variable_set', array(array('xmlsitemap_rebuild_needed' => FALSE)));
// Add the regeneration batch.
$regenerate_batch = xmlsitemap_regenerate_batch();
$batch['operations'] = array_merge($batch['operations'], $regenerate_batch['operations']);
......@@ -386,13 +387,12 @@ function xmlsitemap_rebuild_batch(array $entities, $save_custom = FALSE) {
}
/**
* Batch callback; perform operations before rebuilding the sitemap data.
* Batch callback; set an array of variables and their values.
*/
function xmlsitemap_rebuild_batch_prerebuild() {
// Set the rebuild flag in case something fails during the rebuild.
variable_set('xmlsitemap_rebuild_needed', TRUE);
timer_start('xmlsitemap_rebuild');
function xmlsitemap_batch_variable_set(array $variables) {
foreach ($variables as $variable => $value) {
variable_set($variable, $value);
}
}
/**
......@@ -463,14 +463,12 @@ function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
/**
* Batch callback; sitemap rebuild finished.
*/
function xmlsitemap_rebuild_batch_finished($success, $results, $operations) {
if ($success) {
// Reset the rebuild flag since it was successful.
variable_set('xmlsitemap_rebuild_needed', FALSE);
drupal_set_message(t('The sitemap was rebuilt.'));
function xmlsitemap_rebuild_batch_finished($success, $results, $operations, $elapsed) {
if ($success && !variable_get('xmlsitemap_rebuild_needed', FALSE)) {
drupal_set_message(t('The sitemap links were rebuilt.'));
}
else {
drupal_set_message(t('The sitemap was not successfully rebuilt.'), 'error');
drupal_set_message(t('The sitemap links were not successfully rebuilt.'), 'error');
}
}
......
......@@ -63,12 +63,12 @@ function xmlsitemap_hook_info() {
'xmlsitemap_index_links',
'xmlsitemap_context_info',
'xmlsitemap_context_info_alter',
'xmlsitemap_context_url_options',
'xmlsitemap_context',
'xmlsitemap_sitemap_insert',
'xmlsitemap_sitemap_update',
'xmlsitemap_sitemap_operations',
'xmlsitemap_sitemap_delete',
'xmlsitemap_sitemap_uri_alter',
'xmlsitemap_sitemap_link_url_options_alter',
'query_xmlsitemap_generate_alter',
'query_xmlsitemap_link_bundle_access_alter',
......@@ -240,7 +240,7 @@ function xmlsitemap_cron() {
// Regenerate the sitemap XML files.
module_load_include('generate.inc', 'xmlsitemap');
xmlsitemap_run_progressive_batch('xmlsitemap_regenerate_batch');
xmlsitemap_run_unprogressive_batch('xmlsitemap_regenerate_batch');
}
/**
......@@ -1426,9 +1426,9 @@ function _xmlsitemap_sitemap_context_summary(stdClass $sitemap, $context_key, ar
*/
/**
* Run a progressive batch operation.
* Run a not-progressive batch operation.
*/
function xmlsitemap_run_progressive_batch() {
function xmlsitemap_run_unprogressive_batch() {
$batch = batch_get();
if (!empty($batch)) {
// If there is already something in the batch, don't run.
......@@ -1442,6 +1442,9 @@ function xmlsitemap_run_progressive_batch() {
return FALSE;
}
// Attempt to increase the execution time.
drupal_set_time_limit(240);
// Build the batch array.
$batch = call_user_func_array($batch_callback, $args);
batch_set($batch);
......
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