webprofiler.install 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the webprofiler module.
  5. */
  6. use Drupal\Core\Url;
  7. /**
  8. * Implements hook_schema().
  9. */
  10. function webprofiler_schema() {
  11. $schema['webprofiler'] = [
  12. 'description' => 'Webprofiler profiles storage.',
  13. 'fields' => [
  14. 'token' => [
  15. 'description' => 'Profile token.',
  16. 'type' => 'varchar',
  17. 'length' => 6,
  18. 'not null' => TRUE,
  19. ],
  20. 'data' => [
  21. 'description' => 'Profile data.',
  22. 'type' => 'text',
  23. 'size' => 'big',
  24. 'not null' => TRUE,
  25. ],
  26. 'ip' => [
  27. 'description' => 'Request IP.',
  28. 'type' => 'varchar',
  29. 'length' => 64,
  30. 'not null' => TRUE,
  31. ],
  32. 'method' => [
  33. 'description' => 'Request method.',
  34. 'type' => 'varchar',
  35. 'length' => 6,
  36. 'not null' => TRUE,
  37. ],
  38. 'url' => [
  39. 'description' => 'Requested URL.',
  40. 'type' => 'varchar',
  41. 'length' => 2048,
  42. 'not null' => TRUE,
  43. ],
  44. 'time' => [
  45. 'description' => 'Request time.',
  46. 'type' => 'int',
  47. 'unsigned' => TRUE,
  48. 'not null' => TRUE,
  49. ],
  50. 'parent' => [
  51. 'description' => 'Profile parent.',
  52. 'type' => 'varchar',
  53. 'length' => 6,
  54. 'not null' => FALSE,
  55. ],
  56. 'created_at' => [
  57. 'description' => 'Profile created time.',
  58. 'type' => 'int',
  59. 'unsigned' => TRUE,
  60. 'not null' => TRUE,
  61. ],
  62. 'status_code' => [
  63. 'description' => 'Profile status code.',
  64. 'type' => 'int',
  65. 'size' => 'small',
  66. 'unsigned' => TRUE,
  67. 'not null' => TRUE,
  68. ],
  69. ],
  70. 'indexes' => [
  71. 'created_at' => ['created_at'],
  72. 'ip' => ['ip'],
  73. 'method' => ['method'],
  74. 'parent' => ['parent'],
  75. ],
  76. 'primary key' => ['token'],
  77. ];
  78. return $schema;
  79. }
  80. /**
  81. * Implements hook_requirements().
  82. */
  83. function webprofiler_requirements($phase) {
  84. $requirements = [];
  85. if ('runtime' == $phase) {
  86. $has_d3 = _webprofiler_verify_library('webprofiler', 'd3');
  87. $requirements['d3js'] = [
  88. 'title' => t('D3.js library'),
  89. 'value' => $has_d3 ? t('Enabled') : t('Not found'),
  90. ];
  91. if (!$has_d3) {
  92. $requirements['d3js']['severity'] = REQUIREMENT_WARNING;
  93. $requirements['d3js']['description'] = [
  94. '#prefix' => ' ',
  95. '#markup' => t('Webprofiler module requires D3.js library to properly render data. Composer based install recommended, see README.md file for instructions.'),
  96. ];
  97. }
  98. $has_highlight = _webprofiler_verify_library('webprofiler', 'highlightjs');
  99. $requirements['highlightjs'] = [
  100. 'title' => t('highlight.js library'),
  101. 'value' => $has_highlight ? t('Enabled') : t('Not found'),
  102. ];
  103. if (!$has_highlight) {
  104. $requirements['highlightjs']['severity'] = REQUIREMENT_WARNING;
  105. $requirements['highlightjs']['description'] = [
  106. '#prefix' => ' ',
  107. '#markup' => t('Webprofiler module requires highlight.js library to syntax highlight collected queries. Composer based install recommended, see README.md file for instructions.'),
  108. ];
  109. }
  110. }
  111. return $requirements;
  112. }
  113. /**
  114. * Verify that the library files exist.
  115. *
  116. * @param string $extension
  117. * The name of the extension that registered a library.
  118. * @param string $name
  119. * The name of a registered library to retrieve.
  120. *
  121. * @return bool
  122. * TRUE if all files of this library exists, FALSE otherwise
  123. *
  124. * @see https://drupal.org/node/2231385
  125. */
  126. function _webprofiler_verify_library($extension, $name) {
  127. /** @var Drupal\Core\Asset\LibraryDiscovery $library_discovery */
  128. $library_discovery = \Drupal::service('library.discovery');
  129. $library = $library_discovery->getLibraryByName($extension, $name);
  130. $exist = TRUE;
  131. if ($library['js']) {
  132. foreach ($library['js'] as $js) {
  133. if ($js['type'] == 'file') {
  134. if (!file_exists(DRUPAL_ROOT . '/' . $js['data'])) {
  135. $exist = FALSE;
  136. }
  137. }
  138. }
  139. }
  140. if ($library['css']) {
  141. foreach ($library['css'] as $css) {
  142. if ($css['type'] == 'file') {
  143. if (!file_exists(DRUPAL_ROOT . '/' . $css['data'])) {
  144. $exist = FALSE;
  145. }
  146. }
  147. }
  148. }
  149. if ($library['dependencies']) {
  150. foreach ($library['dependencies'] as $dependency) {
  151. $parts = explode('/', $dependency);
  152. $exist = _webprofiler_verify_library($parts[0], $parts[1]);
  153. }
  154. }
  155. return $exist;
  156. }
  157. /**
  158. * Add a status_code column to the webprofiler table.
  159. */
  160. function webprofiler_update_8001() {
  161. $database = \Drupal::database();
  162. $schema = $database->schema();
  163. $spec = array(
  164. 'description' => 'Profile status code.',
  165. 'type' => 'int',
  166. 'size' => 'small',
  167. 'unsigned' => TRUE,
  168. 'not null' => TRUE,
  169. );
  170. $schema->addField('webprofiler', 'status_code', $spec);
  171. }