Version 1
[yaffs-website] / web / core / modules / dblog / dblog.module
1 <?php
2
3 /**
4  * @file
5  * System monitoring and logging for administrators.
6  *
7  * The Database Logging module monitors your site and keeps a list of recorded
8  * events containing usage and performance data, errors, warnings, and similar
9  * operational information.
10  */
11
12 use Drupal\Core\Form\FormStateInterface;
13 use Drupal\Core\Routing\RouteMatchInterface;
14 use Drupal\Core\StringTranslation\TranslatableMarkup;
15
16 /**
17  * Implements hook_help().
18  */
19 function dblog_help($route_name, RouteMatchInterface $route_match) {
20   switch ($route_name) {
21     case 'help.page.dblog':
22       $output = '';
23       $output .= '<h3>' . t('About') . '</h3>';
24       $output .= '<p>' . t('The Database Logging module logs system events in the Drupal database. For more information, see the <a href=":dblog">online documentation for the Database Logging module</a>.', [':dblog' => 'https://www.drupal.org/documentation/modules/dblog']) . '</p>';
25       $output .= '<h3>' . t('Uses') . '</h3>';
26       $output .= '<dl>';
27       $output .= '<dt>' . t('Monitoring your site') . '</dt>';
28       $output .= '<dd>' . t('The Database Logging module allows you to view an event log on the <a href=":dblog">Recent log messages</a> page. The log is a chronological list of recorded events containing usage data, performance data, errors, warnings and operational information. Administrators should check the log on a regular basis to ensure their site is working properly.', [':dblog' => \Drupal::url('dblog.overview')]) . '</dd>';
29       $output .= '<dt>' . t('Debugging site problems') . '</dt>';
30       $output .= '<dd>' . t('In case of errors or problems with the site, the <a href=":dblog">Recent log messages</a> page can be useful for debugging, since it shows the sequence of events. The log messages include usage information, warnings, and errors.', [':dblog' => \Drupal::url('dblog.overview')]) . '</dd>';
31       $output .= '</dl>';
32       return $output;
33
34     case 'dblog.overview':
35       return '<p>' . t('The Database Logging module logs system events in the Drupal database. Monitor your site or debug site problems on this page.') . '</p>';
36   }
37 }
38
39 /**
40  * Implements hook_menu_links_discovered_alter().
41  */
42 function dblog_menu_links_discovered_alter(&$links) {
43   if (\Drupal::moduleHandler()->moduleExists('search')) {
44     $links['dblog.search'] = [
45       'title' => new TranslatableMarkup('Top search phrases'),
46       'route_name' => 'dblog.search',
47       'description' => new TranslatableMarkup('View most popular search phrases.'),
48       'parent' => 'system.admin_reports',
49     ];
50   }
51
52   return $links;
53 }
54
55 /**
56  * Implements hook_cron().
57  *
58  * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
59  */
60 function dblog_cron() {
61   // Cleanup the watchdog table.
62   $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
63
64   // For row limit n, get the wid of the nth row in descending wid order.
65   // Counting the most recent n rows avoids issues with wid number sequences,
66   // e.g. auto_increment value > 1 or rows deleted directly from the table.
67   if ($row_limit > 0) {
68     $min_row = db_select('watchdog', 'w')
69       ->fields('w', ['wid'])
70       ->orderBy('wid', 'DESC')
71       ->range($row_limit - 1, 1)
72       ->execute()->fetchField();
73
74     // Delete all table entries older than the nth row, if nth row was found.
75     if ($min_row) {
76       db_delete('watchdog')
77         ->condition('wid', $min_row, '<')
78         ->execute();
79     }
80   }
81 }
82
83 /**
84  * Gathers a list of uniquely defined database log message types.
85  *
86  * @return array
87  *   List of uniquely defined database log message types.
88  */
89 function _dblog_get_message_types() {
90   return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
91     ->fetchAllKeyed(0, 0);
92 }
93
94 /**
95  * Implements hook_form_FORM_ID_alter() for system_logging_settings().
96  */
97 function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
98   $row_limits = [100, 1000, 10000, 100000, 1000000];
99   $form['dblog_row_limit'] = [
100     '#type' => 'select',
101     '#title' => t('Database log messages to keep'),
102     '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'),
103     '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
104     '#description' => t('The maximum number of messages to keep in the database log. Requires a <a href=":cron">cron maintenance task</a>.', [':cron' => \Drupal::url('system.status')])
105   ];
106
107   $form['#submit'][] = 'dblog_logging_settings_submit';
108 }
109
110 /**
111  * Form submission handler for system_logging_settings().
112  *
113  * @see dblog_form_system_logging_settings_alter()
114  */
115 function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
116   \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();
117 }