From db2f83b6df28aefbafe73e8cb62cd3bea8ba7890 Mon Sep 17 00:00:00 2001
From: Dries <dries@buytaert.net>
Date: Tue, 7 Aug 2012 15:49:48 -0400
Subject: [PATCH] - Patch #1497310 by webchick, kbasarab, tobiasb, pcambra,
 sun, aspilicious: Convert statistics settings to configuration system.

---
 .../Drupal/search/Tests/SearchRankingTest.php |  2 +-
 .../statistics/config/statistics.settings.yml |  9 +++++
 .../statistics/Tests/StatisticsAdminTest.php  | 29 ++++++++------
 .../Tests/StatisticsLoggingTest.php           |  6 ++-
 .../Tests/StatisticsReportsTest.php           |  2 +-
 .../statistics/Tests/StatisticsTestBase.php   |  7 ++--
 core/modules/statistics/statistics.admin.inc  | 30 +++++++++-----
 core/modules/statistics/statistics.install    | 32 ++++++++-------
 core/modules/statistics/statistics.module     | 40 +++++++++++--------
 core/modules/statistics/statistics.php        |  3 +-
 10 files changed, 99 insertions(+), 61 deletions(-)
 create mode 100644 core/modules/statistics/config/statistics.settings.yml

diff --git a/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php b/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php
index d9debc5110de..75e062845147 100644
--- a/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php
+++ b/core/modules/search/lib/Drupal/search/Tests/SearchRankingTest.php
@@ -72,7 +72,7 @@ function testRankings() {
     $this->drupalPost(NULL, $edit, t('Save'));
 
     // Enable counting of statistics.
-    variable_set('statistics_count_content_views', 1);
+    config('statistics.settings')->set('count_content_views', 1)->save();
 
     // Then View one of the nodes a bunch of times.
     // Manually calling statistics.php, simulating ajax behavior.
diff --git a/core/modules/statistics/config/statistics.settings.yml b/core/modules/statistics/config/statistics.settings.yml
new file mode 100644
index 000000000000..2b30aa67483f
--- /dev/null
+++ b/core/modules/statistics/config/statistics.settings.yml
@@ -0,0 +1,9 @@
+access_log:
+    enabled: '0'
+    max_lifetime: '259200'
+count_content_views: '0'
+block:
+    popular:
+        top_day_limit: '0'
+        top_all_limit: '0'
+        top_recent_limit: '0'
diff --git a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php
index 86b446aa6355..d1a8d9c6dc0f 100644
--- a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php
+++ b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsAdminTest.php
@@ -54,8 +54,9 @@ function setUp() {
    * Verifies that the statistics settings page works.
    */
   function testStatisticsSettings() {
-    $this->assertFalse(variable_get('statistics_enable_access_log', 0), t('Access log is disabled by default.'));
-    $this->assertFalse(variable_get('statistics_count_content_views', 0), t('Count content view log is disabled by default.'));
+    $config = config('statistics.settings');
+    $this->assertFalse($config->get('access_log.enabled'), t('Access log is disabled by default.'));
+    $this->assertFalse($config->get('count_content_views'), t('Count content view log is disabled by default.'));
 
     $this->drupalGet('admin/reports/pages');
     $this->assertRaw(t('No statistics available.'), t('Verifying text shown when no statistics is available.'));
@@ -64,8 +65,9 @@ function testStatisticsSettings() {
     $edit['statistics_enable_access_log'] = 1;
     $edit['statistics_count_content_views'] = 1;
     $this->drupalPost('admin/config/system/statistics', $edit, t('Save configuration'));
-    $this->assertTrue(variable_get('statistics_enable_access_log'), t('Access log is enabled.'));
-    $this->assertTrue(variable_get('statistics_count_content_views'), t('Count content view log is enabled.'));
+    $config = config('statistics.settings');
+    $this->assertTrue($config->get('access_log.enabled'), t('Access log is enabled.'));
+    $this->assertTrue($config->get('count_content_views'), t('Count content view log is enabled.'));
 
     // Hit the node.
     $this->drupalGet('node/' . $this->test_node->nid);
@@ -95,7 +97,7 @@ function testStatisticsSettings() {
    * Tests that when a node is deleted, the node counter is deleted too.
    */
   function testDeleteNode() {
-    variable_set('statistics_count_content_views', 1);
+    config('statistics.settings')->set('count_content_views', 1)->save();
 
     $this->drupalGet('node/' . $this->test_node->nid);
     // Manually calling statistics.php, simulating ajax behavior.
@@ -127,7 +129,7 @@ function testDeleteNode() {
    * Tests that accesslog reflects when a user is deleted.
    */
   function testDeleteUser() {
-    variable_set('statistics_enable_access_log', 1);
+    config('statistics.settings')->set('access_log.enabled', 1)->save();
 
     variable_set('user_cancel_method', 'user_cancel_delete');
     $this->drupalLogout($this->privileged_user);
@@ -158,10 +160,12 @@ function testDeleteUser() {
    * Tests that cron clears day counts and expired access logs.
    */
   function testExpiredLogs() {
-    variable_set('statistics_enable_access_log', 1);
-    variable_set('statistics_count_content_views', 1);
+    config('statistics.settings')
+      ->set('access_log.enabled', 1)
+      ->set('count_content_views', 1)
+      ->set('access_log.max_lifetime', 1)
+      ->save();
     variable_set('statistics_day_timestamp', 8640000);
-    variable_set('statistics_flush_accesslog_timer', 1);
 
     $this->drupalGet('node/' . $this->test_node->nid);
     // Manually calling statistics.php, simulating ajax behavior.
@@ -178,9 +182,10 @@ function testExpiredLogs() {
     $this->drupalGet('admin/reports/pages');
     $this->assertText('node/' . $this->test_node->nid, t('Hit URL found.'));
 
-    // statistics_cron will subtract the statistics_flush_accesslog_timer
-    // variable from REQUEST_TIME in the delete query, so wait two secs here to
-    // make sure the access log will be flushed for the node just hit.
+    // statistics_cron() will subtract
+    // statistics.settings:accesslog.max_lifetime config from REQUEST_TIME in
+    // the delete query, so wait two secs here to make sure the access log will
+    // be flushed for the node just hit.
     sleep(2);
     $this->cronRun();
 
diff --git a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsLoggingTest.php b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsLoggingTest.php
index d895564d7957..8ac2fb6398c8 100644
--- a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsLoggingTest.php
+++ b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsLoggingTest.php
@@ -44,8 +44,10 @@ function setUp() {
     $config->save();
 
     // Enable access logging.
-    variable_set('statistics_enable_access_log', 1);
-    variable_set('statistics_count_content_views', 1);
+    config('statistics.settings')
+      ->set('access_log.enabled', 1)
+      ->set('count_content_views', 1)
+      ->save();
 
     // Clear the logs.
     db_truncate('accesslog');
diff --git a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsReportsTest.php b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsReportsTest.php
index 3df7347f5781..184af5dd0fa9 100644
--- a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsReportsTest.php
+++ b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsReportsTest.php
@@ -89,7 +89,7 @@ function testPopularContentBlock() {
     $block->region = 'sidebar_first';
     $block->cache = -1;
     $block->visibility = 0;
-    $edit = array('statistics_block_top_day_num' => 3, 'statistics_block_top_all_num' => 3, 'statistics_block_top_last_num' => 3);
+    $edit = array('statistics_block_popular_top_day_limit' => 3, 'statistics_block_popular_top_all_limit' => 3, 'statistics_block_popular_top_recent_limit' => 3);
     module_invoke('statistics', 'block_save', 'popular', $edit);
     drupal_write_record('block', $block);
 
diff --git a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsTestBase.php b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsTestBase.php
index c0d70ff183f2..c1a4bc1cb5a5 100644
--- a/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsTestBase.php
+++ b/core/modules/statistics/lib/Drupal/statistics/Tests/StatisticsTestBase.php
@@ -35,9 +35,10 @@ function setUp() {
     $this->drupalLogin($this->blocking_user);
 
     // Enable access logging.
-    variable_set('statistics_enable_access_log', 1);
-    variable_set('statistics_count_content_views', 1);
-
+    config('statistics.settings')
+      ->set('access_log.enabled', 1)
+      ->set('count_content_views', 1)
+      ->save();
     // Insert dummy access by anonymous user into access log.
     db_insert('accesslog')
       ->fields(array(
diff --git a/core/modules/statistics/statistics.admin.inc b/core/modules/statistics/statistics.admin.inc
index d84e07bc6c66..176e2af978e4 100644
--- a/core/modules/statistics/statistics.admin.inc
+++ b/core/modules/statistics/statistics.admin.inc
@@ -99,7 +99,7 @@ function statistics_top_pages() {
     $rows[] = array($page->hits, _statistics_format_item($page->title, $page->path), t('%time ms', array('%time' => round($page->average_time))), format_interval(round($page->total_time / 1000)));
   }
 
-  drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
+  drupal_set_title(t('Top pages in the past %interval', array('%interval' => format_interval(config('statistics.settings')->get('access_log.max_lifetime')))), PASS_THROUGH);
   $build['statistics_top_pages_table'] = array(
     '#theme' => 'table',
     '#header' => $header,
@@ -161,7 +161,7 @@ function statistics_top_visitors() {
     $rows[] = array($account->hits, ($account->uid ? theme('username', array('account' => $account)) : $account->hostname), format_interval(round($account->total / 1000)), (user_access('block IP addresses') && !$account->uid) ? $ban_link : '');
   }
 
-  drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
+  drupal_set_title(t('Top visitors in the past %interval', array('%interval' => format_interval(config('statistics.settings')->get('access_log.max_lifetime')))), PASS_THROUGH);
   $build['statistics_top_visitors_table'] = array(
     '#theme' => 'table',
     '#header' => $header,
@@ -183,7 +183,7 @@ function statistics_top_visitors() {
  *   A render array containing the top referrers information.
  */
 function statistics_top_referrers() {
-  drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(variable_get('statistics_flush_accesslog_timer', 259200)))), PASS_THROUGH);
+  drupal_set_title(t('Top referrers in the past %interval', array('%interval' => format_interval(config('statistics.settings')->get('access_log.max_lifetime')))), PASS_THROUGH);
 
   $header = array(
     array('data' => t('Hits'), 'field' => 'hits', 'sort' => 'desc'),
@@ -282,9 +282,10 @@ function statistics_access_log($aid) {
  * Form constructor for the statistics administration form.
  *
  * @ingroup forms
- * @see system_settings_form()
+ * @see statistics_settings_form_submit().
  */
-function statistics_settings_form() {
+function statistics_settings_form($form, &$form_state) {
+  $config = config('statistics.settings');
   // Access log settings.
   $form['access'] = array(
     '#type' => 'fieldset',
@@ -293,13 +294,13 @@ function statistics_settings_form() {
   $form['access']['statistics_enable_access_log'] = array(
     '#type' => 'checkbox',
     '#title' => t('Enable access log'),
-    '#default_value' => variable_get('statistics_enable_access_log', 0),
+    '#default_value' => $config->get('access_log.enabled'),
     '#description' => t('Log each page access. Required for referrer statistics.'),
   );
   $form['access']['statistics_flush_accesslog_timer'] = array(
     '#type' => 'select',
     '#title' => t('Discard access logs older than'),
-    '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
+    '#default_value' => $config->get('access_log.max_lifetime'),
     '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800, 259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval'),
     '#description' => t('Older access log entries (including referrer statistics) will be automatically discarded. (Requires a correctly configured <a href="@cron">cron maintenance task</a>.)', array('@cron' => url('admin/reports/status'))),
   );
@@ -312,9 +313,20 @@ function statistics_settings_form() {
   $form['content']['statistics_count_content_views'] = array(
     '#type' => 'checkbox',
     '#title' => t('Count content views'),
-    '#default_value' => variable_get('statistics_count_content_views', 0),
+    '#default_value' => $config->get('count_content_views'),
     '#description' => t('Increment a counter each time content is viewed.'),
   );
 
-  return system_settings_form($form);
+  return system_config_form($form, $form_state);
+}
+
+/**
+ * Form submission handler for statistics_settings_form().
+ */
+function statistics_settings_form_submit($form, &$form_state) {
+  config('statistics.settings')
+    ->set('access_log.enabled', $form_state['values']['statistics_enable_access_log'])
+    ->set('access_log.max_lifetime', $form_state['values']['statistics_flush_accesslog_timer'])
+    ->set('count_content_views', $form_state['values']['statistics_count_content_views'])
+    ->save();
 }
diff --git a/core/modules/statistics/statistics.install b/core/modules/statistics/statistics.install
index 5ee20dc6b864..97c55181800d 100644
--- a/core/modules/statistics/statistics.install
+++ b/core/modules/statistics/statistics.install
@@ -2,23 +2,9 @@
 
 /**
  * @file
- * Install, update, and uninstall functions for the Statistics module.
+ * Install and update functions for the Statistics module.
  */
 
-/**
- * Implements hook_uninstall().
- */
-function statistics_uninstall() {
-  // Remove variables.
-  variable_del('statistics_count_content_views');
-  variable_del('statistics_enable_access_log');
-  variable_del('statistics_flush_accesslog_timer');
-  variable_del('statistics_day_timestamp');
-  variable_del('statistics_block_top_day_num');
-  variable_del('statistics_block_top_all_num');
-  variable_del('statistics_block_top_last_num');
-}
-
 /**
  * Implements hook_schema().
  */
@@ -134,3 +120,19 @@ function statistics_schema() {
 
   return $schema;
 }
+
+/**
+ * Moves statistics settings from variables to config.
+ *
+ * @ingroup config_upgrade
+ */
+function statistics_update_8000() {
+  update_variables_to_config('statistics.settings', array(
+    'statistics_count_content_views' => 'count_content_views',
+    'statistics_enable_access_log' => 'access_log.enabled',
+    'statistics_flush_accesslog_timer' => 'access_log.max_lifetime',
+    'statistics_block_top_day_num' => 'block.popular.top_day_limit',
+    'statistics_block_top_all_num' => 'block.popular.top_all_limit',
+    'statistics_block_top_last_num' => 'block.popular.top_recent_limit',
+  ));
+}
diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module
index c960a917a9bb..35f625a76d4b 100644
--- a/core/modules/statistics/statistics.module
+++ b/core/modules/statistics/statistics.module
@@ -54,11 +54,11 @@ function statistics_exit() {
   // When serving cached pages with the 'page_cache_without_database'
   // configuration, system variables need to be loaded. This is a major
   // performance decrease for non-database page caches, but with Statistics
-  // module, it is likely to also have 'statistics_enable_access_log' enabled,
-  // in which case we need to bootstrap to the session phase anyway.
+  // module, it is likely to also have 'statistics.settings:access_log.enabled'
+  // enabled, in which case we need to bootstrap to the session phase anyway.
   drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES);
 
-  if (variable_get('statistics_enable_access_log', 0)) {
+  if (config('statistics.settings')->get('access_log.enabled')) {
     drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
 
     // For anonymous users unicode.inc will not have been loaded.
@@ -240,10 +240,11 @@ function statistics_cron() {
     variable_set('statistics_day_timestamp', REQUEST_TIME);
   }
 
-  // Clean up expired access logs (if applicable).
-  if (variable_get('statistics_flush_accesslog_timer', 259200) > 0) {
+  // Delete access logs (if applicable).
+  $max_lifetime = config('statistics.settings')->get('access_log.max_lifetime');
+  if ($max_lifetime > 0) {
     db_delete('accesslog')
-      ->condition('timestamp', REQUEST_TIME - variable_get('statistics_flush_accesslog_timer', 259200), '<')
+      ->condition('timestamp', REQUEST_TIME - $max_lifetime, '<')
       ->execute();
   }
 }
@@ -312,7 +313,8 @@ function statistics_get($nid) {
 function statistics_block_info() {
   $blocks = array();
 
-  if (variable_get('statistics_count_content_views', 0)) {
+  $statistics_count_content_views = config('statistics.settings')->get('count_content_views');
+  if (!empty($statistics_count_content_views)) {
     $blocks['popular']['info'] = t('Popular content');
     // Too dynamic to cache.
     $blocks['popular']['cache'] = DRUPAL_NO_CACHE;
@@ -324,11 +326,12 @@ function statistics_block_info() {
  * Implements hook_block_configure().
  */
 function statistics_block_configure($delta = '') {
+  $config = config('statistics.settings');
   // Popular content block settings
   $numbers = array('0' => t('Disabled')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25, 30, 40));
-  $form['statistics_block_top_day_num'] = array('#type' => 'select', '#title' => t("Number of day's top views to display"), '#default_value' => variable_get('statistics_block_top_day_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "day" list.'));
-  $form['statistics_block_top_all_num'] = array('#type' => 'select', '#title' => t('Number of all time views to display'), '#default_value' => variable_get('statistics_block_top_all_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "all time" list.'));
-  $form['statistics_block_top_last_num'] = array('#type' => 'select', '#title' => t('Number of most recent views to display'), '#default_value' => variable_get('statistics_block_top_last_num', 0), '#options' => $numbers, '#description' => t('How many content items to display in "recently viewed" list.'));
+  $form['statistics_block_popular_top_day_limit'] = array('#type' => 'select', '#title' => t("Number of day's top views to display"), '#default_value' => $config->get('block.popular.top_day_limit'), '#options' => $numbers, '#description' => t('How many content items to display in "day" list.'));
+  $form['statistics_block_popular_top_all_limit'] = array('#type' => 'select', '#title' => t('Number of all time views to display'), '#default_value' => $config->get('block.popular.top_all_limit'), '#options' => $numbers, '#description' => t('How many content items to display in "all time" list.'));
+  $form['statistics_block_popular_top_recent_limit'] = array('#type' => 'select', '#title' => t('Number of most recent views to display'), '#default_value' => $config->get('block.popular.top_recent_limit'), '#options' => $numbers, '#description' => t('How many content items to display in "recently viewed" list.'));
   return $form;
 }
 
@@ -336,9 +339,11 @@ function statistics_block_configure($delta = '') {
  * Implements hook_block_save().
  */
 function statistics_block_save($delta = '', $edit = array()) {
-  variable_set('statistics_block_top_day_num', $edit['statistics_block_top_day_num']);
-  variable_set('statistics_block_top_all_num', $edit['statistics_block_top_all_num']);
-  variable_set('statistics_block_top_last_num', $edit['statistics_block_top_last_num']);
+  config('statistics.settings')
+    ->set('block.popular.top_day_limit', $edit['statistics_block_popular_top_day_limit'])
+    ->set('block.popular.top_all_limit', $edit['statistics_block_popular_top_all_limit'])
+    ->set('block.popular.top_recent_limit', $edit['statistics_block_popular_top_recent_limit'])
+    ->save();
 }
 
 /**
@@ -348,19 +353,20 @@ function statistics_block_view($delta = '') {
   if (user_access('access content')) {
     $content = array();
 
-    $daytop = variable_get('statistics_block_top_day_num', 0);
+    $config = config('statistics.settings');
+    $daytop = $config->get('block.popular.top_day_limit');
     if ($daytop && ($result = statistics_title_list('daycount', $daytop)) && ($node_title_list = node_title_list($result, t("Today's:")))) {
       $content['top_day'] = $node_title_list;
       $content['top_day']['#suffix'] = '<br />';
     }
 
-    $alltimetop = variable_get('statistics_block_top_all_num', 0);
+    $alltimetop = $config->get('block.popular.top_all_limit');
     if ($alltimetop && ($result = statistics_title_list('totalcount', $alltimetop)) && ($node_title_list = node_title_list($result, t('All time:')))) {
       $content['top_all'] = $node_title_list;
       $content['top_all']['#suffix'] = '<br />';
     }
 
-    $lasttop = variable_get('statistics_block_top_last_num', 0);
+    $lasttop = $config->get('block.popular.top_recent_limit');
     if ($lasttop && ($result = statistics_title_list('timestamp', $lasttop)) && ($node_title_list = node_title_list($result, t('Last viewed:')))) {
       $content['top_last'] = $node_title_list;
       $content['top_last']['#suffix'] = '<br />';
@@ -423,7 +429,7 @@ function statistics_node_predelete(Node $node) {
  * Implements hook_ranking().
  */
 function statistics_ranking() {
-  if (variable_get('statistics_count_content_views', 0)) {
+  if (config('statistics.settings')->get('count_content_views')) {
     return array(
       'views' => array(
         'title' => t('Number of views'),
diff --git a/core/modules/statistics/statistics.php b/core/modules/statistics/statistics.php
index 040dc19f8f0b..6ccc96d225a7 100644
--- a/core/modules/statistics/statistics.php
+++ b/core/modules/statistics/statistics.php
@@ -15,7 +15,8 @@
 
 include_once DRUPAL_ROOT . '/core/includes/bootstrap.inc';
 drupal_bootstrap(DRUPAL_BOOTSTRAP_VARIABLES);
-if (variable_get('statistics_count_content_views', 0)) {
+
+if (config('statistics.settings')->get('count_content_views')) {
   $nid = $_POST['nid'];
   if (is_numeric($nid)) {
     db_merge('node_counter')
-- 
GitLab