diff --git a/core/includes/config.inc b/core/includes/config.inc index b438977..3551073 100644 --- a/core/includes/config.inc +++ b/core/includes/config.inc @@ -55,6 +55,65 @@ function config_install_default_config($module) { } /** + * Transfers a set of variables into a config file. + * + * @param $config_name + * Name of the configuration object. + * @param $variables + * Array with the variable name as the key and the default value as the value. + * @param $name_mapping + * Callback mapping variable names to config parameter names. + */ +function config_transfer_variables_to_config($config_name, $variables, $name_mapping = NULL) { + $config = config($config_name); + if (!$name_mapping) + { + $name_mapping = function ($x) { return $x; }; + } + + // Set all variables stored in the variable table. + $names = array_keys($variables); + $stored_names = array(); + $stored_variables = db_select('variable', 'v') + ->fields('v', array('name', 'value')) + ->condition('v.name', $names) + ->execute() + ->fetchAllKeyed(); + foreach ($stored_variables as $name => $value) + { + $stored_names[] = $name; + $config->set($name_mapping($name), $value); + } + + // Set all variables which rely on a default value. + $default_names = array_diff($names, $stored_names); + foreach ($default_names as $name) + { + $config->set($name_mapping($name), $variables[$name]); + } + + // Save. + $config->save(); +} + +/** + * Generates the default name mapping from variables to config for a module. + * + * @see config_transfer_variables_to_config() + * + * @param $module + * Name of the module. + * + * @return + * Callback mapping variable names to config parameter names. + */ +function _config_default_name_mapping($module) { + return function ($variable_name) use ($module) { + return substr($variable_name, strlen($module) + 1); + }; +} + +/** * Retrieves an iterable array which lists the children under a config 'branch'. * * Given the following configuration files: diff --git a/core/includes/errors.inc b/core/includes/errors.inc index 0524170..12d773a 100644 --- a/core/includes/errors.inc +++ b/core/includes/errors.inc @@ -163,7 +163,7 @@ function _drupal_render_exception_safe($exception) { * TRUE if an error should be displayed. */ function error_displayable($error = NULL) { - $error_level = variable_get('error_level', ERROR_REPORTING_DISPLAY_ALL); + $error_level = config('system.logging')->get('error_level'); $updating = (defined('MAINTENANCE_MODE') && MAINTENANCE_MODE == 'update'); $all_errors_displayed = ($error_level == ERROR_REPORTING_DISPLAY_ALL); $error_needs_display = ($error_level == ERROR_REPORTING_DISPLAY_SOME && diff --git a/core/modules/dblog/config/dblog.logging.xml b/core/modules/dblog/config/dblog.logging.xml new file mode 100644 index 0000000..3127015 --- /dev/null +++ b/core/modules/dblog/config/dblog.logging.xml @@ -0,0 +1,4 @@ + + + 1000 + diff --git a/core/modules/dblog/dblog.install b/core/modules/dblog/dblog.install index 23f85ba..69dc011 100644 --- a/core/modules/dblog/dblog.install +++ b/core/modules/dblog/dblog.install @@ -92,8 +92,9 @@ function dblog_schema() { } /** - * Implements hook_uninstall(). - */ -function dblog_uninstall() { - variable_del('dblog_row_limit'); + * Update settings to the new configuration system. + **/ +function dblog_update_8000() { + $variables = array('dblog_row_limit' => 1000); + config_transfer_variables_to_config('dblog.logging', $variables, _config_default_name_mapping('dblog')); } diff --git a/core/modules/dblog/dblog.module b/core/modules/dblog/dblog.module index a43e0a5..96d7c95 100644 --- a/core/modules/dblog/dblog.module +++ b/core/modules/dblog/dblog.module @@ -102,7 +102,7 @@ function dblog_init() { */ function dblog_cron() { // Cleanup the watchdog table. - $row_limit = variable_get('dblog_row_limit', 1000); + $row_limit = config('dblog.logging')->get('row_limit'); // For row limit n, get the wid of the nth row in descending wid order. // Counting the most recent n rows avoids issues with wid number sequences, @@ -160,14 +160,27 @@ function dblog_watchdog(array $log_entry) { * Implements hook_form_FORM_ID_alter(). */ function dblog_form_system_logging_settings_alter(&$form, $form_state) { - $form['dblog_row_limit'] = array( + $form['row_limit'] = array( '#type' => 'select', '#title' => t('Database log messages to keep'), - '#default_value' => variable_get('dblog_row_limit', 1000), + '#default_value' => config('dblog.logging')->get('row_limit'), '#options' => array(0 => t('All')) + drupal_map_assoc(array(100, 1000, 10000, 100000, 1000000)), '#description' => t('The maximum number of messages to keep in the database log. Requires a cron maintenance task.', array('@cron' => url('admin/reports/status'))) ); $form['actions']['#weight'] = 1; + + $form['#submit'][] = 'dblog_logging_settings_submit'; +} + +/** + * Form submission handler for dblog logging settings. + * + * @ingroup forms + */ +function dblog_logging_settings_submit($form, &$form_state) { + config('dblog.logging') + ->set('row_limit', $form_state['values']['row_limit']) + ->save(); } /** diff --git a/core/modules/dblog/dblog.test b/core/modules/dblog/dblog.test index 77765d7..494b3b3 100644 --- a/core/modules/dblog/dblog.test +++ b/core/modules/dblog/dblog.test @@ -55,16 +55,13 @@ class DBLogTestCase extends DrupalWebTestCase { private function verifyRowLimit($row_limit) { // Change the dblog row limit. $edit = array(); - $edit['dblog_row_limit'] = $row_limit; + $edit['row_limit'] = $row_limit; $this->drupalPost('admin/config/development/logging', $edit, t('Save configuration')); $this->assertResponse(200); // Check row limit variable. - $current_limit = variable_get('dblog_row_limit', 1000); + $current_limit = config('dblog.logging')->get('row_limit'); $this->assertTrue($current_limit == $row_limit, t('[Cache] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit))); - // Verify dblog row limit equals specified row limit. - $current_limit = unserialize(db_query("SELECT value FROM {variable} WHERE name = :dblog_limit", array(':dblog_limit' => 'dblog_row_limit'))->fetchField()); - $this->assertTrue($current_limit == $row_limit, t('[Variable table] Row limit variable of @count equals row limit of @limit', array('@count' => $current_limit, '@limit' => $row_limit))); } /** @@ -474,7 +471,7 @@ class DBLogTestCase extends DrupalWebTestCase { $count = $this->getTypeCount($types); $this->assertEqual(array_sum($count), $type['count'], 'Count matched'); } - + // Clear all logs and make sure the confirmation message is found. $this->drupalPost('admin/reports/dblog', array(), t('Clear log messages')); $this->assertText(t('Database log cleared.'), t('Confirmation message found')); diff --git a/core/modules/syslog/config/syslog.logging.xml b/core/modules/syslog/config/syslog.logging.xml new file mode 100644 index 0000000..4b316dc --- /dev/null +++ b/core/modules/syslog/config/syslog.logging.xml @@ -0,0 +1,5 @@ + + + drupal + !base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message + diff --git a/core/modules/syslog/syslog.install b/core/modules/syslog/syslog.install index 12ff4fb..6383815 100644 --- a/core/modules/syslog/syslog.install +++ b/core/modules/syslog/syslog.install @@ -6,10 +6,24 @@ */ /** - * Implements hook_uninstall(). + * Implements hook_install(). */ -function syslog_uninstall() { - variable_del('syslog_identity'); - variable_del('syslog_facility'); - variable_del('syslog_format'); +function syslog_install() { + // The default facility setting depends on the operating system, so it needs + // to be set dynamically during installation. + config('syslog.logging') + ->set('facility', defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER) + ->save(); +} + +/** + * Update settings to the new configuration system. + **/ +function syslog_update_8000() { + $variables = array( + 'syslog_facility' => defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER, + 'syslog_format' => '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message', + 'syslog_identity' => 'drupal', + ); + config_transfer_variables_to_config('syslog.logging', $variables, _config_default_name_mapping('syslog')); } diff --git a/core/modules/syslog/syslog.module b/core/modules/syslog/syslog.module index 1327606..7019962 100644 --- a/core/modules/syslog/syslog.module +++ b/core/modules/syslog/syslog.module @@ -45,29 +45,45 @@ function syslog_help($path, $arg) { * Implements hook_form_FORM_ID_alter(). */ function syslog_form_system_logging_settings_alter(&$form, &$form_state) { + $config = config('syslog.logging'); $help = module_exists('help') ? ' ' . l(t('More information'), 'admin/help/syslog') . '.' : NULL; - $form['syslog_identity'] = array( + $form['identity'] = array( '#type' => 'textfield', '#title' => t('Syslog identity'), - '#default_value' => variable_get('syslog_identity', 'drupal'), + '#default_value' => $config->get('identity'), '#description' => t('A string that will be prepended to every message logged to Syslog. If you have multiple sites logging to the same Syslog log file, a unique identity per site makes it easy to tell the log entries apart.') . $help, ); if (defined('LOG_LOCAL0')) { - $form['syslog_facility'] = array( + $form['facility'] = array( '#type' => 'select', '#title' => t('Syslog facility'), - '#default_value' => variable_get('syslog_facility', LOG_LOCAL0), + '#default_value' => $config->get('facility'), '#options' => syslog_facility_list(), '#description' => t('Depending on the system configuration, Syslog and other logging tools use this code to identify or filter messages from within the entire system log.') . $help, ); } - $form['syslog_format'] = array( + $form['format'] = array( '#type' => 'textarea', '#title' => t('Syslog format'), - '#default_value' => variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'), + '#default_value' => $config->get('format'), '#description' => t('Specify the format of the syslog entry. Available variables are:
!base_url
Base URL of the site.
!timestamp
Unix timestamp of the log entry.
!type
The category to which this message belongs.
!ip
IP address of the user triggering the message.
!request_uri
The requested URI.
!referer
HTTP Referer if available.
!uid
User ID.
!link
A link to associate with the message.
!message
The message to store in the log.
'), ); $form['actions']['#weight'] = 1; + + $form['#submit'][] = 'syslog_logging_settings_submit'; +} + +/** + * Form submission handler for logging settings. + * + * @ingroup forms + */ +function syslog_logging_settings_submit($form, &$form_state) { + config('syslog.logging') + ->set('identity', $form_state['values']['identity']) + ->set('facility', $form_state['values']['facility']) + ->set('format', $form_state['values']['format']) + ->save(); } /** @@ -95,14 +111,14 @@ function syslog_watchdog(array $log_entry) { global $base_url; $log_init = &drupal_static(__FUNCTION__, FALSE); + $config = config('syslog.logging'); if (!$log_init) { $log_init = TRUE; - $default_facility = defined('LOG_LOCAL0') ? LOG_LOCAL0 : LOG_USER; - openlog(variable_get('syslog_identity', 'drupal'), LOG_NDELAY, variable_get('syslog_facility', $default_facility)); + openlog($config->get('identity'), LOG_NDELAY, $config->get('facility')); } - $message = strtr(variable_get('syslog_format', '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'), array( + $message = strtr($config->get('format'), array( '!base_url' => $base_url, '!timestamp' => $log_entry['timestamp'], '!type' => $log_entry['type'], diff --git a/core/modules/syslog/syslog.test b/core/modules/syslog/syslog.test index 1f7ab2e..b5dc13e 100644 --- a/core/modules/syslog/syslog.test +++ b/core/modules/syslog/syslog.test @@ -31,7 +31,7 @@ class SyslogTestCase extends DrupalWebTestCase { $edit = array(); // If we're on Windows, there is no configuration form. if (defined('LOG_LOCAL6')) { - $this->drupalPost('admin/config/development/logging', array('syslog_facility' => LOG_LOCAL6), t('Save configuration')); + $this->drupalPost('admin/config/development/logging', array('facility' => LOG_LOCAL6), t('Save configuration')); $this->assertText(t('The configuration options have been saved.')); $this->drupalGet('admin/config/development/logging'); diff --git a/core/modules/system/config/system.logging.xml b/core/modules/system/config/system.logging.xml new file mode 100644 index 0000000..b1a6936 --- /dev/null +++ b/core/modules/system/config/system.logging.xml @@ -0,0 +1,4 @@ + + + 2 + diff --git a/core/modules/system/system.admin.inc b/core/modules/system/system.admin.inc index aac0c3d..339b5f2 100644 --- a/core/modules/system/system.admin.inc +++ b/core/modules/system/system.admin.inc @@ -1622,7 +1622,7 @@ function system_logging_settings() { $form['error_level'] = array( '#type' => 'radios', '#title' => t('Error messages to display'), - '#default_value' => variable_get('error_level', ERROR_REPORTING_DISPLAY_ALL), + '#default_value' => config('system.logging')->get('error_level'), '#options' => array( ERROR_REPORTING_HIDE => t('None'), ERROR_REPORTING_DISPLAY_SOME => t('Errors and warnings'), @@ -1631,7 +1631,24 @@ function system_logging_settings() { '#description' => t('It is recommended that sites running on production environments do not display any errors.'), ); - return system_settings_form($form); + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save configuration'), + ); + return $form; +} + +/** + * Form submission handler for system_logging_settings(). + * + * @ingroup forms + */ +function system_logging_settings_submit($form, &$form_state) { + config('system.logging') + ->set('error_level', $form_state['values']['error_level']) + ->save(); + drupal_set_message('The configuration options have been saved.'); } /** diff --git a/core/modules/system/system.install b/core/modules/system/system.install index e3517d7..f71929d 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1847,6 +1847,14 @@ function system_update_8007() { } /** + * Update settings to the new configuration system. + */ +function system_update_8008() { + $variables = array('error_level' => 2); + config_transfer_variables_to_config('system.logging', $variables); +} + +/** * @} End of "defgroup updates-7.x-to-8.x" * The next series of updates should start at 9000. */ diff --git a/core/modules/system/tests/error.test b/core/modules/system/tests/error.test index ead3526..7dea5b9 100644 --- a/core/modules/system/tests/error.test +++ b/core/modules/system/tests/error.test @@ -20,6 +20,7 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { * Test the error handler. */ function testErrorHandler() { + $config = config('system.logging'); $error_notice = array( '%type' => 'Notice', '!message' => 'Undefined variable: bananas', @@ -40,7 +41,7 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { ); // Set error reporting to collect notices. - variable_set('error_level', ERROR_REPORTING_DISPLAY_ALL); + $config->set('error_level', ERROR_REPORTING_DISPLAY_ALL)->save(); $this->drupalGet('error-test/generate-warnings'); $this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertErrorMessage($error_notice); @@ -48,7 +49,7 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { $this->assertErrorMessage($error_user_notice); // Set error reporting to not collect notices. - variable_set('error_level', ERROR_REPORTING_DISPLAY_SOME); + $config->set('error_level', ERROR_REPORTING_DISPLAY_SOME)->save(); $this->drupalGet('error-test/generate-warnings'); $this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertNoErrorMessage($error_notice); @@ -56,7 +57,7 @@ class DrupalErrorHandlerUnitTest extends DrupalWebTestCase { $this->assertErrorMessage($error_user_notice); // Set error reporting to not show any errors. - variable_set('error_level', ERROR_REPORTING_HIDE); + $config->set('error_level', ERROR_REPORTING_HIDE)->save(); $this->drupalGet('error-test/generate-warnings'); $this->assertResponse(200, t('Received expected HTTP status code.')); $this->assertNoErrorMessage($error_notice);