search_api_saved_searches.install 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Search API saved searches
  5. * module.
  6. */
  7. /**
  8. * Implements hook_schema().
  9. */
  10. function search_api_saved_searches_schema() {
  11. $schema['search_api_saved_searches_settings'] = array(
  12. 'description' => 'The "saved searches" settings for a specific index.',
  13. 'fields' => array(
  14. 'id' => array(
  15. 'description' => 'The primary identifier for saved search settings.',
  16. 'type' => 'serial',
  17. 'unsigned' => TRUE,
  18. 'not null' => TRUE,
  19. ),
  20. 'delta' => array(
  21. 'description' => 'The {block}.delta used for these saved searches.',
  22. 'type' => 'varchar',
  23. 'length' => 32,
  24. 'not null' => TRUE,
  25. 'default' => '0',
  26. ),
  27. 'index_id' => array(
  28. 'description' => 'The {search_api_index}.machine_name this settings record is for.',
  29. 'type' => 'varchar',
  30. 'length' => 50,
  31. 'not null' => TRUE,
  32. ),
  33. 'enabled' => array(
  34. 'description' => 'A flag indicating whether saved searches for this index are enabled.',
  35. 'type' => 'int',
  36. 'size' => 'tiny',
  37. 'not null' => TRUE,
  38. 'default' => 1,
  39. ),
  40. 'options' => array(
  41. 'description' => 'The actual settings for saved searches.',
  42. 'type' => 'blob',
  43. 'serialize' => TRUE,
  44. 'not null' => TRUE,
  45. ),
  46. 'status' => array(
  47. 'type' => 'int',
  48. 'not null' => TRUE,
  49. 'default' => 0x01,
  50. 'size' => 'tiny',
  51. 'description' => 'The exportable status of the entity.',
  52. ),
  53. 'module' => array(
  54. 'description' => 'The name of the providing module if the entity has been defined in code.',
  55. 'type' => 'varchar',
  56. 'length' => 255,
  57. 'not null' => FALSE,
  58. ),
  59. ),
  60. 'indexes' => array(
  61. 'index_id' => array('index_id'),
  62. ),
  63. 'unique keys' => array(
  64. 'delta' => array('delta'),
  65. ),
  66. 'primary key' => array('id'),
  67. );
  68. $schema['search_api_saved_search'] = array(
  69. 'description' => 'A saved search that will be periodically executed.',
  70. 'fields' => array(
  71. 'id' => array(
  72. 'description' => 'The primary identifier for a saved search.',
  73. 'type' => 'serial',
  74. 'unsigned' => TRUE,
  75. 'not null' => TRUE,
  76. ),
  77. 'uid' => array(
  78. 'description' => 'The {users}.uid that created this saved search.',
  79. 'type' => 'int',
  80. 'not null' => TRUE,
  81. 'default' => 0,
  82. ),
  83. 'settings_id' => array(
  84. 'description' => 'The {search_api_saved_searches_settings}.delta this saved search uses.',
  85. 'type' => 'varchar',
  86. 'length' => 50,
  87. 'not null' => TRUE,
  88. ),
  89. 'enabled' => array(
  90. 'description' => 'The state of this saved search, enabled (1) or not (0).',
  91. 'type' => 'int',
  92. 'size' => 'tiny',
  93. 'not null' => TRUE,
  94. 'default' => 0,
  95. ),
  96. 'name' => array(
  97. 'description' => 'The displayed name for this saved search.',
  98. 'type' => 'varchar',
  99. 'length' => 50,
  100. 'not null' => TRUE,
  101. ),
  102. 'mail' => array(
  103. 'description' => 'The mail address that should receive notifications.',
  104. 'type' => 'varchar',
  105. 'length' => 100,
  106. 'not null' => FALSE,
  107. ),
  108. 'created' => array(
  109. 'description' => 'The Unix timestamp when the saved search was created.',
  110. 'type' => 'int',
  111. 'not null' => TRUE,
  112. ),
  113. 'last_execute' => array(
  114. 'description' => 'The Unix timestamp when the saved search was last executed.',
  115. 'type' => 'int',
  116. 'not null' => TRUE,
  117. ),
  118. 'notify_interval' => array(
  119. 'description' => 'The interval in which this saved search will be checked for new results, in seconds.',
  120. 'type' => 'int',
  121. 'not null' => FALSE,
  122. ),
  123. 'query' => array(
  124. 'description' => 'The actual search query to be executed.',
  125. 'type' => 'blob',
  126. 'serialize' => TRUE,
  127. 'not null' => TRUE,
  128. ),
  129. 'options' => array(
  130. 'description' => 'Additional options for this saved search.',
  131. 'type' => 'blob',
  132. 'serialize' => TRUE,
  133. 'not null' => TRUE,
  134. ),
  135. 'results' => array(
  136. 'description' => 'A comma-separated list of all "old" results for this saved search.',
  137. 'type' => 'text',
  138. 'not null' => FALSE,
  139. ),
  140. ),
  141. 'indexes' => array(
  142. 'uid' => array('uid'),
  143. 'mail' => array('mail'),
  144. ),
  145. 'primary key' => array('id'),
  146. );
  147. return $schema;
  148. }
  149. /**
  150. * Implements hook_uninstall().
  151. */
  152. function search_api_saved_searches_uninstall() {
  153. variable_del('search_api_saved_searches_search_ids');
  154. }
  155. /**
  156. * Update searches to correctly use the settings' delta, not numeric ID, as the identifier.
  157. */
  158. function search_api_saved_searches_update_7101() {
  159. $settings = db_query('SELECT id, delta FROM {search_api_saved_searches_settings}')->fetchAllKeyed();
  160. db_delete('search_api_saved_search')
  161. ->condition('settings_id', array_keys($settings), 'NOT IN')
  162. ->execute();
  163. foreach ($settings as $id => $delta) {
  164. db_update('search_api_saved_search')
  165. ->condition('settings_id', $id)
  166. ->fields(array('settings_id' => $delta))
  167. ->execute();
  168. }
  169. }