Security update for Core, with self-updated composer
[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 use Drupal\views\ViewEntityInterface;
16 use Drupal\views\ViewExecutable;
17
18 /**
19  * Implements hook_help().
20  */
21 function dblog_help($route_name, RouteMatchInterface $route_match) {
22   switch ($route_name) {
23     case 'help.page.dblog':
24       $output = '';
25       $output .= '<h3>' . t('About') . '</h3>';
26       $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>';
27       $output .= '<h3>' . t('Uses') . '</h3>';
28       $output .= '<dl>';
29       $output .= '<dt>' . t('Monitoring your site') . '</dt>';
30       $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>';
31       $output .= '<dt>' . t('Debugging site problems') . '</dt>';
32       $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>';
33       $output .= '</dl>';
34       return $output;
35
36     case 'dblog.overview':
37       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>';
38   }
39 }
40
41 /**
42  * Implements hook_menu_links_discovered_alter().
43  */
44 function dblog_menu_links_discovered_alter(&$links) {
45   if (\Drupal::moduleHandler()->moduleExists('search')) {
46     $links['dblog.search'] = [
47       'title' => new TranslatableMarkup('Top search phrases'),
48       'route_name' => 'dblog.search',
49       'description' => new TranslatableMarkup('View most popular search phrases.'),
50       'parent' => 'system.admin_reports',
51     ];
52   }
53
54   return $links;
55 }
56
57 /**
58  * Implements hook_cron().
59  *
60  * Controls the size of the log table, paring it to 'dblog_row_limit' messages.
61  */
62 function dblog_cron() {
63   // Cleanup the watchdog table.
64   $row_limit = \Drupal::config('dblog.settings')->get('row_limit');
65
66   // For row limit n, get the wid of the nth row in descending wid order.
67   // Counting the most recent n rows avoids issues with wid number sequences,
68   // e.g. auto_increment value > 1 or rows deleted directly from the table.
69   if ($row_limit > 0) {
70     $min_row = db_select('watchdog', 'w')
71       ->fields('w', ['wid'])
72       ->orderBy('wid', 'DESC')
73       ->range($row_limit - 1, 1)
74       ->execute()->fetchField();
75
76     // Delete all table entries older than the nth row, if nth row was found.
77     if ($min_row) {
78       db_delete('watchdog')
79         ->condition('wid', $min_row, '<')
80         ->execute();
81     }
82   }
83 }
84
85 /**
86  * Gathers a list of uniquely defined database log message types.
87  *
88  * @return array
89  *   List of uniquely defined database log message types.
90  */
91 function _dblog_get_message_types() {
92   return db_query('SELECT DISTINCT(type) FROM {watchdog} ORDER BY type')
93     ->fetchAllKeyed(0, 0);
94 }
95
96 /**
97  * Implements hook_form_FORM_ID_alter() for system_logging_settings().
98  */
99 function dblog_form_system_logging_settings_alter(&$form, FormStateInterface $form_state) {
100   $row_limits = [100, 1000, 10000, 100000, 1000000];
101   $form['dblog_row_limit'] = [
102     '#type' => 'select',
103     '#title' => t('Database log messages to keep'),
104     '#default_value' => \Drupal::configFactory()->getEditable('dblog.settings')->get('row_limit'),
105     '#options' => [0 => t('All')] + array_combine($row_limits, $row_limits),
106     '#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')])
107   ];
108
109   $form['#submit'][] = 'dblog_logging_settings_submit';
110 }
111
112 /**
113  * Form submission handler for system_logging_settings().
114  *
115  * @see dblog_form_system_logging_settings_alter()
116  */
117 function dblog_logging_settings_submit($form, FormStateInterface $form_state) {
118   \Drupal::configFactory()->getEditable('dblog.settings')->set('row_limit', $form_state->getValue('dblog_row_limit'))->save();
119 }
120
121 /**
122  * Implements hook_ENTITY_TYPE_presave() for views entities.
123  *
124  * This hook ensures there are no views based that are using a wrong plugin for
125  * wid and uid fields on views that use watchdog as base table.
126  *
127  * @deprecated in Drupal 8.4.x and will be removed before 9.0.0.
128  *
129  * @see https://www.drupal.org/node/2876378
130  */
131 function dblog_view_presave(ViewEntityInterface $view) {
132   // Only interested in watchdog based views.
133   if ($view->get('base_table') != 'watchdog') {
134     return;
135   }
136
137   $displays = $view->get('display');
138   $update = FALSE;
139   foreach ($displays as $display_name => $display) {
140
141     // Iterate through all the fields of watchdog views based tables.
142     if (isset($display['display_options']['fields'])) {
143       foreach ($display['display_options']['fields'] as $field_name => $field) {
144         // We are only interested in wid and uid fields from the watchdog table
145         // that still use the numeric id.
146         if (isset($field['table']) &&
147             $field['table'] === 'watchdog' &&
148             $field['plugin_id'] == 'numeric' &&
149             in_array($field['field'], ['wid', 'uid'], TRUE)) {
150
151           $displays[$display_name]['display_options']['fields'][$field_name]['plugin_id'] = 'standard';
152
153           // Delete all the attributes related to numeric fields.
154           unset(
155             $displays[$display_name]['display_options']['fields'][$field_name]['set_precision'],
156             $displays[$display_name]['display_options']['fields'][$field_name]['precision'],
157             $displays[$display_name]['display_options']['fields'][$field_name]['decimal'],
158             $displays[$display_name]['display_options']['fields'][$field_name]['separator'],
159             $displays[$display_name]['display_options']['fields'][$field_name]['format_plural'],
160             $displays[$display_name]['display_options']['fields'][$field_name]['format_plural_string'],
161             $displays[$display_name]['display_options']['fields'][$field_name]['prefix'],
162             $displays[$display_name]['display_options']['fields'][$field_name]['suffix']
163           );
164
165           $update = TRUE;
166           @trigger_error("The numeric plugin for watchdog.$field_name field is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use standard plugin instead. See https://www.drupal.org/node/2876378.", E_USER_DEPRECATED);
167         }
168       }
169     }
170
171     // Iterate all filters looking for type filters to update.
172     if (isset($display['display_options']['filters'])) {
173       foreach ($display['display_options']['filters'] as $filter_name => $filter) {
174         if (isset($filter['table']) &&
175             $filter['table'] === 'watchdog' &&
176             $filter['plugin_id'] == 'in_operator' &&
177             $filter['field'] == 'type') {
178
179           $displays[$display_name]['display_options']['filters'][$filter_name]['plugin_id'] = 'dblog_types';
180           $update = TRUE;
181           @trigger_error("The in_operator plugin for watchdog.type filter is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Must use dblog_types plugin instead. See https://www.drupal.org/node/2876378.", E_USER_DEPRECATED);
182         }
183       }
184     }
185   }
186
187   if ($update) {
188     $view->set('display', $displays);
189   }
190 }
191
192 /**
193  * Implements hook_views_pre_render().
194  */
195 function dblog_views_pre_render(ViewExecutable $view) {
196   if (isset($view) && ($view->storage->get('base_table') == 'watchdog')) {
197     $view->element['#attached']['library'][] = 'dblog/drupal.dblog';
198   }
199 }