diff --git a/config/install/xmlsitemap.settings.yml b/config/install/xmlsitemap.settings.yml index 32d03ac19c6b562c85a4196d3ceec5d1e6b4728d..9156de370001ca25fc127683f1e2433030c02824 100644 --- a/config/install/xmlsitemap.settings.yml +++ b/config/install/xmlsitemap.settings.yml @@ -9,7 +9,5 @@ frontpage_changefreq: 86400 lastmod_format: 'Y-m-d\TH:i\Z' gz: false clean_url: 0 -cron_threshold_error: 1209600 -cron_threshold_warning: 172800 disable_cron_regeneration: false i18n_selection_mode: 'simple' diff --git a/config/schema/xmlsitemap.schema.yml b/config/schema/xmlsitemap.schema.yml index df2a7a2829e47a0fab722918252e66752fd8ca4c..e0fb12b8e807264129df6ff4d9a084a547107bc8 100644 --- a/config/schema/xmlsitemap.schema.yml +++ b/config/schema/xmlsitemap.schema.yml @@ -37,12 +37,6 @@ xmlsitemap.settings: clean_url: type: integer label: 'Insert clean urls into sitemap' - cron_threshold_error: - type: integer - label: 'Threshold error for cron job' - cron_threshold_warning: - type: integer - label: 'Threshold warning for cron job' disable_cron_regeneration: type: boolean label: 'Disable cron generation of sitemap files' diff --git a/src/Tests/XmlSitemapFunctionalTest.php b/src/Tests/XmlSitemapFunctionalTest.php index de1bec79e5e1711f022619e6016a6a725f11bf6a..def03e861616695ff1035d205d367c97378ff401 100644 --- a/src/Tests/XmlSitemapFunctionalTest.php +++ b/src/Tests/XmlSitemapFunctionalTest.php @@ -77,6 +77,8 @@ class XmlSitemapFunctionalTest extends XmlSitemapTestBase { * report. */ public function testStatusReport() { + $cron_warning_threshold = $this->config('system.cron')->get('threshold.requirements_warning'); + // Test the rebuild flag. $this->drupalLogin($this->admin_user); $this->state->set('xmlsitemap_generated_last', REQUEST_TIME); @@ -86,9 +88,18 @@ class XmlSitemapFunctionalTest extends XmlSitemapTestBase { $this->assertResponse(200); $this->state->set('xmlsitemap_rebuild_needed', FALSE); $this->assertNoXMLSitemapProblems(); + + // Test the regenerate flag (and cron has run recently). + $this->state->set('xmlsitemap_regenerate_needed', TRUE); + $this->state->set('xmlsitemap_generated_last', REQUEST_TIME - $cron_warning_threshold - 600); + $this->state->set('system.cron_last', REQUEST_TIME - $cron_warning_threshold + 600); + $this->assertNoXMLSitemapProblems(); + // Test the regenerate flag (and cron hasn't run in a while). $this->state->set('xmlsitemap_regenerate_needed', TRUE); - $this->state->set('xmlsitemap_generated_last', REQUEST_TIME - $this->config->get('cron_threshold_warning') - 100); + $this->state->set('system.cron_last', 0); + $this->state->set('install_time', 0); + $this->state->set('xmlsitemap_generated_last', REQUEST_TIME - $cron_warning_threshold - 600); $this->assertXMLSitemapProblems(t('The XML cached files are out of date and need to be regenerated. You can run cron manually to regenerate the sitemap files.')); $this->clickLink(t('run cron manually')); $this->assertResponse(200); diff --git a/xmlsitemap.install b/xmlsitemap.install index 889919b16921b03a70bdcdd2a87dc3a699037a86..8aa05d4bd06c538419c3857edd103d88d867f320 100644 --- a/xmlsitemap.install +++ b/xmlsitemap.install @@ -73,7 +73,7 @@ function xmlsitemap_requirements($phase) { $requirements['xmlsitemap_directory']['description'] = $t('The following directories were not found or are not writable by the server. See <a href="@docpage">@docpage</a> for more information. @directories', ['@directories' => \Drupal::service('renderer')->renderPlain($items), '@docpage' => 'https://www.drupal.org/node/244924']); } } - $sitemaps = xmlsitemap_sitemap_load_multiple(); + $sitemaps = \Drupal::entityTypeManager()->getStorage('xmlsitemap')->loadMultiple(); $max_links = -1; $max_chunks = -1; $max_filesize = -1; @@ -135,20 +135,36 @@ function xmlsitemap_requirements($phase) { $requirements['xmlsitemap_generated']['description'] = $t('The XML sitemap data is out of sync and needs to be <a href="@link-rebuild">completely rebuilt<a>.', ['@link-rebuild' => Url::fromRoute('xmlsitemap.admin_rebuild')->toString()]); } elseif (\Drupal::state()->get('xmlsitemap_regenerate_needed')) { + $last_run = $generated_last; + // If cron regeneration is enabled, factor in the last time cron was run + // because the regenerate flag might have been set between the last cron + // run and now. + if (!\Drupal::config('xmlsitemap.settings')->get('disable_cron_regeneration')) { + $last_run = max($generated_last, \Drupal::state()->get('system.cron_last', 0), \Drupal::state()->get('install_time', 0)); + } + $last_run_ago = REQUEST_TIME - $last_run; + $cron_warning_threshold = \Drupal::config('system.cron')->get('threshold.requirements_warning'); + $cron_error_threshold = \Drupal::config('system.cron')->get('threshold.requirements_error'); if ($max_filesize == 0) { // A maximum sitemap file size of 0 indicates an error in generation. $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR; } - elseif ($generated_ago >= \Drupal::config('xmlsitemap.settings')->get('cron_threshold_error')) { + elseif ($last_run_ago >= $cron_error_threshold) { $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR; } - elseif ($generated_ago >= \Drupal::config('xmlsitemap.settings')->get('cron_threshold_warning')) { + elseif ($last_run_ago >= $cron_warning_threshold) { $requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_WARNING; } if ($requirements['xmlsitemap_generated']['severity']) { - $requirements['xmlsitemap_generated']['description'] = $t('The XML cached files are out of date and need to be regenerated. You can <a href="@link-cron">run cron manually</a> to regenerate the sitemap files.', [ - '@link-cron' => Url::fromRoute('system.run_cron', [], ['query' => drupal_get_destination()])->toString(), - ]); + if (\Drupal::config('xmlsitemap.settings')->get('disable_cron_regeneration')) { + // Don't show the link to run cron if cron regeneration is disabled. + $requirements['xmlsitemap_generated']['description'] = $t('The XML cached files are out of date and need to be regenerated.'); + } + else { + $requirements['xmlsitemap_generated']['description'] = $t('The XML cached files are out of date and need to be regenerated. You can <a href="@link-cron">run cron manually</a> to regenerate the sitemap files.', [ + '@link-cron' => Url::fromRoute('system.run_cron', [], ['query' => drupal_get_destination()])->toString(), + ]); + } } } $anonymous_accout = new AnonymousUserSession();