diff --git a/xmlsitemap.drush.inc b/xmlsitemap.drush.inc index fe9f21d..94bfce2 100644 --- a/xmlsitemap.drush.inc +++ b/xmlsitemap.drush.inc @@ -29,6 +29,11 @@ function xmlsitemap_drush_command() { 'limit' => 'The limit of links of each type to process. Default value: ' . variable_get('xmlsitemap_batch_limit', 100), ), ); + $items['xmlsitemap-rebuild-queue'] = array( + 'options' => array( + 'chunk-size' => 'The number of links to queue up for processing at a time.', + ), + ); return $items; } @@ -89,3 +94,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_rebuild_queue() { + 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('chunk-size', 50); + + // @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'); + } +} diff --git a/xmlsitemap.install b/xmlsitemap.install index 169bd96..916ebec 100644 --- a/xmlsitemap.install +++ b/xmlsitemap.install @@ -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(); } /** diff --git a/xmlsitemap.module b/xmlsitemap.module index 372c622..37f7a27 100644 --- a/xmlsitemap.module +++ b/xmlsitemap.module @@ -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_item', + 'time' => 60, + ); + + return $info; +} + +/** + * Queue callback for processing a sitemap link. + */ +function xmlsitemap_link_queue_process_item($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 a sitemap link 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); +}