diff --git a/src/Plugin/QueueWorker/XmlSitemapLinkProcessQueue.php b/src/Plugin/QueueWorker/XmlSitemapLinkProcessQueue.php new file mode 100644 index 0000000..5540417 --- /dev/null +++ b/src/Plugin/QueueWorker/XmlSitemapLinkProcessQueue.php @@ -0,0 +1,29 @@ + 'The limit of links of each type to process.', ], ]; + $items['xmlsitemap-queue-rebuild'] = [ + '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' => [ + 'limit' => 'The number of links to be processed in each queue task.', + ], + ]; return $items; } @@ -90,3 +95,78 @@ function drush_xmlsitemap_index() { drush_print(dt('Indexed @count new XML sitemap links.', ['@count' => $count_after - $count_before])); } } + +/** + * Dump and queue all the sitemap links to be rebuilt in a queue process. + */ +function drush_xmlsitemap_queue_rebuild() { + $entity_types = xmlsitemap_get_rebuildable_link_types(); + if (empty($entity_types)) { + return drush_set_error(dt('No link types are rebuildable.')); + } + + $context = []; + xmlsitemap_rebuild_batch_clear($entity_types, TRUE, $context); + $limit = \Drupal::config('xmlsitemap.settings')->get('batch_limit'); + $link_count = 0; + $chunk_count = 0; + $chunk_size = (int) drush_get_option('limit', $limit); + + // @todo Figure out how to re-use this code with xmlsitemap_rebuild_batch_fetch() + foreach ($entity_types as $entity_type_id) { + $info = xmlsitemap_get_link_info($entity_type_id); + $entity_type = \Drupal::entityTypeManager()->getDefinition($entity_type_id); + + $query = \Drupal::entityQuery($entity_type_id); + $query->addTag('xmlsitemap_link_bundle_access'); + $query->addTag('xmlsitemap_rebuild'); + $query->addMetaData('entity_type_id', $entity_type_id); + $query->addMetaData('entity_info', $info); + + if ($types = xmlsitemap_get_link_type_enabled_bundles($entity_type->id())) { + $bundle_name = $entity_type->getKey('bundle'); + $query->condition($bundle_name, $types, 'IN'); + } + else { + // If no enabled bundle types, skip everything else + continue; + } + + $results = $query->execute(); + + if (empty($results)) { + drush_log(dt('No links to queue for rebuild processing.'), 'ok'); + return; + } + + $ids = $results; + $link_count += count($ids); + $chunks = array_chunk($ids, $chunk_size); + $chunk_count += count($chunks); + foreach ($chunks as $chunk) { + xmlsitemap_link_enqueue($entity_type_id, $chunk); + } + } + + 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).', ['@link_count' => $link_count, '@chunk_count' => $chunk_count, '@chunk_size' => $chunk_size]), 'success'); +} + +/** + * 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 = []; + + $data['type'] = $type; + $data['ids'] = is_array($ids) ? $ids : [$ids]; + + $queue_factory = \Drupal::service('queue'); + /** @var \Drupal\Core\Queue\QueueInterface $queue */ + $queue = $queue_factory->get('xmlsitemap_link_process'); + $queue->createItem($data); +}