dblog.install 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the dblog module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function dblog_schema() {
  10. $schema['watchdog'] = [
  11. 'description' => 'Table that contains logs of all system events.',
  12. 'fields' => [
  13. 'wid' => [
  14. 'type' => 'serial',
  15. 'not null' => TRUE,
  16. 'description' => 'Primary Key: Unique watchdog event ID.',
  17. ],
  18. 'uid' => [
  19. 'type' => 'int',
  20. 'unsigned' => TRUE,
  21. 'not null' => TRUE,
  22. 'default' => 0,
  23. 'description' => 'The {users}.uid of the user who triggered the event.',
  24. ],
  25. 'type' => [
  26. 'type' => 'varchar_ascii',
  27. 'length' => 64,
  28. 'not null' => TRUE,
  29. 'default' => '',
  30. 'description' => 'Type of log message, for example "user" or "page not found."',
  31. ],
  32. 'message' => [
  33. 'type' => 'text',
  34. 'not null' => TRUE,
  35. 'size' => 'big',
  36. 'description' => 'Text of log message to be passed into the t() function.',
  37. ],
  38. 'variables' => [
  39. 'type' => 'blob',
  40. 'not null' => TRUE,
  41. 'size' => 'big',
  42. 'description' => 'Serialized array of variables that match the message string and that is passed into the t() function.',
  43. ],
  44. 'severity' => [
  45. 'type' => 'int',
  46. 'unsigned' => TRUE,
  47. 'not null' => TRUE,
  48. 'default' => 0,
  49. 'size' => 'tiny',
  50. 'description' => 'The severity level of the event; ranges from 0 (Emergency) to 7 (Debug)',
  51. ],
  52. 'link' => [
  53. 'type' => 'text',
  54. 'not null' => FALSE,
  55. 'description' => 'Link to view the result of the event.',
  56. ],
  57. 'location' => [
  58. 'type' => 'text',
  59. 'not null' => TRUE,
  60. 'description' => 'URL of the origin of the event.',
  61. ],
  62. 'referer' => [
  63. 'type' => 'text',
  64. 'not null' => FALSE,
  65. 'description' => 'URL of referring page.',
  66. ],
  67. 'hostname' => [
  68. 'type' => 'varchar_ascii',
  69. 'length' => 128,
  70. 'not null' => TRUE,
  71. 'default' => '',
  72. 'description' => 'Hostname of the user who triggered the event.',
  73. ],
  74. 'timestamp' => [
  75. 'type' => 'int',
  76. 'not null' => TRUE,
  77. 'default' => 0,
  78. 'description' => 'Unix timestamp of when event occurred.',
  79. ],
  80. ],
  81. 'primary key' => ['wid'],
  82. 'indexes' => [
  83. 'type' => ['type'],
  84. 'uid' => ['uid'],
  85. 'severity' => ['severity'],
  86. ],
  87. ];
  88. return $schema;
  89. }
  90. /**
  91. * Use standard plugin for wid and uid fields. Use dblog_types for type filter.
  92. */
  93. function dblog_update_8400() {
  94. $config_factory = \Drupal::configFactory();
  95. foreach ($config_factory->listAll('views.view.') as $view_config_name) {
  96. $view = $config_factory->getEditable($view_config_name);
  97. if ($view->get('base_table') != 'watchdog') {
  98. continue;
  99. }
  100. $save = FALSE;
  101. foreach ($view->get('display') as $display_name => $display) {
  102. // Iterate through all the fields of watchdog views based tables.
  103. if (isset($display['display_options']['fields'])) {
  104. foreach ($display['display_options']['fields'] as $field_name => $field) {
  105. // We are only interested in wid and uid fields from the watchdog
  106. // table that still use the numeric id.
  107. if (isset($field['table']) &&
  108. $field['table'] === 'watchdog' &&
  109. $field['plugin_id'] == 'numeric' &&
  110. in_array($field['field'], ['wid', 'uid'])) {
  111. $save = TRUE;
  112. $new_value = $field;
  113. $new_value['plugin_id'] = 'standard';
  114. // Delete all the attributes related to numeric fields.
  115. unset(
  116. $new_value['set_precision'],
  117. $new_value['precision'],
  118. $new_value['decimal'],
  119. $new_value['separator'],
  120. $new_value['format_plural'],
  121. $new_value['format_plural_string'],
  122. $new_value['prefix'],
  123. $new_value['suffix']
  124. );
  125. $view->set("display.$display_name.display_options.fields.$field_name", $new_value);
  126. }
  127. }
  128. }
  129. // Iterate all filters looking for type filters to update.
  130. if (isset($display['display_options']['filters'])) {
  131. foreach ($display['display_options']['filters'] as $filter_name => $filter) {
  132. if (isset($filter['table']) &&
  133. $filter['table'] === 'watchdog' &&
  134. $filter['plugin_id'] == 'in_operator' &&
  135. $filter['field'] == 'type') {
  136. $save = TRUE;
  137. $filter['plugin_id'] = 'dblog_types';
  138. $view->set("display.$display_name.display_options.filters.$filter_name", $filter);
  139. }
  140. }
  141. }
  142. }
  143. if ($save) {
  144. $view->save();
  145. }
  146. }
  147. }