Commit 2b3cf006 authored by alexpott's avatar alexpott
Browse files

Issue #733054 by jhodgdon, mkalkbrenner, amitgoyal, ndewhurst: Fixed Watchdog...

Issue #733054 by jhodgdon, mkalkbrenner, amitgoyal, ndewhurst: Fixed Watchdog logging of all searches is performance hit; need ability to turn it off.
parent 9eba3805
......@@ -17,3 +17,4 @@ index:
strong: 3
em: 3
a: 10
logging: false
......@@ -63,6 +63,9 @@ search.settings:
a:
type: integer
label: 'Tag a weight'
logging:
type: boolean
label: 'Log searches'
search.page.*:
type: config_entity
......
......@@ -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 @@ public function __construct(SearchPageRepositoryInterface $search_page_repositor
*/
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 @@ public function view(Request $request, SearchPageInterface $entity) {
if ($request->query->has('keys')) {
if ($plugin->isSearchExecutable()) {
// Log the search.
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();
......
......@@ -223,6 +223,20 @@ public function buildForm(array $form, array &$form_state) {
'#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 @@ public function submitForm(array &$form, array &$form_state) {
$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.'));
......
......@@ -39,7 +39,7 @@ function setUp() {
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 @@ function testSearchSettingsPage() {
);
$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');
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment