search_api_saved_searches.install 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_queued' => array(
  114. 'description' => 'The Unix timestamp when the saved search was last queued.',
  115. 'type' => 'int',
  116. 'not null' => TRUE,
  117. ),
  118. 'last_execute' => array(
  119. 'description' => 'The Unix timestamp when the saved search was last executed.',
  120. 'type' => 'int',
  121. 'not null' => TRUE,
  122. ),
  123. 'notify_interval' => array(
  124. 'description' => 'The interval in which this saved search will be checked for new results, in seconds.',
  125. 'type' => 'int',
  126. 'not null' => FALSE,
  127. ),
  128. 'query' => array(
  129. 'description' => 'The actual search query to be executed.',
  130. 'type' => 'blob',
  131. 'serialize' => TRUE,
  132. 'not null' => TRUE,
  133. ),
  134. 'options' => array(
  135. 'description' => 'Additional options for this saved search.',
  136. 'type' => 'blob',
  137. 'serialize' => TRUE,
  138. 'not null' => TRUE,
  139. ),
  140. 'results' => array(
  141. 'description' => 'A comma-separated list of all "old" results for this saved search.',
  142. 'type' => 'text',
  143. 'not null' => FALSE,
  144. ),
  145. ),
  146. 'indexes' => array(
  147. 'user' => array('mail', 'uid'),
  148. 'notify' => array('enabled', 'notify_interval', 'last_queued', 'last_execute'),
  149. ),
  150. 'primary key' => array('id'),
  151. );
  152. return $schema;
  153. }
  154. /**
  155. * Implements hook_uninstall().
  156. */
  157. function search_api_saved_searches_uninstall() {
  158. variable_del('search_api_saved_searches_search_ids');
  159. }
  160. /**
  161. * Update searches to correctly use the settings' delta, not numeric ID, as the identifier.
  162. */
  163. function search_api_saved_searches_update_7101() {
  164. $settings = db_query('SELECT id, delta FROM {search_api_saved_searches_settings}')->fetchAllKeyed();
  165. db_delete('search_api_saved_search')
  166. ->condition('settings_id', array_keys($settings), 'NOT IN')
  167. ->execute();
  168. foreach ($settings as $id => $delta) {
  169. db_update('search_api_saved_search')
  170. ->condition('settings_id', $id)
  171. ->fields(array('settings_id' => $delta))
  172. ->execute();
  173. }
  174. }
  175. /**
  176. * Add {search_api_saved_search}.last_queued for more accurate queue tracking.
  177. */
  178. function search_api_saved_searches_update_7102() {
  179. $spec = array(
  180. 'description' => 'The Unix timestamp when the saved search was last queued.',
  181. 'type' => 'int',
  182. 'not null' => TRUE,
  183. );
  184. db_change_field('search_api_saved_search', 'last_execute', 'last_queued', $spec);
  185. $spec['description'] = 'The Unix timestamp when the saved search was last executed.';
  186. $spec['not null'] = FALSE;
  187. db_add_field('search_api_saved_search', 'last_execute', $spec);
  188. db_update('search_api_saved_search')
  189. ->expression('last_execute', 'last_queued')
  190. ->execute();
  191. $spec['not null'] = TRUE;
  192. db_change_field('search_api_saved_search', 'last_execute', 'last_execute', $spec);
  193. }
  194. /**
  195. * Optimize indexes on {search_api_saved_search} to improve performance.
  196. */
  197. function search_api_saved_searches_update_7103() {
  198. db_drop_index('search_api_saved_search', 'uid');
  199. db_drop_index('search_api_saved_search', 'mail');
  200. db_add_index('search_api_saved_search', 'user', array('mail', 'uid'));
  201. db_add_index('search_api_saved_search', 'notify', array('enabled', 'notify_interval', 'last_queued', 'last_execute'));
  202. }
  203. /**
  204. * Fix incorrect {search_api_saved_search}.last_queued values.
  205. *
  206. * This might lead to duplicate mails for saved searches in some rare cases, but
  207. * is necessary to prevent saved searches from never being executed at all
  208. * anymore in others.
  209. *
  210. * @see https://www.drupal.org/node/2354863
  211. */
  212. function search_api_saved_searches_update_7104() {
  213. db_update('search_api_saved_search')
  214. ->expression('last_queued', 'last_execute')
  215. ->where('last_queued > last_execute')
  216. ->execute();
  217. }
  218. /**
  219. * Disable saved searches whose users were disabled.
  220. */
  221. function search_api_saved_searches_update_7105() {
  222. // Get the UIDs of all inactive users.
  223. $query = db_select('users', 'u');
  224. $query->fields('u', array('uid'))
  225. ->condition('u.status', 0)
  226. ->condition('u.uid', 0, '<>');
  227. // Then, disable all their searches.
  228. db_update('search_api_saved_search')
  229. ->condition('uid', $query, 'IN')
  230. ->fields(array(
  231. 'enabled' => 0,
  232. ))
  233. ->execute();
  234. }