Drupal investigation

search.install 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Search module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function search_schema() {
  10. $schema['search_dataset'] = [
  11. 'description' => 'Stores items that will be searched.',
  12. 'fields' => [
  13. 'sid' => [
  14. 'type' => 'int',
  15. 'unsigned' => TRUE,
  16. 'not null' => TRUE,
  17. 'default' => 0,
  18. 'description' => 'Search item ID, e.g. node ID for nodes.',
  19. ],
  20. 'langcode' => [
  21. 'type' => 'varchar_ascii',
  22. 'length' => '12',
  23. 'not null' => TRUE,
  24. 'description' => 'The {languages}.langcode of the item variant.',
  25. 'default' => '',
  26. ],
  27. 'type' => [
  28. 'type' => 'varchar_ascii',
  29. 'length' => 64,
  30. 'not null' => TRUE,
  31. 'description' => 'Type of item, e.g. node.',
  32. ],
  33. 'data' => [
  34. 'type' => 'text',
  35. 'not null' => TRUE,
  36. 'size' => 'big',
  37. 'description' => 'List of space-separated words from the item.',
  38. ],
  39. 'reindex' => [
  40. 'type' => 'int',
  41. 'unsigned' => TRUE,
  42. 'not null' => TRUE,
  43. 'default' => 0,
  44. 'description' => 'Set to force node reindexing.',
  45. ],
  46. ],
  47. 'primary key' => ['sid', 'langcode', 'type'],
  48. ];
  49. $schema['search_index'] = [
  50. 'description' => 'Stores the search index, associating words, items and scores.',
  51. 'fields' => [
  52. 'word' => [
  53. 'type' => 'varchar',
  54. 'length' => 50,
  55. 'not null' => TRUE,
  56. 'default' => '',
  57. 'description' => 'The {search_total}.word that is associated with the search item.',
  58. ],
  59. 'sid' => [
  60. 'type' => 'int',
  61. 'unsigned' => TRUE,
  62. 'not null' => TRUE,
  63. 'default' => 0,
  64. 'description' => 'The {search_dataset}.sid of the searchable item to which the word belongs.',
  65. ],
  66. 'langcode' => [
  67. 'type' => 'varchar_ascii',
  68. 'length' => '12',
  69. 'not null' => TRUE,
  70. 'description' => 'The {languages}.langcode of the item variant.',
  71. 'default' => '',
  72. ],
  73. 'type' => [
  74. 'type' => 'varchar_ascii',
  75. 'length' => 64,
  76. 'not null' => TRUE,
  77. 'description' => 'The {search_dataset}.type of the searchable item to which the word belongs.',
  78. ],
  79. 'score' => [
  80. 'type' => 'float',
  81. 'not null' => FALSE,
  82. 'description' => 'The numeric score of the word, higher being more important.',
  83. ],
  84. ],
  85. 'indexes' => [
  86. 'sid_type' => ['sid', 'langcode', 'type'],
  87. ],
  88. 'foreign keys' => [
  89. 'search_dataset' => [
  90. 'table' => 'search_dataset',
  91. 'columns' => [
  92. 'sid' => 'sid',
  93. 'langcode' => 'langcode',
  94. 'type' => 'type',
  95. ],
  96. ],
  97. ],
  98. 'primary key' => ['word', 'sid', 'langcode', 'type'],
  99. ];
  100. $schema['search_total'] = [
  101. 'description' => 'Stores search totals for words.',
  102. 'fields' => [
  103. 'word' => [
  104. 'description' => 'Primary Key: Unique word in the search index.',
  105. 'type' => 'varchar',
  106. 'length' => 50,
  107. 'not null' => TRUE,
  108. 'default' => '',
  109. ],
  110. 'count' => [
  111. 'description' => "The count of the word in the index using Zipf's law to equalize the probability distribution.",
  112. 'type' => 'float',
  113. 'not null' => FALSE,
  114. ],
  115. ],
  116. 'primary key' => ['word'],
  117. ];
  118. return $schema;
  119. }
  120. /**
  121. * Implements hook_requirements().
  122. *
  123. * For the Status Report, return information about search index status.
  124. */
  125. function search_requirements($phase) {
  126. $requirements = [];
  127. if ($phase == 'runtime') {
  128. $remaining = 0;
  129. $total = 0;
  130. $search_page_repository = \Drupal::service('search.search_page_repository');
  131. foreach ($search_page_repository->getIndexableSearchPages() as $entity) {
  132. $status = $entity->getPlugin()->indexStatus();
  133. $remaining += $status['remaining'];
  134. $total += $status['total'];
  135. }
  136. $done = $total - $remaining;
  137. // Use floor() to calculate the percentage, so if it is not quite 100%, it
  138. // will show as 99%, to indicate "almost done".
  139. $percent = ($total > 0 ? floor(100 * $done / $total) : 100);
  140. $requirements['search_status'] = [
  141. 'title' => t('Search index progress'),
  142. 'value' => t('@percent% (@remaining remaining)', ['@percent' => $percent, '@remaining' => $remaining]),
  143. 'severity' => REQUIREMENT_INFO,
  144. ];
  145. }
  146. return $requirements;
  147. }