Commit 2ae93cb0 authored by Pawel G's avatar Pawel G

Clean up SitemapGenerator class

parent acdc063d
...@@ -78,28 +78,15 @@ class Batch { ...@@ -78,28 +78,15 @@ class Batch {
} }
/** /**
* Adds operations to the batch of type 'entity_types' or 'custom_paths'. * Adds an operation to the batch.
* *
* @param string $type * @param string $processing_method
* @param array $operations * @param array $data
*/ */
public function addOperations($type, $operations) { public function addOperation($processing_method, $data) {
switch ($type) { $this->batch['operations'][] = [
case 'entity_types': __CLASS__ . '::' . $processing_method, [$data, $this->batchInfo]
foreach ($operations as $operation) { ];
$this->batch['operations'][] = [
__CLASS__ . '::generateBundleUrls',
[$operation['entity_info'], $this->batchInfo]
];
};
break;
case 'custom_paths':
$this->batch['operations'][] = [
__CLASS__ . '::generateCustomUrls',
[$operations, $this->batchInfo]
];
break;
}
} }
/** /**
...@@ -260,13 +247,13 @@ class Batch { ...@@ -260,13 +247,13 @@ class Batch {
self::SetCurrentId($i, $context); self::SetCurrentId($i, $context);
} }
$user_input = $custom_path['path'][0] === '/' ? $custom_path['path'] : '/' . $custom_path['path']; // $user_input = $custom_path['path'][0] === '/' ? $custom_path['path'] : '/' . $custom_path['path']; // Not needed due to checks on form submit
if (!\Drupal::service('path.validator')->isValid($custom_path['path'])) { //todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush. if (!\Drupal::service('path.validator')->isValid($custom_path['path'])) { //todo: Change to different function, as this also checks if current user has access. The user however varies depending if process was started from the web interface or via cron/drush.
self::registerError(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, ['@faulty_path' => $custom_path['path']], 'warning'); self::registerError(self::PATH_DOES_NOT_EXIST_OR_NO_ACCESS, ['@faulty_path' => $custom_path['path']], 'warning');
continue; continue;
} }
$options = ['absolute' => TRUE, 'language' => $languages[Simplesitemap::getDefaultLangId()]]; $options = ['absolute' => TRUE, 'language' => $languages[Simplesitemap::getDefaultLangId()]];
$url_object = Url::fromUserInput($user_input, $options); $url_object = Url::fromUserInput($custom_path['path'], $options);
if (!$url_object->access($batch_info['anonymous_user_account'])) if (!$url_object->access($batch_info['anonymous_user_account']))
continue; continue;
......
...@@ -47,29 +47,35 @@ class SitemapGenerator { ...@@ -47,29 +47,35 @@ class SitemapGenerator {
'remove_duplicates' => $this->generator->getSetting('remove_duplicates'), 'remove_duplicates' => $this->generator->getSetting('remove_duplicates'),
'entity_types' => $this->generator->getConfig('entity_types'), 'entity_types' => $this->generator->getConfig('entity_types'),
]); ]);
$batch->addOperations('custom_paths', $this->batchAddCustomPaths()); // Add custom link generating operation.
$batch->addOperations('entity_types', $this->batchAddEntityTypePaths()); $batch->addOperation('generateCustomUrls', $this->getCustomUrlsData());
// Add entity link generating operations.
foreach($this->getEntityTypeData() as $data) {
$batch->addOperation('generateBundleUrls', $data);
}
$batch->start(); $batch->start();
} }
/** /**
* Returns the custom path generating operation. * Returns a batch-ready data array for custom link generation.
* *
* @return array $operation. * @return array $data
* Data to be processed.
*/ */
private function batchAddCustomPaths() { private function getCustomUrlsData() {
$link_generator = new CustomLinkGenerator(); $link_generator = new CustomLinkGenerator();
return $link_generator->getCustomPaths($this->generator->getConfig('custom')); return $link_generator->getCustomPaths($this->generator->getConfig('custom'));
} }
/** /**
* Collects entity metadata for entities that are set to be indexed * Collects entity metadata for entities that are set to be indexed
* and returns a batch-ready operation. * and returns an array of batch-ready data sets for entity link generation.
* *
* @return array $operations. * @return array $operations.
*/ */
private function batchAddEntityTypePaths() { private function getEntityTypeData() {
$operations = []; $data_sets = [];
$sitemap_entity_types = Simplesitemap::getSitemapEntityTypes(); $sitemap_entity_types = Simplesitemap::getSitemapEntityTypes();
$entity_types = $this->generator->getConfig('entity_types'); $entity_types = $this->generator->getConfig('entity_types');
foreach($entity_types as $entity_type_name => $bundles) { foreach($entity_types as $entity_type_name => $bundles) {
...@@ -78,19 +84,17 @@ class SitemapGenerator { ...@@ -78,19 +84,17 @@ class SitemapGenerator {
$keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix. $keys['bundle'] = $entity_type_name == 'menu_link_content' ? 'menu_name' : $keys['bundle']; // Menu fix.
foreach($bundles as $bundle_name => $bundle_settings) { foreach($bundles as $bundle_name => $bundle_settings) {
if ($bundle_settings['index']) { if ($bundle_settings['index']) {
$operations[] = [ $data_sets[] = [
'entity_info' => [ 'bundle_settings' => $bundle_settings,
'bundle_settings' => $bundle_settings, 'bundle_name' => $bundle_name,
'bundle_name' => $bundle_name, 'entity_type_name' => $entity_type_name,
'entity_type_name' => $entity_type_name, 'keys' => $keys,
'keys' => $keys,
],
]; ];
} }
} }
} }
} }
return $operations; return $data_sets;
} }
/** /**
...@@ -104,7 +108,7 @@ class SitemapGenerator { ...@@ -104,7 +108,7 @@ class SitemapGenerator {
*/ */
public static function generateSitemap($links, $remove_sitemap = FALSE) { public static function generateSitemap($links, $remove_sitemap = FALSE) {
// Invoke alter hook. // Invoke alter hook.
\Drupal::moduleHandler()->alter('simple_sitemap_links', $links); \Drupal::moduleHandler()->alter('simple_sitemap_links', $links);
$values = [ $values = [
'id' => $remove_sitemap ? 1 : \Drupal::service('database')->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1, 'id' => $remove_sitemap ? 1 : \Drupal::service('database')->query('SELECT MAX(id) FROM {simple_sitemap}')->fetchField() + 1,
'sitemap_string' => self::generateSitemapChunk($links), 'sitemap_string' => self::generateSitemapChunk($links),
......
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