diff --git a/core/includes/update.inc b/core/includes/update.inc index acdf83e..788ac7d 100644 --- a/core/includes/update.inc +++ b/core/includes/update.inc @@ -1237,6 +1237,33 @@ function update_variables_to_config($config_name, array $variable_map) { } /** + * Updates 7.x variables to state records. + * + * Provides a generalized method to migrate variables from 7.x to 8.x's + * state() system. + * + * @param array $variable_map + * An associative array that maps old variables names to new state record + * names; e.g.: + * @code + * array('old_variable' => 'extension.new_name') + * @endcode + * This would migrate the value contained in variable name 'old_variable' into + * the state item 'extension.new_name'. + * Non-existing variables and variables with NULL values are omitted. + */ +function update_variables_to_state(array $variable_map) { + foreach ($variable_map as $variable_name => $state_name) { + if (NULL !== $value = update_variable_get($variable_name)) { + state()->set($state_name, $value); + } + } + + // Delete the migrated variables. + db_delete('variable')->condition('name', array_keys($variable_map), 'IN')->execute(); +} + +/** * Helper function to update entities with uuid. * * @param array $sandbox diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php new file mode 100644 index 0000000..04d065c --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/StateSystemUpgradePathTest.php @@ -0,0 +1,53 @@ + 'State system upgrade test', + 'description' => 'Tests upgrade of system variables to the state system.', + 'group' => 'Upgrade path', + ); + } + + public function setUp() { + $this->databaseDumpFiles = array( + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.bare.standard_all.database.php.gz', + drupal_get_path('module', 'system') . '/tests/upgrade/drupal-7.state.system.database.php', + ); + parent::setUp(); + } + + /** + * Tests upgrade of system variables to state system. + */ + public function testSystemVariableUpgrade() { + $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + + $expected_state = array(); + + $expected_state['update.last_check'] = array( + 'value' => 1304208000, + 'variable_name' => 'update_last_check', + ); + $expected_state['update.last_email_notification'] = array( + 'value' => 1304208000, + 'variable_name' => 'update_last_email_notification', + ); + + foreach ($expected_state as $name => $data) { + $this->assertIdentical(state()->get($name), $data['value']); + $deleted = !db_query('SELECT value FROM {variable} WHERE name = :name', array(':name' => $data['variable_name']))->fetchField(); + $this->assertTrue($deleted, format_string('Variable !name has been deleted.', array('!name' => $data['variable_name']))); + } + } +} diff --git a/core/modules/system/tests/upgrade/drupal-7.state.system.database.php b/core/modules/system/tests/upgrade/drupal-7.state.system.database.php new file mode 100644 index 0000000..8e6fdcb --- /dev/null +++ b/core/modules/system/tests/upgrade/drupal-7.state.system.database.php @@ -0,0 +1,20 @@ +key(array('name' => 'update_last_check'))->fields(array('value' => serialize(1304208000))) + ->execute(); +db_merge('variable') + ->key(array('name' => 'update_last_email_notification'))->fields(array('value' => serialize(1304208000))) + ->execute(); +// Add non-default system settings. diff --git a/core/modules/update/update.fetch.inc b/core/modules/update/update.fetch.inc index a4d1bd7..d5f3593 100644 --- a/core/modules/update/update.fetch.inc +++ b/core/modules/update/update.fetch.inc @@ -186,7 +186,7 @@ function _update_process_fetch_task($project) { _update_cache_set('fetch_failures', $fail, $now + (60 * 5)); // Whether this worked or not, we did just (try to) check for updates. - variable_set('update_last_check', $now); + state()->set('update.last_check', $now); // Now that we processed the fetch task for this project, clear out the // record in {cache_update} for this task so we're willing to fetch again. @@ -378,7 +378,7 @@ function _update_cron_notify() { // Track when the last mail was successfully sent to avoid sending // too many e-mails. if ($message['result']) { - variable_set('update_last_email_notification', REQUEST_TIME); + state()->set('update.last_email_notification', REQUEST_TIME); } } } diff --git a/core/modules/update/update.install b/core/modules/update/update.install index 14efc3a..b767e25 100644 --- a/core/modules/update/update.install +++ b/core/modules/update/update.install @@ -162,3 +162,16 @@ function update_update_8000() { 'update_notification_threshold' => 'notification.threshold', )); } + +/** + * Convert update_last_check, last_email_notification variables to state api value. + * + * @ingroup config_upgrade + */ +function update_update_8001() { + $variable_map = array( + 'update_last_check' => 'update.last_check', + 'update_last_email_notification' => 'update.last_email_notification' + ); + update_variables_to_state($variable_map); +} diff --git a/core/modules/update/update.manager.inc b/core/modules/update/update.manager.inc index 5720af7..062ffca 100644 --- a/core/modules/update/update.manager.inc +++ b/core/modules/update/update.manager.inc @@ -278,7 +278,7 @@ function update_manager_update_form($form, $form_state = array(), $context) { */ function theme_update_manager_update_form($variables) { $form = $variables['form']; - $last = variable_get('update_last_check', 0); + $last = state()->get('update.last_check') ?: 0; $output = theme('update_last_check', array('last' => $last)); $output .= drupal_render_children($form); return $output; diff --git a/core/modules/update/update.module b/core/modules/update/update.module index d9aa464..865caea 100644 --- a/core/modules/update/update.module +++ b/core/modules/update/update.module @@ -285,7 +285,8 @@ function update_cron() { $update_config = config('update.settings'); $frequency = $update_config->get('check.interval_days'); $interval = 60 * 60 * 24 * $frequency; - if ((REQUEST_TIME - variable_get('update_last_check', 0)) > $interval) { + $last_check = state()->get('update.last_check') ?: 0; + if ((REQUEST_TIME - $last_check) > $interval) { // If the configured update interval has elapsed, we want to invalidate // the cached data for all projects, attempt to re-fetch, and trigger any // configured notifications about the new status. @@ -297,7 +298,8 @@ function update_cron() { // missing data, and if so, try to fetch the data. update_get_available(TRUE); } - if ((REQUEST_TIME - variable_get('update_last_email_notification', 0)) > $interval) { + $last_email_notice = state()->get('update.last_email_notification') ?: 0; + if ((REQUEST_TIME - $last_email_notice) > $interval) { // If configured time between notifications elapsed, send email about // updates possibly available. module_load_include('inc', 'update', 'update.fetch'); diff --git a/core/modules/update/update.report.inc b/core/modules/update/update.report.inc index 409284a..607b523 100644 --- a/core/modules/update/update.report.inc +++ b/core/modules/update/update.report.inc @@ -33,7 +33,7 @@ function update_status() { function theme_update_report($variables) { $data = $variables['data']; - $last = variable_get('update_last_check', 0); + $last = state()->get('update.last_check') ?: 0; $output = theme('update_last_check', array('last' => $last)); if (!is_array($data)) {