Thu Dec 20 14:07:43 2018 +0600 55d273d0d4 (HEAD -> 8.6.x) Make HISTORY_READ_LIMIT configurable  [Yuriy Shupen'ko]
diff --git a/core/modules/comment/src/CommentManager.php b/core/modules/comment/src/CommentManager.php
index 80cc427e34..5429d9ebfe 100644
--- a/core/modules/comment/src/CommentManager.php
+++ b/core/modules/comment/src/CommentManager.php
@@ -59,6 +59,13 @@ class CommentManager implements CommentManagerInterface {
    */
   protected $currentUser;
 
+  /**
+   * History read limit time.
+   *
+   * @var int
+   */
+  protected $historyReadLimit;
+
   /**
    * Construct the CommentManager object.
    *
@@ -82,6 +89,7 @@ public function __construct(EntityManagerInterface $entity_manager, ConfigFactor
     $this->urlGenerator = $url_generator;
     $this->moduleHandler = $module_handler;
     $this->currentUser = $current_user;
+    $this->historyReadLimit = REQUEST_TIME - $config_factory->get('history.settings')->get('history_mark_read');
   }
 
   /**
@@ -198,7 +206,7 @@ public function getCountNewComments(EntityInterface $entity, $field_name = NULL,
           }
         }
       }
-      $timestamp = ($timestamp > HISTORY_READ_LIMIT ? $timestamp : HISTORY_READ_LIMIT);
+      $timestamp = ($timestamp > $this->historyReadLimit ? $timestamp : $this->historyReadLimit);
 
       // Use the timestamp to retrieve the number of new comments.
       $query = $this->entityManager->getStorage('comment')->getQuery()
diff --git a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
index bfb0b5cbd8..1d1d15c138 100644
--- a/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
+++ b/core/modules/comment/src/Plugin/views/field/NodeNewComments.php
@@ -2,6 +2,7 @@
 
 namespace Drupal\comment\Plugin\views\field;
 
+use Drupal\Core\Config\ConfigFactory;
 use Drupal\Core\Database\Connection;
 use Drupal\comment\CommentInterface;
 use Drupal\Core\Form\FormStateInterface;
@@ -35,6 +36,13 @@ public function usesGroupBy() {
    */
   protected $database;
 
+  /**
+   * History read limit time.
+   *
+   * @var int
+   */
+  protected $historyReadLimit;
+
   /**
    * Constructs a \Drupal\comment\Plugin\views\field\NodeNewComments object.
    *
@@ -46,18 +54,21 @@ public function usesGroupBy() {
    *   The plugin implementation definition.
    * @param \Drupal\Core\Database\Connection $database
    *   Database Service Object.
+   * @param \Drupal\Core\Config\ConfigFactory $configFactory
+   *   Config factory service.
    */
-  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database) {
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, ConfigFactory $configFactory) {
     parent::__construct($configuration, $plugin_id, $plugin_definition);
 
     $this->database = $database;
+    $this->historyReadLimit = REQUEST_TIME - $configFactory->get('history.settings')->get('history_mark_read');
   }
 
   /**
    * {@inheritdoc}
    */
   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
-    return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'));
+    return new static($configuration, $plugin_id, $plugin_definition, $container->get('database'), $container->get('config.factory'));
   }
 
   /**
@@ -133,8 +144,8 @@ public function preRender(&$values) {
         ':status' => CommentInterface::PUBLISHED,
         ':h_uid' => $user->id(),
         ':nids[]' => $nids,
-        ':timestamp1' => HISTORY_READ_LIMIT,
-        ':timestamp2' => HISTORY_READ_LIMIT,
+        ':timestamp1' => $this->historyReadLimit,
+        ':timestamp2' => $this->historyReadLimit,
       ]);
       foreach ($result as $node) {
         foreach ($ids[$node->nid] as $id) {
diff --git a/core/modules/forum/src/ForumManager.php b/core/modules/forum/src/ForumManager.php
index 1ecd3b68fa..f96c881ce9 100644
--- a/core/modules/forum/src/ForumManager.php
+++ b/core/modules/forum/src/ForumManager.php
@@ -304,7 +304,7 @@ protected function getTopicOrder($sortby) {
    *
    * @return int
    *   The timestamp when the user last viewed this node, if the user has
-   *   previously viewed the node; otherwise HISTORY_READ_LIMIT.
+   *   previously viewed the node; otherwise - history read limit.
    */
   protected function lastVisit($nid, AccountInterface $account) {
     if (empty($this->history[$nid])) {
@@ -313,10 +313,10 @@ protected function lastVisit($nid, AccountInterface $account) {
         ->condition('uid', $account->id())
         ->execute();
       foreach ($result as $t) {
-        $this->history[$t->nid] = $t->timestamp > HISTORY_READ_LIMIT ? $t->timestamp : HISTORY_READ_LIMIT;
+        $this->history[$t->nid] = $t->timestamp > $this->history_read_limit ? $t->timestamp : $this->history_read_limit;
       }
     }
-    return isset($this->history[$nid]) ? $this->history[$nid] : HISTORY_READ_LIMIT;
+    return isset($this->history[$nid]) ? $this->history[$nid] : $this->history_read_limit;
   }
 
   /**
@@ -483,7 +483,7 @@ public function unreadTopics($term, $uid) {
       // @todo This should be actually filtering on the desired node status
       //   field language and just fall back to the default language.
       ->condition('n.default_langcode', 1)
-      ->condition('n.created', HISTORY_READ_LIMIT, '>')
+      ->condition('n.created', $history_read_limit, '>')
       ->isNull('h.nid')
       ->addTag('node_access')
       ->execute()
diff --git a/core/modules/forum/src/ForumManagerInterface.php b/core/modules/forum/src/ForumManagerInterface.php
index 9e8833ebcf..4f81ae0997 100644
--- a/core/modules/forum/src/ForumManagerInterface.php
+++ b/core/modules/forum/src/ForumManagerInterface.php
@@ -79,7 +79,7 @@ public function checkNodeType(NodeInterface $node);
   /**
    * Calculates the number of new posts in a forum that the user has not yet read.
    *
-   * Nodes are new if they are newer than HISTORY_READ_LIMIT.
+   * Nodes are new if they are newer than history read limit.
    *
    * @param int $term
    *   The term ID of the forum.
diff --git a/core/modules/history/config/install/history.settings.yml b/core/modules/history/config/install/history.settings.yml
new file mode 100644
index 0000000000..7e3ebe68b0
--- /dev/null
+++ b/core/modules/history/config/install/history.settings.yml
@@ -0,0 +1,5 @@
+# Entities changed before this time are always shown as read.
+#
+# Entities changed within this time may be marked as new, updated, or read,
+# depending on their state for the current user. Defaults to 30 days ago.
+history_mark_read: '2592000'
diff --git a/core/modules/history/config/schema/history.settings.schema.yml b/core/modules/history/config/schema/history.settings.schema.yml
new file mode 100644
index 0000000000..530dddd111
--- /dev/null
+++ b/core/modules/history/config/schema/history.settings.schema.yml
@@ -0,0 +1,7 @@
+history.settings:
+  type: config_object
+  label: 'History settings'
+  mapping:
+    history_mark_read:
+      type: integer
+      label: 'Entities changed within this time period may be marked as new.'
diff --git a/core/modules/history/history.module b/core/modules/history/history.module
index db563d10e8..d5330c070c 100644
--- a/core/modules/history/history.module
+++ b/core/modules/history/history.module
@@ -14,14 +14,6 @@
 use Drupal\Core\Routing\RouteMatchInterface;
 use Drupal\user\UserInterface;
 
-/**
- * Entities changed before this time are always shown as read.
- *
- * Entities changed within this time may be marked as new, updated, or read,
- * depending on their state for the current user. Defaults to 30 days ago.
- */
-define('HISTORY_READ_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);
-
 /**
  * Implements hook_help().
  */
@@ -125,8 +117,9 @@ function history_write($nid, $account = NULL) {
  * Implements hook_cron().
  */
 function history_cron() {
+  $history_read_limit = REQUEST_TIME - \Drupal::config('history.settings')->get('history_mark_read');
   db_delete('history')
-    ->condition('timestamp', HISTORY_READ_LIMIT, '<')
+    ->condition('timestamp', $history_read_limit, '<')
     ->execute();
 }
 
diff --git a/core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php b/core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php
index 617cd23e12..6e9c5ef3c8 100644
--- a/core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php
+++ b/core/modules/history/src/Plugin/views/field/HistoryUserTimestamp.php
@@ -3,10 +3,12 @@
 namespace Drupal\history\Plugin\views\field;
 
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\views\ResultRow;
 use Drupal\views\ViewExecutable;
 use Drupal\views\Plugin\views\display\DisplayPluginBase;
 use Drupal\node\Plugin\views\field\Node;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Field handler to display the marker for new content.
@@ -18,7 +20,43 @@
  *
  * @ViewsField("history_user_timestamp")
  */
-class HistoryUserTimestamp extends Node {
+class HistoryUserTimestamp extends Node implements ContainerFactoryPluginInterface {
+
+  /**
+   * History read limit
+   *
+   * @var int
+   */
+  protected $historyReadLimit;
+
+  /**
+   * {@inheritdoc}
+   */
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      REQUEST_TIME - $container->get('config.factory')->get('history.settings')->get('history_mark_read')
+    );
+  }
+
+  /**
+   * HistoryUserTimestamp constructor.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin ID for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param integer $history_read_limit
+   *   History read limit.
+   */
+  public function __construct($configuration, $plugin_id, $plugin_definition, $history_read_limit) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->historyReadLimit = $history_read_limit;
+  }
 
   /**
    * {@inheritdoc}
@@ -92,13 +130,13 @@ public function render(ResultRow $values) {
 
       $last_comment = \Drupal::moduleHandler()->moduleExists('comment') && !empty($this->options['comments']) ? $this->getValue($values, 'last_comment') : 0;
 
-      if (!$last_read && $changed > HISTORY_READ_LIMIT) {
+      if (!$last_read && $changed > $this->historyReadLimit) {
         $mark = MARK_NEW;
       }
-      elseif ($changed > $last_read && $changed > HISTORY_READ_LIMIT) {
+      elseif ($changed > $last_read && $changed > $this->historyReadLimit) {
         $mark = MARK_UPDATED;
       }
-      elseif ($last_comment > $last_read && $last_comment > HISTORY_READ_LIMIT) {
+      elseif ($last_comment > $last_read && $last_comment > $this->historyReadLimit) {
         $mark = MARK_UPDATED;
       }
       $build = [
diff --git a/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php b/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php
index 02288da1c4..3cdf566925 100644
--- a/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php
+++ b/core/modules/history/src/Plugin/views/filter/HistoryUserTimestamp.php
@@ -4,7 +4,9 @@
 
 use Drupal\Core\Cache\UncacheableDependencyTrait;
 use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
 use Drupal\views\Plugin\views\filter\FilterPluginBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Filter for new content.
@@ -16,10 +18,44 @@
  *
  * @ViewsFilter("history_user_timestamp")
  */
-class HistoryUserTimestamp extends FilterPluginBase {
+class HistoryUserTimestamp extends FilterPluginBase implements ContainerFactoryPluginInterface {
 
   use UncacheableDependencyTrait;
 
+  /**
+   * History read limit
+   *
+   * @var int
+   */
+  protected $historyReadLimit;
+
+  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+    return new static(
+      $configuration,
+      $plugin_id,
+      $plugin_definition,
+      REQUEST_TIME - $container->get('config.factory')->get('history.settings')->config->get('history_mark_read')
+
+    );
+  }
+
+  /**
+   * HistoryUserTimestamp constructor.
+   *
+   * @param array $configuration
+   *   A configuration array containing information about the plugin instance.
+   * @param string $plugin_id
+   *   The plugin ID for the plugin instance.
+   * @param mixed $plugin_definition
+   *   The plugin implementation definition.
+   * @param integer $history_read_limit
+   *   History read limit.
+   */
+  public function __construct($configuration, $plugin_id, $plugin_definition, $history_read_limit) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->historyReadLimit = $history_read_limit;
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -80,8 +116,8 @@ public function query() {
     }
 
     // Hey, Drupal kills old history, so nodes that haven't been updated
-    // since HISTORY_READ_LIMIT are bzzzzzzzt outta here!
-    $limit = REQUEST_TIME - HISTORY_READ_LIMIT;
+    // since history_cron() are bzzzzzzzt outta here!
+    $limit = $this->historyReadLimit;
 
     $this->ensureMyTable();
     $field = "$this->tableAlias.$this->realField";
diff --git a/core/modules/node/node.module b/core/modules/node/node.module
index b2df3783ad..fd370ab233 100644
--- a/core/modules/node/node.module
+++ b/core/modules/node/node.module
@@ -246,6 +246,7 @@ function node_title_list(StatementInterface $result, $title = NULL) {
 function node_mark($nid, $timestamp) {
 
   $cache = &drupal_static(__FUNCTION__, []);
+  $history_read_limit = REQUEST_TIME - \Drupal::config('history.settings')->get('history_mark_read');
 
   if (\Drupal::currentUser()->isAnonymous() || !\Drupal::moduleHandler()->moduleExists('history')) {
     return MARK_READ;
@@ -253,10 +254,10 @@ function node_mark($nid, $timestamp) {
   if (!isset($cache[$nid])) {
     $cache[$nid] = history_read($nid);
   }
-  if ($cache[$nid] == 0 && $timestamp > HISTORY_READ_LIMIT) {
+  if ($cache[$nid] == 0 && $timestamp > $history_read_limit) {
     return MARK_NEW;
   }
-  elseif ($timestamp > $cache[$nid] && $timestamp > HISTORY_READ_LIMIT) {
+  elseif ($timestamp > $cache[$nid] && $timestamp > $history_read_limit) {
     return MARK_UPDATED;
   }
   return MARK_READ;
