You are here

search_api_saved_searches.install in Search API Saved Searches 7

Same filename and directory in other branches
  1. 8 search_api_saved_searches.install

Install, update and uninstall functions for the Search API saved searches module.

File

search_api_saved_searches.install
View source
<?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' => 0x1,
        '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');
  variable_del('search_api_saved_searches_load_max');
  variable_del('search_api_saved_searches_debug_messages');
}

/**
 * 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);
}

Functions

Namesort descending Description
search_api_saved_searches_schema Implements hook_schema().
search_api_saved_searches_uninstall Implements hook_uninstall().
search_api_saved_searches_update_7101 Update searches to correctly use the settings' delta, not numeric ID, as the identifier.
search_api_saved_searches_update_7102 Add {search_api_saved_search}.last_queued for more accurate queue tracking.
search_api_saved_searches_update_7103 Optimize indexes on {search_api_saved_search} to improve performance.
search_api_saved_searches_update_7104 Fix incorrect {search_api_saved_search}.last_queued values.
search_api_saved_searches_update_7105 Disable saved searches whose users were disabled.
search_api_saved_searches_update_7106 Increase the maximum length of the {search_api_saved_search}.name field.