Commit 795efcea authored by gbyte.co's avatar gbyte.co

Clean up some

parent 7ca73d2e
......@@ -118,11 +118,11 @@ function simple_sitemap_entity_bundle_delete($entity_type_id, $bundle) {
if (isset($entity_types[$entity_type_id][$bundle])) {
unset($entity_types[$entity_type_id][$bundle]);
$generator->saveConfig('entity_types', $entity_types);
$message = t("You may want to <a href='@url'>regenerate</a> your XML sitemap now.", ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']);
$message = "You may want to <a href='@url'>regenerate</a> your XML sitemap now.";
if ($generator->getSetting('cron_generate')) {
$message .= '</br>' . t('Otherwise the sitemap will be regenerated on the next cron run.');
$message .= ' Otherwise the sitemap will be regenerated on the next cron run.';
}
// drupal_set_message($message); // todo: Commented out, as html code is visible.
drupal_set_message(t($message, ['@url' => $GLOBALS['base_url'] . '/admin/config/search/simplesitemap']));
}
}
......
......@@ -50,5 +50,6 @@ services:
logger.channel.simple_sitemap:
parent: logger.channel_base
prublic: false
arguments:
- simple_sitemap
......@@ -56,7 +56,7 @@ class Batch {
batch_set($this->batch);
$this->batch =& batch_get();
$this->batch['progressive'] = FALSE;
drush_log(t(self::BATCH_INIT_MESSAGE), 'status');
drush_log($this->t(self::BATCH_INIT_MESSAGE), 'status');
drush_backend_batch_process();
break;
......@@ -78,7 +78,7 @@ class Batch {
$operation[1][] = &$context;
call_user_func_array($operation[0], $operation[1]);
}
self::finishGeneration(TRUE, $context['results'], []);
$this->finishGeneration(TRUE, $context['results'], []);
break;
}
}
......@@ -103,7 +103,7 @@ class Batch {
* @param array &$context
*/
public static function generateBundleUrls($entity_info, $batch_info, &$context) {
BatchUrlGenerator::service()
\Drupal::service('simple_sitemap.batch_url_generator')
->setContext($context)
->setBatchInfo($batch_info)
->generateBundleUrls($entity_info);
......@@ -119,7 +119,7 @@ class Batch {
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function generateCustomUrls($custom_paths, $batch_info, &$context) {
BatchUrlGenerator::service()
\Drupal::service('simple_sitemap.batch_url_generator')
->setContext($context)
->setBatchInfo($batch_info)
->generateCustomUrls($custom_paths);
......@@ -135,7 +135,7 @@ class Batch {
* @see https://api.drupal.org/api/drupal/core!includes!form.inc/group/batch/8
*/
public static function finishGeneration($success, $results, $operations) {
BatchUrlGenerator::service()
\Drupal::service('simple_sitemap.batch_url_generator')
->finishGeneration($success, $results, $operations);
}
......
......@@ -20,6 +20,7 @@ class BatchUrlGenerator {
const PATH_DOES_NOT_EXIST_OR_NO_ACCESS_MESSAGE = "The path @path has been omitted from the XML sitemap as it either does not exist, or it is not accessible to anonymous users.";
const PROCESSING_PATH_MESSAGE = 'Processing path #@current out of @max: @path';
const REGENERATION_FINISHED_MESSAGE = "The <a href='@url' target='_blank'>XML sitemap</a> has been regenerated for all languages.";
const REGENERATION_FINISHED_ERROR_MESSAGE = 'The sitemap generation finished with an error.';
protected $sitemapGenerator;
protected $languageManager;
......@@ -64,22 +65,6 @@ class BatchUrlGenerator {
$this->anonUser = $this->entityTypeManager->getStorage('user')->load(self::ANONYMOUS_USER_ID);
}
/**
* The Drupal batch API can only call procedural functions or static methods.
* To circumvent exclusively procedural code, on every batch iteration this
* static method is called by the batch API and returns a freshly created
* Drupal service object of this class. All following calls can be made on
* the returned service the OOP way. This is is obviously trading performance
* for cleanness. The service is created within its own class to improve
* testability.
*
* @return object
* Symfony service object of this class
*/
public static function service() {
return \Drupal::service('simple_sitemap.batch_url_generator');
}
/**
* @param $context
* @return $this
......@@ -146,8 +131,9 @@ class BatchUrlGenerator {
continue;
}
// Do not include paths that have been already indexed.
$path = $url_object->getInternalPath();
// Do not include paths that have been already indexed.
if ($this->batchInfo['remove_duplicates'] && $this->pathProcessed($path)) {
continue;
}
......@@ -414,11 +400,13 @@ class BatchUrlGenerator {
$this->sitemapGenerator->generateSitemap($results['generate'], $remove_sitemap);
}
Cache::invalidateTags(['simple_sitemap']);
drupal_set_message($this->t(self::REGENERATION_FINISHED_MESSAGE,
['@url' => $GLOBALS['base_url'] . '/sitemap.xml']));
$this->logger->m(self::REGENERATION_FINISHED_MESSAGE,
['@url' => $GLOBALS['base_url'] . '/sitemap.xml'])
->display('status')
->log('info');
}
else {
$this->logger->m('The sitemap generation finished with an error.')
$this->logger->m(self::REGENERATION_FINISHED_ERROR_MESSAGE)
->display('error', 'administer sitemap settings')
->log('error');
}
......
......@@ -54,7 +54,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
foreach ($this->getCustomLinks($form_state->getValue('custom_links')) as $i => $link_config) {
foreach ($this->getCustomLinksFromString($form_state->getValue('custom_links')) as $i => $link_config) {
$placeholders = ['@line' => ++$i, '@path' => $link_config['path'], '@priority' => isset($link_config['priority']) ? $link_config['priority'] : ''];
// Checking if internal path exists.
......@@ -80,7 +80,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$custom_links = $this->getCustomLinks($form_state->getValue('custom_links'));
$custom_links = $this->getCustomLinksFromString($form_state->getValue('custom_links'));
$this->generator->removeCustomLinks();
foreach ($custom_links as $link_config) {
$this->generator->addCustomLink($link_config['path'], $link_config);
......@@ -97,7 +97,7 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
* @param $custom_links_string
* @return array
*/
private function getCustomLinks($custom_links_string) {
private function getCustomLinksFromString($custom_links_string) {
// Unify newline characters and explode into array.
$custom_links_string_lines = explode("\n", str_replace("\r\n", "\n", $custom_links_string));
// Remove empty values and whitespaces from array.
......@@ -112,5 +112,4 @@ class SimplesitemapCustomLinksForm extends SimplesitemapFormBase {
}
return $custom_links;
}
}
......@@ -96,6 +96,8 @@ class SimplesitemapTest extends WebTestBase {
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS');
$this->assertText('node/' . $this->node->id());
// Verify the sitemap is taken from cache on second call and node is in the sitemap.
$this->drupalGet('sitemap.xml');
$this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT');
$this->assertText('node/' . $this->node->id());
......
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