From 2b3cf006d76486e55daa3cdf8ab5830b7539f0e8 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Fri, 18 Jul 2014 19:47:24 +0100 Subject: [PATCH] Issue #733054 by jhodgdon, mkalkbrenner, amitgoyal, ndewhurst: Fixed Watchdog logging of all searches is performance hit; need ability to turn it off. --- .../search/config/install/search.settings.yml | 1 + .../search/config/schema/search.schema.yml | 3 +++ .../src/Controller/SearchController.php | 20 ++++++++++++++++--- .../search/src/SearchPageListBuilder.php | 15 ++++++++++++++ .../Tests/SearchConfigSettingsFormTest.php | 17 +++++++++++++++- 5 files changed, 52 insertions(+), 4 deletions(-) diff --git a/core/modules/search/config/install/search.settings.yml b/core/modules/search/config/install/search.settings.yml index 09c09cb06a6..10a4f01e535 100644 --- a/core/modules/search/config/install/search.settings.yml +++ b/core/modules/search/config/install/search.settings.yml @@ -17,3 +17,4 @@ index: strong: 3 em: 3 a: 10 +logging: false diff --git a/core/modules/search/config/schema/search.schema.yml b/core/modules/search/config/schema/search.schema.yml index 3be192c44b4..450c60c2d86 100644 --- a/core/modules/search/config/schema/search.schema.yml +++ b/core/modules/search/config/schema/search.schema.yml @@ -63,6 +63,9 @@ search.settings: a: type: integer label: 'Tag a weight' + logging: + type: boolean + label: 'Log searches' search.page.*: type: config_entity diff --git a/core/modules/search/src/Controller/SearchController.php b/core/modules/search/src/Controller/SearchController.php index 20ce39ab07c..7865c828574 100644 --- a/core/modules/search/src/Controller/SearchController.php +++ b/core/modules/search/src/Controller/SearchController.php @@ -7,6 +7,7 @@ namespace Drupal\search\Controller; +use Drupal\Core\Config\ConfigFactory; use Drupal\Core\Controller\ControllerBase; use Drupal\search\SearchPageInterface; use Drupal\search\SearchPageRepositoryInterface; @@ -25,14 +26,24 @@ class SearchController extends ControllerBase { */ protected $searchPageRepository; + /** + * The configuration factory. + * + * @var \Drupal\Core\Config\ConfigFactory + */ + protected $configFactory; + /** * Constructs a new search controller. * * @param \Drupal\search\SearchPageRepositoryInterface $search_page_repository * The search page repository. + * @param \Drupal\Core\Config\ConfigFactory $factory + * The configuration factory object. */ - public function __construct(SearchPageRepositoryInterface $search_page_repository) { + public function __construct(SearchPageRepositoryInterface $search_page_repository, ConfigFactory $factory) { $this->searchPageRepository = $search_page_repository; + $this->configFactory = $factory; } /** @@ -40,7 +51,8 @@ class SearchController extends ControllerBase { */ public static function create(ContainerInterface $container) { return new static( - $container->get('search.search_page_repository') + $container->get('search.search_page_repository'), + $container->get('config.factory') ); } @@ -75,7 +87,9 @@ class SearchController extends ControllerBase { if ($request->query->has('keys')) { if ($plugin->isSearchExecutable()) { // Log the search. - watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $entity->label()), WATCHDOG_NOTICE); + if ($this->configFactory->get('search.settings')->get('logging')) { + watchdog('search', 'Searched %type for %keys.', array('%keys' => $keys, '%type' => $entity->label()), WATCHDOG_NOTICE); + } // Collect the search results. $results = $plugin->buildResults(); diff --git a/core/modules/search/src/SearchPageListBuilder.php b/core/modules/search/src/SearchPageListBuilder.php index 701a6051872..044ee648b00 100644 --- a/core/modules/search/src/SearchPageListBuilder.php +++ b/core/modules/search/src/SearchPageListBuilder.php @@ -223,6 +223,20 @@ class SearchPageListBuilder extends DraggableListBuilder implements FormInterfac '#description' => $this->t('Whether to apply a simple Chinese/Japanese/Korean tokenizer based on overlapping sequences. Turn this off if you want to use an external preprocessor for this instead. Does not affect other languages.') ); + // Indexing settings: + $form['logging'] = array( + '#type' => 'details', + '#title' => $this->t('Logging'), + '#open' => TRUE, + ); + + $form['logging']['logging'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Log searches'), + '#default_value' => $search_settings->get('logging'), + '#description' => $this->t('If checked, all searches will be logged. Uncheck to skip logging. Logging may affect performance.'), + ); + $form['search_pages'] = array( '#type' => 'details', '#title' => $this->t('Search pages'), @@ -320,6 +334,7 @@ class SearchPageListBuilder extends DraggableListBuilder implements FormInterfac $search_settings ->set('index.cron_limit', $form_state['values']['cron_limit']) + ->set('logging', $form_state['values']['logging']) ->save(); drupal_set_message($this->t('The configuration options have been saved.')); diff --git a/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php b/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php index 6e57d955f24..84ef12ab55a 100644 --- a/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php +++ b/core/modules/search/src/Tests/SearchConfigSettingsFormTest.php @@ -39,7 +39,7 @@ class SearchConfigSettingsFormTest extends SearchTestBase { parent::setUp(); // Login as a user that can create and search content. - $this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks')); + $this->search_user = $this->drupalCreateUser(array('search content', 'administer search', 'administer nodes', 'bypass node access', 'access user profiles', 'administer users', 'administer blocks', 'access site reports')); $this->drupalLogin($this->search_user); // Add a single piece of content and index it. @@ -85,6 +85,21 @@ class SearchConfigSettingsFormTest extends SearchTestBase { ); $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration')); $this->assertNoText(t('The configuration options have been saved.'), 'Form does not save with an invalid word length.'); + + // Test logging setting. It should be off by default. + $text = $this->randomName(5); + $this->drupalPostForm('search/node', array('keys' => $text), t('Search')); + $this->drupalGet('admin/reports/dblog'); + $this->assertNoLink('Searched Content for ' . $text . '.', 'Search was not logged'); + + // Turn on logging. + $edit = array('logging' => TRUE); + $this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration')); + $text = $this->randomName(5); + $this->drupalPostForm('search/node', array('keys' => $text), t('Search')); + $this->drupalGet('admin/reports/dblog'); + $this->assertLink('Searched Content for ' . $text . '.', 0, 'Search was logged'); + } /**