123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- <?php
- /**
- * @file
- * Install, update and uninstall functions for the Search API saved searches
- * module.
- */
- /**
- * Implements hook_schema().
- */
- function search_api_saved_searches_schema() {
- $schema['search_api_saved_searches_settings'] = array(
- 'description' => 'The "saved searches" settings for a specific index.',
- 'fields' => array(
- 'id' => array(
- 'description' => 'The primary identifier for saved search settings.',
- 'type' => 'serial',
- 'unsigned' => TRUE,
- 'not null' => TRUE,
- ),
- 'delta' => array(
- 'description' => 'The {block}.delta used for these saved searches.',
- 'type' => 'varchar',
- 'length' => 32,
- 'not null' => TRUE,
- 'default' => '0',
- ),
- 'index_id' => array(
- 'description' => 'The {search_api_index}.machine_name this settings record is for.',
- 'type' => 'varchar',
- 'length' => 50,
- 'not null' => TRUE,
- ),
- 'enabled' => array(
- 'description' => 'A flag indicating whether saved searches for this index are enabled.',
- 'type' => 'int',
- 'size' => 'tiny',
- 'not null' => TRUE,
- 'default' => 1,
- ),
- 'options' => array(
- 'description' => 'The actual settings for saved searches.',
- 'type' => 'blob',
- 'serialize' => TRUE,
- 'not null' => TRUE,
- ),
- 'status' => array(
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 0x01,
- 'size' => 'tiny',
- 'description' => 'The exportable status of the entity.',
- ),
- 'module' => array(
- 'description' => 'The name of the providing module if the entity has been defined in code.',
- 'type' => 'varchar',
- 'length' => 255,
- 'not null' => FALSE,
- ),
- ),
- 'indexes' => array(
- 'index_id' => array('index_id'),
- ),
- 'unique keys' => array(
- 'delta' => array('delta'),
- ),
- 'primary key' => array('id'),
- );
- $schema['search_api_saved_search'] = array(
- 'description' => 'A saved search that will be periodically executed.',
- 'fields' => array(
- 'id' => array(
- 'description' => 'The primary identifier for a saved search.',
- 'type' => 'serial',
- 'unsigned' => TRUE,
- 'not null' => TRUE,
- ),
- 'uid' => array(
- 'description' => 'The {users}.uid that created this saved search.',
- 'type' => 'int',
- 'not null' => TRUE,
- 'default' => 0,
- ),
- 'settings_id' => array(
- 'description' => 'The {search_api_saved_searches_settings}.delta this saved search uses.',
- 'type' => 'varchar',
- 'length' => 50,
- 'not null' => TRUE,
- ),
- 'enabled' => array(
- 'description' => 'The state of this saved search, enabled (1) or not (0).',
- 'type' => 'int',
- 'size' => 'tiny',
- 'not null' => TRUE,
- 'default' => 0,
- ),
- 'name' => array(
- 'description' => 'The displayed name for this saved search.',
- 'type' => 'varchar',
- 'length' => 255,
- 'not null' => TRUE,
- ),
- 'mail' => array(
- 'description' => 'The mail address that should receive notifications.',
- 'type' => 'varchar',
- 'length' => 100,
- 'not null' => FALSE,
- ),
- 'created' => array(
- 'description' => 'The Unix timestamp when the saved search was created.',
- 'type' => 'int',
- 'not null' => TRUE,
- ),
- 'last_queued' => array(
- 'description' => 'The Unix timestamp when the saved search was last queued.',
- 'type' => 'int',
- 'not null' => TRUE,
- ),
- 'last_execute' => array(
- 'description' => 'The Unix timestamp when the saved search was last executed.',
- 'type' => 'int',
- 'not null' => TRUE,
- ),
- 'notify_interval' => array(
- 'description' => 'The interval in which this saved search will be checked for new results, in seconds.',
- 'type' => 'int',
- 'not null' => FALSE,
- ),
- 'query' => array(
- 'description' => 'The actual search query to be executed.',
- 'type' => 'blob',
- 'serialize' => TRUE,
- 'not null' => TRUE,
- ),
- 'options' => array(
- 'description' => 'Additional options for this saved search.',
- 'type' => 'blob',
- 'serialize' => TRUE,
- 'not null' => TRUE,
- ),
- 'results' => array(
- 'description' => 'A comma-separated list of all "old" results for this saved search.',
- 'type' => 'text',
- 'not null' => FALSE,
- ),
- ),
- 'indexes' => array(
- 'user' => array('mail', 'uid'),
- 'notify' => array('enabled', 'notify_interval', 'last_queued', 'last_execute'),
- ),
- 'primary key' => array('id'),
- );
- return $schema;
- }
- /**
- * Implements hook_uninstall().
- */
- function search_api_saved_searches_uninstall() {
- variable_del('search_api_saved_searches_search_ids');
- variable_del('search_api_saved_search_queue_item_time');
- }
- /**
- * Update searches to correctly use the settings' delta, not numeric ID, as the identifier.
- */
- function search_api_saved_searches_update_7101() {
- $settings = db_query('SELECT id, delta FROM {search_api_saved_searches_settings}')->fetchAllKeyed();
- db_delete('search_api_saved_search')
- ->condition('settings_id', array_keys($settings), 'NOT IN')
- ->execute();
- foreach ($settings as $id => $delta) {
- db_update('search_api_saved_search')
- ->condition('settings_id', $id)
- ->fields(array('settings_id' => $delta))
- ->execute();
- }
- }
- /**
- * Add {search_api_saved_search}.last_queued for more accurate queue tracking.
- */
- function search_api_saved_searches_update_7102() {
- $spec = array(
- 'description' => 'The Unix timestamp when the saved search was last queued.',
- 'type' => 'int',
- 'not null' => TRUE,
- );
- db_change_field('search_api_saved_search', 'last_execute', 'last_queued', $spec);
- $spec['description'] = 'The Unix timestamp when the saved search was last executed.';
- $spec['not null'] = FALSE;
- db_add_field('search_api_saved_search', 'last_execute', $spec);
- db_update('search_api_saved_search')
- ->expression('last_execute', 'last_queued')
- ->execute();
- $spec['not null'] = TRUE;
- db_change_field('search_api_saved_search', 'last_execute', 'last_execute', $spec);
- }
- /**
- * Optimize indexes on {search_api_saved_search} to improve performance.
- */
- function search_api_saved_searches_update_7103() {
- db_drop_index('search_api_saved_search', 'uid');
- db_drop_index('search_api_saved_search', 'mail');
- db_add_index('search_api_saved_search', 'user', array('mail', 'uid'));
- db_add_index('search_api_saved_search', 'notify', array('enabled', 'notify_interval', 'last_queued', 'last_execute'));
- }
- /**
- * Fix incorrect {search_api_saved_search}.last_queued values.
- *
- * This might lead to duplicate mails for saved searches in some rare cases, but
- * is necessary to prevent saved searches from never being executed at all
- * anymore in others.
- *
- * @see https://www.drupal.org/node/2354863
- */
- function search_api_saved_searches_update_7104() {
- db_update('search_api_saved_search')
- ->expression('last_queued', 'last_execute')
- ->where('last_queued > last_execute')
- ->execute();
- }
- /**
- * Disable saved searches whose users were disabled.
- */
- function search_api_saved_searches_update_7105() {
- // Get the UIDs of all inactive users.
- $query = db_select('users', 'u');
- $query->fields('u', array('uid'))
- ->condition('u.status', 0)
- ->condition('u.uid', 0, '<>');
- // Then, disable all their searches.
- db_update('search_api_saved_search')
- ->condition('uid', $query, 'IN')
- ->fields(array(
- 'enabled' => 0,
- ))
- ->execute();
- }
- /**
- * Increase the maximum length of the {search_api_saved_search}.name field.
- */
- function search_api_saved_searches_update_7106() {
- $spec = array(
- 'description' => 'The displayed name for this saved search.',
- 'type' => 'varchar',
- 'length' => 255,
- 'not null' => TRUE,
- );
- db_change_field('search_api_saved_search', 'name', 'name', $spec);
- }
|