Commit b5f07c28 authored by andrei.dincu's avatar andrei.dincu

Issue #2249723 by andrei.dincu: Make regenerate_needed and rebuild_needed state variables

parent f09adb00
rebuild_needed: false
regenerate_needed: true
minimum_lifetime: 0
generated_last: 0
xsl: 1
......
......@@ -4,12 +4,6 @@ xmlsitemap.settings:
type: mapping
label: 'XMLSitemap settings'
mapping:
rebuild_needed:
type: boolean
label: 'Sitemap rebuilt'
regenerate_needed:
type: boolean
label: 'Regenerate files'
minimum_lifetime:
type: integer
label: 'Minumum lifetime of an xmlsitemap xml file'
......
......@@ -28,8 +28,8 @@ class XmlSitemapRebuildForm extends ConfigFormBase {
*/
public function buildForm(array $form, array &$form_state) {
$request = $this->getRequest();
if (!$request->request && !\Drupal::config('xmlsitemap.settings')->get('rebuild_needed')) {
if (!\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if (!$request->request && !\Drupal::state()->get('rebuild_needed')) {
if (!\Drupal::state()->get('regenerate_needed')) {
drupal_set_message(t('Your sitemap is up to date and does not need to be rebuilt.'), 'error');
}
else {
......@@ -49,7 +49,7 @@ class XmlSitemapRebuildForm extends ConfigFormBase {
'#description' => t('If no link types are selected, the sitemap files will just be regenerated.'),
'#multiple' => TRUE,
'#options' => $rebuild_types,
'#default_value' => \Drupal::config('xmlsitemap.settings')->get('rebuild_needed') || !\Drupal::state()->get('developer_mode') ? $rebuild_types : array(),
'#default_value' => \Drupal::state()->get('rebuild_needed') || !\Drupal::state()->get('developer_mode') ? $rebuild_types : array(),
'#access' => \Drupal::state()->get('developer_mode'),
);
$form['save_custom'] = array(
......
......@@ -77,15 +77,15 @@ class XmlSitemapFunctionalTest extends XmlSitemapTestBase {
// Test the rebuild flag.
$this->drupalLogin($this->admin_user);
\Drupal::config('xmlsitemap.settings')->set('generated_last', REQUEST_TIME);
\Drupal::config('xmlsitemap.settings')->set('rebuild_needed', TRUE);
\Drupal::config('xmlsitemap.settings')->save();
\Drupal::state()->set('rebuild_needed', TRUE);
$this->assertXMLSitemapProblems(t('The XML sitemap data is out of sync and needs to be completely rebuilt.'));
$this->clickLink(t('completely rebuilt'));
$this->assertResponse(200);
\Drupal::config('xmlsitemap.settings')->set('rebuild_needed', FALSE)->save();
\Drupal::state()->set('rebuild_needed', FALSE);
$this->assertNoXMLSitemapProblems();
//Test the regenerate flag (and cron hasn't run in a while).
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE);
\Drupal::state()->set('regenerate_needed', TRUE);
\Drupal::config('xmlsitemap.settings')->set('generated_last', REQUEST_TIME - \Drupal::config('xmlsitemap.settings')->get('cron_threshold_warning') - 100);
\Drupal::config('xmlsitemap.settings')->save();
$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.'));
......
......@@ -140,7 +140,8 @@ class XmlSitemapNodeFunctionalTest extends XmlSitemapTestBase {
public function testCron() {
$limit = 5;
\Drupal::config('xmlsitemap.settings')->set('batch_limit', $limit)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
$nodes = array();
for ($i = 1; $i <= ($limit + 1); $i++) {
$node = $this->drupalCreateNode();
......
......@@ -15,7 +15,6 @@ use Drupal\simpletest\WebTestBase;
abstract class XmlSitemapTestBase extends WebTestBase {
public static $modules = array('xmlsitemap');
protected $admin_user;
public function setUp() {
......@@ -41,7 +40,7 @@ abstract class XmlSitemapTestBase extends WebTestBase {
* @return
* Assertion result.
*/
protected function assertNoResponse($code, $message = '',$group = 'Browser') {
protected function assertNoResponse($code, $message = '', $group = 'Browser') {
$curl_code = curl_getinfo($this->curlHandle, CURLINFO_HTTP_CODE);
$match = is_array($code) ? in_array($curl_code, $code) : $curl_code == $code;
return $this->assertFalse($match, $message ? $message : t('HTTP response not expected !code, actual !curl_code', array('!code' => $code, '!curl_code' => $curl_code)), t('Browser'));
......@@ -85,11 +84,11 @@ abstract class XmlSitemapTestBase extends WebTestBase {
* Regenerate the sitemap by setting the regenerate flag and running cron.
*/
protected function regenerateSitemap() {
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE);
\Drupal::state()->set('regenerate_needed', TRUE);
\Drupal::config('xmlsitemap.settings')->set('generated_last', 0);
\Drupal::config('xmlsitemap.settings')->save();
$this->cronRun();
$this->assertTrue(\Drupal::config('xmlsitemap.settings')->get('generated_last') && !\Drupal::config('xmlsitemap.settings')->get('regenerate_needed'), t('XML sitemaps regenerated and flag cleared.'));
$this->assertTrue(\Drupal::config('xmlsitemap.settings')->get('generated_last') && !\Drupal::state()->get('regenerate_needed'), t('XML sitemaps regenerated and flag cleared.'));
}
protected function assertSitemapLink($entity_type, $entity_id = NULL) {
......@@ -140,7 +139,7 @@ abstract class XmlSitemapTestBase extends WebTestBase {
}
else {
// Otherwise check simple equality (==).
$this->assertEqual($link[$key], $value, t('Equal values for @type @id link field @key - @a - @b.', array('@type' => $entity_type, '@id' => $entity_id, '@key' => $key,'@a' => $link[$key],'@b' => $value)));
$this->assertEqual($link[$key], $value, t('Equal values for @type @id link field @key - @a - @b.', array('@type' => $entity_type, '@id' => $entity_id, '@key' => $key, '@a' => $link[$key], '@b' => $value)));
}
}
}
......@@ -202,7 +201,13 @@ abstract class XmlSitemapTestBase extends WebTestBase {
$value = xmlsitemap_var($variable);
if ($reset_if_true && $value) {
\Drupal::config('xmlsitemap.settings')->set($variable, FALSE)->save();
$state_variables = xmlsitemap_state_variables();
if (isset($state_variables[$variable])) {
\Drupal::state()->set($variable, FALSE);
}
else {
\Drupal::config('xmlsitemap.settings')->set($variable, FALSE)->save();
}
}
return $this->assertEqual($value, $assert_value, "xmlsitemap_$variable is " . ($assert_value ? 'TRUE' : 'FALSE'));
......@@ -305,7 +310,7 @@ abstract class XmlSitemapTestBase extends WebTestBase {
'@timestamp' => $message->timestamp,
'@severity' => $levels[$message->severity],
'@type' => $message->type,
//'!message' => theme_dblog_message(array('event' => $message, 'link' => FALSE)),
//'!message' => theme_dblog_message(array('event' => $message, 'link' => FALSE)),
));
}
......
......@@ -29,7 +29,7 @@ class XmlSitemapUnitTest extends XmlSitemapTestBase {
}
public function testAssertFlag() {
\Drupal::config('xmlsitemap.settings')->set('rebuild_needed', TRUE)->save();
\Drupal::state()->set('rebuild_needed', TRUE);
$this->assertTrue(xmlsitemap_var('rebuild_needed'));
$this->assertTrue($this->assertFlag('rebuild_needed', TRUE, FALSE));
$this->assertTrue(xmlsitemap_var('rebuild_needed'));
......@@ -205,7 +205,7 @@ class XmlSitemapUnitTest extends XmlSitemapTestBase {
$link1 = $this->addSitemapLink(array('loc' => 'testing1', 'status' => 0));
$link2 = $this->addSitemapLink(array('loc' => 'testing1', 'status' => 1));
$link3 = $this->addSitemapLink(array('status' => 0));
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', FALSE)->save();
\Drupal::state()->set('regenerate_needed', FALSE);
// Test delete multiple links.
// Test that the regenerate flag is set when visible links are deleted.
......@@ -231,7 +231,7 @@ class XmlSitemapUnitTest extends XmlSitemapTestBase {
$links[1] = $this->addSitemapLink(array('subtype' => 'group1'));
$links[2] = $this->addSitemapLink(array('subtype' => 'group1'));
$links[3] = $this->addSitemapLink(array('subtype' => 'group2'));
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', FALSE)->save();
\Drupal::state()->set('regenerate_needed', FALSE);
// id | type | subtype | language | access | status | priority
// 1 | testing | group1 | '' | 1 | 1 | 0.5
// 2 | testing | group1 | '' | 1 | 1 | 0.5
......
......@@ -324,7 +324,7 @@ class XmlSitemapGeneratorService implements XmlSitemapGeneratorInterface {
* {@inheritdoc}
*/
public function regenerateBatchFinished($success, $results, $operations, $elapsed) {
if ($success && !\Drupal::config('xmlsitemap.settings')->get('regenerate_needed', FALSE)) {
if ($success && !\Drupal::state()->get('regenerate_needed', FALSE)) {
\Drupal::config('xmlsitemap.settings')->set('xmlsitemap_generated_last', REQUEST_TIME);
//drupal_set_message(t('The sitemaps were regenerated.'));
// Show a watchdog message that the sitemap was regenerated.
......
......@@ -49,7 +49,7 @@ class XmlSitemapLinkStorage {
$existing = db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = :type AND id = :id", 0, 1, array(':type' => $link['type'], ':id' => $link['id']))->fetchAssoc();
// Check if this is a changed link and set the regenerate flag if necessary.
if (!\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if (!\Drupal::state()->get('regenerate_needed')) {
self::checkChangedLink($link, $existing, TRUE);
}
......@@ -107,7 +107,7 @@ class XmlSitemapLinkStorage {
}
if ($changed && $flag) {
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
}
return $changed;
......@@ -138,7 +138,7 @@ class XmlSitemapLinkStorage {
$changed = $query->execute()->fetchField();
if ($changed && $flag) {
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
}
return $changed;
......@@ -183,7 +183,7 @@ class XmlSitemapLinkStorage {
return FALSE;
}
if (!\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if (!\Drupal::state()->get('regenerate_needed')) {
self::checkChangedLinks($conditions, array(), TRUE);
}
......@@ -213,7 +213,7 @@ class XmlSitemapLinkStorage {
public static function linkUpdateMultiple($updates = array(), $conditions = array(), $check_flag = TRUE) {
// If we are going to modify a visible sitemap link, we will need to set
// the regenerate needed flag.
if ($check_flag && !\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if ($check_flag && !\Drupal::state()->get('regenerate_needed')) {
self::checkChangedLinks($conditions, $updates, TRUE);
}
......
......@@ -348,7 +348,7 @@ function xmlsitemap_regenerate_batch_generate_index($smid, array &$context) {
* Batch callback; sitemap regeneration finished.
*/
function xmlsitemap_regenerate_batch_finished($success, $results, $operations, $elapsed) {
if ($success && \Drupal::config('xmlsitemap.settings')->get('regenerate_needed') == FALSE) {
if ($success && \Drupal::state()->get('regenerate_needed') == FALSE) {
\Drupal::config('xmlsitemap.settings')->set('generated_last', REQUEST_TIME)->save();
drupal_set_message(t('The sitemaps were regenerated.'));
// Show a watchdog message that the sitemap was regenerated.
......@@ -400,8 +400,15 @@ function xmlsitemap_rebuild_batch(array $entities, $save_custom = FALSE) {
* Batch callback; set an array of variables and their values.
*/
function xmlsitemap_batch_variable_set(array $variables) {
$state_variables = xmlsitemap_state_variables();
$config_variables = xmlsitemap_config_variables();
foreach ($variables as $variable => $value) {
\Drupal::config('xmlsitemap.settings')->set($variable, $value);
if (isset($state_variables[$variable])) {
\Drupal::state()->set($variable, $value);
}
else {
\Drupal::config('xmlsitemap.settings')->set($variable, $value);
}
}
\Drupal::config('xmlsitemap.settings')->save();
}
......@@ -483,7 +490,7 @@ function xmlsitemap_rebuild_batch_fetch($entity, &$context) {
* Batch callback; sitemap rebuild finished.
*/
function xmlsitemap_rebuild_batch_finished($success, $results, $operations, $elapsed) {
if ($success && !\Drupal::config('xmlsitemap.settings')->get('rebuild_needed')) {
if ($success && !\Drupal::state()->get('rebuild_needed')) {
drupal_set_message(t('The sitemap links were rebuilt.'));
}
else {
......
......@@ -123,11 +123,11 @@ function xmlsitemap_requirements($phase) {
'value' => $generated_last ? $t('Last attempted generation on !date (!interval ago).', array('!date' => format_date($generated_last, 'small'), '!interval' => format_interval($generated_ago))) : $t('Cached files have not been generated yet.'),
'severity' => REQUIREMENT_OK,
);
if (\Drupal::config('xmlsitemap.settings')->get('rebuild_needed')) {
if (\Drupal::state()->get('rebuild_needed')) {
$requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR;
$requirements['xmlsitemap_generated']['description'] = $t('The XML sitemap data is out of sync and needs to be <a href="@link-rebuild">completely rebuilt<a>.', array('@link-rebuild' => url('admin/config/search/xmlsitemap/rebuild')));
}
elseif (\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
elseif (\Drupal::state()->get('regenerate_needed')) {
if ($max_filesize == 0) {
// A maximum sitemap file size of 0 indicates an error in generation.
$requirements['xmlsitemap_generated']['severity'] = REQUIREMENT_ERROR;
......@@ -313,7 +313,7 @@ function xmlsitemap_enable() {
// Ensure the file cache directory is available and ready.
xmlsitemap_check_directory();
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE);
\Drupal::state()->set('regenerate_needed', TRUE);
}
/**
......@@ -321,12 +321,15 @@ function xmlsitemap_enable() {
*/
function xmlsitemap_uninstall() {
// Remove config variables.
$variables = array_keys(xmlsitemap_variables());
$variables = array_keys(xmlsitemap_config_variables());
foreach ($variables as $variable) {
\Drupal::config('xmlsitemap.settings')->clear($variable);
}
\Drupal::state()->deleteMultiple(array('base_url', 'developer_mode'));
$variables = array_keys(xmlsitemap_state_variables());
foreach ($variables as $variable) {
\Drupal::state()->delete($variable);
}
// Remove the file cache directory.
xmlsitemap_clear_directory(NULL, TRUE);
......@@ -336,12 +339,11 @@ function xmlsitemap_uninstall() {
$state_variables = array();
foreach ($entities as $entity_id => $entity) {
$state_variables[] = 'xmlsitemap_entity_' . $entity_id;
\Drupal::config('xmlsitemap.settings')->clear('xmlsitemap_entity_' . $entity_id);
foreach ($bundles[$entity_id] as $bundle_id => $bundle) {
$state_variables[] = 'xmlsitemap_entity' . $entity_id . '_bundle_' . $bundle_id;
\Drupal::config('xmlsitemap.settings')->clear('xmlsitemap_entity' . $entity_id . '_bundle_' . $bundle_id);
}
}
\Drupal::state()->deleteMultiple($state_variables);
}
/**
......@@ -426,8 +428,8 @@ function xmlsitemap_update_6202() {
\Drupal::config()->clear('languages');
// Ensure that the sitemaps will be refreshed on next cron.
\Drupal::config('xmlsitemap.settings')->set('generated_last', 0);
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE);
\Drupal::config('xmlsitemap.settings')->set('generated_last', 0)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
}
/**
......
......@@ -171,7 +171,7 @@ function _xmlsitemap_rebuild_form_access() {
*/
function xmlsitemap_cron() {
// If there were no new or changed links, skip.
if (!\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if (!\Drupal::state()->get('regenerate_needed')) {
return;
}
......@@ -212,12 +212,10 @@ function xmlsitemap_robotstxt() {
}
/**
* Internal default variables for xmlsitemap_var().
* Internal default variables config for xmlsitemap_var().
*/
function xmlsitemap_variables() {
function xmlsitemap_config_variables() {
return array(
'rebuild_needed' => FALSE,
'regenerate_needed' => FALSE,
'minimum_lifetime' => 0,
'generated_last' => 0,
'xsl' => 1,
......@@ -241,13 +239,26 @@ function xmlsitemap_variables() {
);
}
/**
* Internal default variables state for xmlsitemap_var().
*/
function xmlsitemap_state_variables() {
return array(
'rebuild_needed' => FALSE,
'regenerate_needed' => TRUE,
'base_url' => '',
'developer_mode' => 0
);
}
/**
* Internal implementation of variable_get().
*/
function xmlsitemap_var($name, $default = NULL) {
$defaults = &drupal_static(__FUNCTION__);
if (!isset($defaults)) {
$defaults = xmlsitemap_variables();
$defaults = xmlsitemap_config_variables();
$defaults += xmlsitemap_state_variables();
}
// @todo Remove when stable.
......@@ -255,7 +266,10 @@ function xmlsitemap_var($name, $default = NULL) {
trigger_error(strtr('Default variable for %variable not found.', array('%variable' => drupal_placeholder($name))));
}
return \Drupal::config('xmlsitemap.settings')->get($name);
if (\Drupal::state()->get($name, NULL) === NULL) {
return \Drupal::config('xmlsitemap.settings')->get($name);
}
return \Drupal::state()->get($name);
}
/**
......@@ -362,7 +376,7 @@ function xmlsitemap_sitemap_save(XmlSitemapInterface $sitemap) {
xmlsitemap_directory_move($old_dir, $new_dir);
// Mark the sitemaps as needing regeneration.
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
}
$sitemap->save();
......@@ -521,7 +535,7 @@ function xmlsitemap_link_save(array $link) {
$existing = db_query_range("SELECT loc, access, status, lastmod, priority, changefreq, changecount, language FROM {xmlsitemap} WHERE type = :type AND id = :id", 0, 1, array(':type' => $link['type'], ':id' => $link['id']))->fetchAssoc();
// Check if this is a changed link and set the regenerate flag if necessary.
if (!\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if (!\Drupal::state()->get('regenerate_needed')) {
_xmlsitemap_check_changed_link($link, $existing, TRUE);
}
......@@ -554,7 +568,7 @@ function xmlsitemap_link_save(array $link) {
function xmlsitemap_link_update_multiple($updates = array(), $conditions = array(), $check_flag = TRUE) {
// If we are going to modify a visible sitemap link, we will need to set
// the regenerate needed flag.
if ($check_flag && !\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if ($check_flag && !\Drupal::state()->get('regenerate_needed')) {
_xmlsitemap_check_changed_links($conditions, $updates, TRUE);
}
......@@ -607,7 +621,7 @@ function xmlsitemap_link_delete_multiple(array $conditions) {
return FALSE;
}
if (!\Drupal::config('xmlsitemap.settings')->get('regenerate_needed')) {
if (!\Drupal::state()->get('regenerate_needed')) {
_xmlsitemap_check_changed_links($conditions, array(), TRUE);
}
......@@ -646,7 +660,7 @@ function _xmlsitemap_check_changed_links(array $conditions = array(), array $upd
$changed = $query->execute()->fetchField();
if ($changed && $flag) {
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
}
return $changed;
......@@ -693,7 +707,7 @@ function _xmlsitemap_check_changed_link(array $link, $original_link = NULL, $fla
}
if ($changed && $flag) {
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
}
return $changed;
......@@ -1207,7 +1221,7 @@ function xmlsitemap_form_submit_flag_regenerate($form, $form_state) {
$value = array_keys(array_filter($value));
}
if ($stored_value != 'not_a_variable' && $stored_value != $value) {
\Drupal::config('xmlsitemap.settings')->set('regenerate_needed', TRUE)->save();
\Drupal::state()->set('regenerate_needed', TRUE);
drupal_set_message(t('XML sitemap settings have been modified and the files should be regenerated. You can <a href="@run-cron">run cron manually</a> to regenerate the cached files.', array('@run-cron' => url('admin/reports/status/run-cron', array('query' => drupal_get_destination())))), 'warning', FALSE);
return;
}
......
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