From cff48a2209854d8e2a6f4457b03f09e70f0b7321 Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 4 Oct 2018 15:17:32 +0100 Subject: [PATCH] Issue #3001216 by longwave, mondrake, voleger, tstoeckler, Berdir, andypost: Use the database.replica service where appropriate --- core/core.services.yml | 8 +++--- core/modules/comment/comment.services.yml | 2 +- .../modules/comment/src/CommentStatistics.php | 16 +++++++++--- .../src/Unit/CommentStatisticsUnitTest.php | 2 +- .../node/src/Plugin/Search/NodeSearch.php | 25 +++++++++++++------ .../src/Plugin/views/argument/Search.php | 3 +-- .../search/src/Plugin/views/filter/Search.php | 3 +-- core/modules/search/src/SearchQuery.php | 3 +-- core/modules/tracker/tracker.pages.inc | 5 ++-- 9 files changed, 42 insertions(+), 25 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 5cd8297f94..ffc3753eae 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -354,6 +354,10 @@ services: class: Drupal\Core\Database\Connection factory: Drupal\Core\Database\Database::getConnection arguments: [default] + database.replica: + class: Drupal\Core\Database\Connection + factory: Drupal\Core\Database\Database::getConnection + arguments: [replica] datetime.time: class: Drupal\Component\Datetime\Time arguments: ['@request_stack'] @@ -791,10 +795,6 @@ services: arguments: ['@language.default'] tags: - { name: service_collector, tag: string_translator, call: addTranslator } - database.replica: - class: Drupal\Core\Database\Connection - factory: Drupal\Core\Database\Database::getConnection - arguments: [replica] typed_data_manager: class: Drupal\Core\TypedData\TypedDataManager arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@class_resolver'] diff --git a/core/modules/comment/comment.services.yml b/core/modules/comment/comment.services.yml index 46398a06a4..52b27c89a6 100644 --- a/core/modules/comment/comment.services.yml +++ b/core/modules/comment/comment.services.yml @@ -11,7 +11,7 @@ services: comment.statistics: class: Drupal\comment\CommentStatistics - arguments: ['@database', '@current_user', '@entity.manager', '@state'] + arguments: ['@database', '@current_user', '@entity.manager', '@state', '@database.replica'] tags: - { name: backend_overridable } diff --git a/core/modules/comment/src/CommentStatistics.php b/core/modules/comment/src/CommentStatistics.php index b58452c594..ba4e97e30e 100644 --- a/core/modules/comment/src/CommentStatistics.php +++ b/core/modules/comment/src/CommentStatistics.php @@ -20,6 +20,13 @@ class CommentStatistics implements CommentStatisticsInterface { */ protected $database; + /** + * The replica database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $databaseReplica; + /** * The current logged in user. * @@ -52,9 +59,12 @@ class CommentStatistics implements CommentStatisticsInterface { * The entity manager service. * @param \Drupal\Core\State\StateInterface $state * The state service. + * @param \Drupal\Core\Database\Connection|null $database_replica + * (Optional) the replica database connection. */ - public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state) { + public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state, Connection $database_replica = NULL) { $this->database = $database; + $this->databaseReplica = $database_replica ?: $database; $this->currentUser = $current_user; $this->entityManager = $entity_manager; $this->state = $state; @@ -64,8 +74,8 @@ public function __construct(Connection $database, AccountInterface $current_user * {@inheritdoc} */ public function read($entities, $entity_type, $accurate = TRUE) { - $options = $accurate ? [] : ['target' => 'replica']; - $stats = $this->database->select('comment_entity_statistics', 'ces', $options) + $connection = $accurate ? $this->database : $this->databaseReplica; + $stats = $connection->select('comment_entity_statistics', 'ces') ->fields('ces') ->condition('ces.entity_id', array_keys($entities), 'IN') ->condition('ces.entity_type', $entity_type) diff --git a/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php b/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php index 2a6d379308..ace47adef4 100644 --- a/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php +++ b/core/modules/comment/tests/src/Unit/CommentStatisticsUnitTest.php @@ -82,7 +82,7 @@ protected function setUp() { ->method('select') ->will($this->returnValue($this->select)); - $this->commentStatistics = new CommentStatistics($this->database, $this->getMock('Drupal\Core\Session\AccountInterface'), $this->getMock('Drupal\Core\Entity\EntityManagerInterface'), $this->getMock('Drupal\Core\State\StateInterface')); + $this->commentStatistics = new CommentStatistics($this->database, $this->getMock('Drupal\Core\Session\AccountInterface'), $this->getMock('Drupal\Core\Entity\EntityManagerInterface'), $this->getMock('Drupal\Core\State\StateInterface'), $this->database); } /** diff --git a/core/modules/node/src/Plugin/Search/NodeSearch.php b/core/modules/node/src/Plugin/Search/NodeSearch.php index f14aac0bd4..dd3f441a1a 100644 --- a/core/modules/node/src/Plugin/Search/NodeSearch.php +++ b/core/modules/node/src/Plugin/Search/NodeSearch.php @@ -36,12 +36,19 @@ class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface { /** - * A database connection object. + * The current database connection. * * @var \Drupal\Core\Database\Connection */ protected $database; + /** + * The replica database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $databaseReplica; + /** * An entity manager object. * @@ -138,7 +145,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('language_manager'), $container->get('renderer'), $container->get('messenger'), - $container->get('current_user') + $container->get('current_user'), + $container->get('database.replica') ); } @@ -152,7 +160,7 @@ public static function create(ContainerInterface $container, array $configuratio * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\Database\Connection $database - * A database connection object. + * The current database connection. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * An entity manager object. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler @@ -167,9 +175,12 @@ public static function create(ContainerInterface $container, array $configuratio * The messenger. * @param \Drupal\Core\Session\AccountInterface $account * The $account object to use for checking for access to advanced search. + * @param \Drupal\Core\Database\Connection|null $database_replica + * (Optional) the replica database connection. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, MessengerInterface $messenger, AccountInterface $account = NULL) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, MessengerInterface $messenger, AccountInterface $account = NULL, Connection $database_replica = NULL) { $this->database = $database; + $this->databaseReplica = $database_replica ?: $database; $this->entityManager = $entity_manager; $this->moduleHandler = $module_handler; $this->searchSettings = $search_settings; @@ -237,8 +248,8 @@ protected function findResults() { $keys = $this->keywords; // Build matching conditions. - $query = $this->database - ->select('search_index', 'i', ['target' => 'replica']) + $query = $this->databaseReplica + ->select('search_index', 'i') ->extend('Drupal\search\SearchQuery') ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); $query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode'); @@ -439,7 +450,7 @@ public function updateIndex() { // per cron run. $limit = (int) $this->searchSettings->get('index.cron_limit'); - $query = Database::getConnection('replica')->select('node', 'n'); + $query = $this->databaseReplica->select('node', 'n'); $query->addField('n', 'nid'); $query->leftJoin('search_dataset', 'sd', 'sd.sid = n.nid AND sd.type = :type', [':type' => $this->getPluginId()]); $query->addExpression('CASE MAX(sd.reindex) WHEN NULL THEN 0 ELSE 1 END', 'ex'); diff --git a/core/modules/search/src/Plugin/views/argument/Search.php b/core/modules/search/src/Plugin/views/argument/Search.php index c52a69681b..e8b431d8a6 100644 --- a/core/modules/search/src/Plugin/views/argument/Search.php +++ b/core/modules/search/src/Plugin/views/argument/Search.php @@ -2,7 +2,6 @@ namespace Drupal\search\Plugin\views\argument; -use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\Condition; use Drupal\search\ViewsSearchQuery; use Drupal\views\Plugin\views\argument\ArgumentPluginBase; @@ -50,7 +49,7 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o */ protected function queryParseSearchExpression($input) { if (!isset($this->searchQuery)) { - $this->searchQuery = Database::getConnection('replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class); + $this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class); $this->searchQuery->searchExpression($input, $this->searchType); $this->searchQuery->publicParseSearchExpression(); } diff --git a/core/modules/search/src/Plugin/views/filter/Search.php b/core/modules/search/src/Plugin/views/filter/Search.php index 41548aa10c..5ac43f4cfe 100644 --- a/core/modules/search/src/Plugin/views/filter/Search.php +++ b/core/modules/search/src/Plugin/views/filter/Search.php @@ -2,7 +2,6 @@ namespace Drupal\search\Plugin\views\filter; -use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\Condition; use Drupal\Core\Form\FormStateInterface; use Drupal\search\ViewsSearchQuery; @@ -122,7 +121,7 @@ public function validateExposed(&$form, FormStateInterface $form_state) { protected function queryParseSearchExpression($input) { if (!isset($this->searchQuery)) { $this->parsed = TRUE; - $this->searchQuery = Database::getConnection('replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class); + $this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class); $this->searchQuery->searchExpression($input, $this->searchType); $this->searchQuery->publicParseSearchExpression(); } diff --git a/core/modules/search/src/SearchQuery.php b/core/modules/search/src/SearchQuery.php index ebbf84d337..9e47cf1a42 100644 --- a/core/modules/search/src/SearchQuery.php +++ b/core/modules/search/src/SearchQuery.php @@ -2,7 +2,6 @@ namespace Drupal\search; -use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\Condition; use Drupal\Core\Database\Query\SelectExtender; use Drupal\Core\Database\Query\SelectInterface; @@ -622,7 +621,7 @@ public function countQuery() { $expressions = []; // Add sid as the only field and count them as a subquery. - $count = Database::getConnection('replica')->select($inner->fields('i', ['sid']), NULL); + $count = $this->connection->select($inner->fields('i', ['sid']), NULL); // Add the COUNT() expression. $count->addExpression('COUNT(*)'); diff --git a/core/modules/tracker/tracker.pages.inc b/core/modules/tracker/tracker.pages.inc index eccd4754c7..9093776de2 100644 --- a/core/modules/tracker/tracker.pages.inc +++ b/core/modules/tracker/tracker.pages.inc @@ -6,7 +6,6 @@ */ use Drupal\Core\Cache\Cache; -use Drupal\Core\Database\Database; use Drupal\node\Entity\Node; /** @@ -23,13 +22,13 @@ */ function tracker_page($account = NULL) { if ($account) { - $query = Database::getConnection()->select('tracker_user', 't') + $query = \Drupal::database()->select('tracker_user', 't') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') ->addMetaData('base_table', 'tracker_user') ->condition('t.uid', $account->id()); } else { - $query = Database::getConnection('replica')->select('tracker_node', 't') + $query = \Drupal::service('database.replica')->select('tracker_node', 't') ->extend('Drupal\Core\Database\Query\PagerSelectExtender') ->addMetaData('base_table', 'tracker_node'); } -- GitLab