search_log.module in Search Log 8
Same filename and directory in other branches
This module holds functions of Search Log Module.
File
search_log.moduleView source
<?php
/**
* @file
* This module holds functions of Search Log Module.
*/
use Drupal\Component\Utility\Unicode;
define('SEARCH_LOG_RESULT_UNKNOWN', 0);
define('SEARCH_LOG_RESULT_SUCCESS', 1);
define('SEARCH_LOG_RESULT_FAILED', -1);
define('SEARCH_LOG_TERMS_LOWERCASE', 0);
define('SEARCH_LOG_TERMS_UPPERCASE_FIRST', 1);
define('SEARCH_LOG_TERMS_UPPERCASE_WORDS', 2);
define('SEARCH_LOG_ADMIN_TERM_LENGTH', 50);
define('SEARCH_LOG_ADMIN_ROWS', 40);
define('SEARCH_LOG_DAY', 86400);
/**
* Implements hook_cron().
*
* Expire outdated entries.
*/
function search_log_cron() {
if ($days = (int) \Drupal::config('search_log.settings')
->get('search_log_cron')) {
// Get timestamp for 12:00 today UTC minus days.
$day = _search_log_get_time() - $days * 86400;
\Drupal::database()
->delete('search_log')
->condition('day', $day, '<')
->execute();
}
}
/**
* Utility time function.
*
* Effectively returns time() rounded down to nearest day.
*/
function _search_log_get_time() {
static $today;
if (!isset($today)) {
$today = mktime(0, 0, 0);
}
return $today;
}
/**
* Implements hook_form_alter().
*
* Alter standard search forms to capture submission.
*/
function search_log_form_alter(&$form, &$form_state, $form_id) {
$form_id_processed = $form_id;
// Custom Search module.
if (strpos($form_id_processed, 'custom_search_blocks_form') !== FALSE) {
$form_id_processed = 'custom_search_blocks_form';
}
switch ($form_id_processed) {
case 'search_form':
array_unshift($form['#submit'], 'search_log_search_form_submit');
break;
case 'search_block_form':
array_unshift($form['#submit'], 'search_log_search_block_form_submit');
break;
case 'search_theme_form':
array_unshift($form['#submit'], 'search_log_search_theme_form_submit');
break;
case 'custom_search_blocks_form':
array_unshift($form['#submit'], 'search_log_custom_search_blocks_form_submit');
break;
}
}
/**
* Process search form to capture keys.
*/
function search_log_search_form_submit($form, &$form_state) {
$module = isset($form_state['values']['module']) ? $form_state['values']['module'] : NULL;
$keys = isset($form_state['values']['processed_keys']) ? $form_state['values']['processed_keys'] : NULL;
if ($keys) {
_search_log_preprocess_search_form($form_state['values'], $keys, $module);
}
}
/**
* Process block search form to capture keys.
*/
function search_log_search_block_form_submit($form, &$form_state) {
$keys = isset($form_state['values']['search_block_form']) ? $form_state['values']['search_block_form'] : NULL;
if ($keys) {
_search_log_preprocess_search_form($form_state['values'], $keys);
}
}
/**
* Process theme search form to capture keys.
*/
function search_log_search_theme_form_submit($form, &$form_state) {
$keys = isset($form_state['values']['search_theme_form']) ? $form_state['values']['search_theme_form'] : NULL;
if ($keys) {
_search_log_preprocess_search_form($form_state['values'], $keys);
}
}
/**
* Process custom search form to capture keys.
*/
function search_log_custom_search_blocks_form_submit($form, &$form_state) {
$keys = NULL;
foreach ($form_state['values'] as $key => $value) {
if (strpos($key, 'custom_search_blocks_form') !== FALSE) {
$keys = $value;
break;
}
}
if ($keys) {
_search_log_preprocess_search_form($form_state['values'], $keys);
}
}
/**
* Preprocess search form results before writing to DB.
*/
function _search_log_preprocess_search_form($submitted, $keys, $module = NULL) {
$language = \Drupal::languageManager()
->getCurrentLanguage();
//@todo To be changed later on.
// if (!$module) {
// $info = search_get_default_module_info();
// $module = $info['module'];
// }
$modules = \Drupal::config('search_log.settings')
->get('search_log_modules_enabled');
if (!empty($modules) && !in_array($module, $modules, TRUE)) {
return;
}
// Custom Search content filter integration.
if (\Drupal::moduleHandler()
->moduleExists('custom_search')) {
$types = isset($submitted['custom_search_types']) ? $submitted['custom_search_types'] : array();
if (!is_array($types)) {
$types = array(
$types,
);
}
$types = array_map('_custom_search_filter_keys', array_filter($types));
if (in_array('all', $types)) {
// do nothing to keys or module.
}
elseif (in_array('user', $types)) {
$module = 'user';
}
else {
$keys .= ' type:' . implode(',', $types);
}
}
search_log($keys, $module, $language
->getName());
}
/**
* Store search keys, module, language and day.
*
* Developers can call this function directly to add additional entries to the
* log or record failed searches (e.g. Lucene integration).
*/
function search_log($keys, $module, $language = 'en', $counter = 1, $result = SEARCH_LOG_RESULT_UNKNOWN) {
$today = _search_log_get_time();
$keys = _search_log_normalize_keys($keys);
if (!$keys || !$module || !$language) {
return;
}
// If search_log_preprocess is enabled, the default is successful search.
if (!$result && \Drupal::config('search_log.settings')
->get('search_log_preprocess')) {
$result = SEARCH_LOG_RESULT_SUCCESS;
}
if ($qid = \Drupal::database()
->query("SELECT qid FROM {search_log} WHERE q = :q AND module = :module AND language = :language AND day = :day", array(
':q' => $keys,
':module' => $module,
':language' => $language,
':day' => $today,
))
->fetchField()) {
\Drupal::database()
->update('search_log')
->fields(array(
'result' => $result,
))
->expression('counter', 'counter + :counter', array(
':counter' => $counter,
))
->condition('qid', $qid)
->execute();
}
else {
\Drupal::database()
->insert('search_log')
->fields(array(
'q' => $keys,
'module' => $module,
'language' => $language,
'day' => $today,
'counter' => $counter,
'result' => $result,
))
->execute();
}
}
/**
* Internal function to normalize keys.
*/
function _search_log_normalize_keys($keys) {
$keys = preg_replace('/\\s+/', ' ', trim($keys));
switch (\Drupal::config('search_log.settings')
->get('search_log_terms')) {
case SEARCH_LOG_TERMS_LOWERCASE:
$keys = str_replace(' or ', ' OR ', mb_strtolower($keys));
break;
case SEARCH_LOG_TERMS_UPPERCASE_FIRST:
$keys = str_replace(' or ', ' OR ', Unicode::ucfirst($keys));
break;
case SEARCH_LOG_TERMS_UPPERCASE_WORDS:
$keys = str_replace(' Or ', ' OR ', ucwords(mb_strtolower($keys)));
break;
}
return $keys;
}
Functions
Name | Description |
---|---|
search_log | Store search keys, module, language and day. |
search_log_cron | Implements hook_cron(). |
search_log_custom_search_blocks_form_submit | Process custom search form to capture keys. |
search_log_form_alter | Implements hook_form_alter(). |
search_log_search_block_form_submit | Process block search form to capture keys. |
search_log_search_form_submit | Process search form to capture keys. |
search_log_search_theme_form_submit | Process theme search form to capture keys. |
_search_log_get_time | Utility time function. |
_search_log_normalize_keys | Internal function to normalize keys. |
_search_log_preprocess_search_form | Preprocess search form results before writing to DB. |