Commit 696ab938 authored by Dave Reid's avatar Dave Reid Committed by Dave Reid

Issue #2417027 by Dave Reid: Added support for updating links and rebuilding all links via a queue.

parent c40292c0
......@@ -29,6 +29,12 @@ function xmlsitemap_drush_command() {
'limit' => 'The limit of links of each type to process. Default value: ' . variable_get('xmlsitemap_batch_limit', 100),
),
);
$items['xmlsitemap-queue-rebuild'] = array(
'description' => 'Dump and queues all possible XML sitemap data to be re-processed via the xmlsitemap_link_process queue. This command does not regenerate the sitemap files.',
'options' => array(
'limit' => 'The number of links to be processed in each queue task.',
),
);
return $items;
}
......@@ -89,3 +95,58 @@ function drush_xmlsitemap_index() {
drush_print(dt('Indexed @count new XML sitemap links.', array('@count' => $count_after - $count_before)));
}
}
/**
* Dump and queue all the sitemap links to be rebuilt in a queue process.
*/
function drush_xmlsitemap_queue_rebuild() {
module_load_include('generate.inc', 'xmlsitemap');
$entity_types = xmlsitemap_get_rebuildable_link_types();
if (empty($entity_types)) {
return drush_set_error(dt('No link types are rebuildable.'));
}
$context = array();
xmlsitemap_rebuild_batch_clear($entity_types, TRUE, $context);
$link_count = 0;
$chunk_count = 0;
$chunk_size = (int) drush_get_option('limit', variable_get('xmlsitemap_batch_limit', 100));
// @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch()
foreach ($entity_types as $entity_type) {
$info = xmlsitemap_get_link_info($entity_type);
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', $entity_type);
$query->entityCondition('entity_id', 0, '>');
$query->addTag('xmlsitemap_link_bundle_access');
$query->addTag('xmlsitemap_rebuild');
$query->addMetaData('entity', $entity_type);
$query->addMetaData('entity_info', $info);
if ($types = xmlsitemap_get_link_type_enabled_bundles($entity_type)) {
$query->entityCondition('bundle', $types, 'IN');
}
else {
// If no enabled bundle types, skip everything else.
continue;
}
$results = $query->execute();
if (!empty($results[$entity_type])) {
$ids = array_keys($results[$entity_type]);
$link_count += count($ids);
$chunks = array_chunk($ids, $chunk_size);
$chunk_count += count($chunks);
foreach ($chunks as $chunk) {
xmlsitemap_link_enqueue($entity_type, $chunk);
}
}
}
if ($link_count) {
drush_log(dt('Queued @link_count links for rebuild processing in the xmlsitemap_link_process (in @chunk_count chunks of up to @chunk_size links each).', array('@link_count' => $link_count, '@chunk_count' => $chunk_count, '@chunk_size' => $chunk_size)), 'success');
}
else {
drush_log(dt('No links to queue for rebuild processing.'), 'ok');
}
}
......@@ -338,6 +338,11 @@ function xmlsitemap_install() {
->execute();
// @todo Does the sitemap show up on first install or is it a 404 page?
// Create the link process the queue.
/** @var DrupalReliableQueueInterface $queue */
$queue = DrupalQueue::get('xmlsitemap_link_process', TRUE);
$queue->createQueue();
}
/**
......@@ -363,6 +368,11 @@ function xmlsitemap_uninstall() {
// Remove the file cache directory.
xmlsitemap_clear_directory(NULL, TRUE);
// Remove the queue.
/** @var DrupalReliableQueueInterface $queue */
$queue = DrupalQueue::get('xmlsitemap_link_process', TRUE);
$queue->deleteQueue();
}
/**
......
......@@ -1551,3 +1551,44 @@ function xmlsitemap_get_operation_link($url, $options = array()) {
drupal_alter('xmlsitemap_operation_link', $link);
return $link;
}
/**
* Implements hook_cron_queue_info().
*/
function xmlsitemap_cron_queue_info() {
$info['xmlsitemap_link_process'] = array(
'worker callback' => 'xmlsitemap_link_queue_process',
'time' => 60,
);
return $info;
}
/**
* Queue callback for processing sitemap links.
*/
function xmlsitemap_link_queue_process($data) {
$info = xmlsitemap_get_link_info($data['type']);
$ids = isset($data['ids']) ? $data['ids'] : array($data['id']);
if (function_exists($info['xmlsitemap']['process callback'])) {
$info['xmlsitemap']['process callback']($ids);
}
}
/**
* Enqueue sitemap links to be updated via the xmlsitemap_link_process queue.
*
* @param string $type
* The link type.
* @param array|int $ids
* An array of link IDs or a singular link ID.
*/
function xmlsitemap_link_enqueue($type, $ids) {
$data = array();
$data['type'] = $type;
$data['ids'] = is_array($ids) ? $ids : array($ids);
/** @var DrupalReliableQueueInterface $queue */
$queue = DrupalQueue::get('xmlsitemap_link_process');
$queue->createItem($data);
}
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