You are here

function search_api_update_7116 in Search API 7

Transfers the tasks for disabled servers to a separate database table.

File

./search_api.install, line 952
Install, update and uninstall functions for the Search API module.

Code

function search_api_update_7116() {

  // Create table.
  $table = array(
    'description' => 'Stores pending tasks for servers.',
    'fields' => array(
      'id' => array(
        'description' => 'An integer identifying this task.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'server_id' => array(
        'description' => 'The {search_api_server}.machine_name for which this task should be executed.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'A keyword identifying the type of task that should be executed.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => TRUE,
      ),
      'index_id' => array(
        'description' => 'The {search_api_index}.machine_name to which this task pertains, if applicable for this type.',
        'type' => 'varchar',
        'length' => 50,
        'not null' => FALSE,
      ),
      'data' => array(
        'description' => 'Some data needed for the task, might be optional depending on the type.',
        'type' => 'text',
        'size' => 'medium',
        'serialize' => TRUE,
        'not null' => FALSE,
      ),
    ),
    'indexes' => array(
      'server' => array(
        'server_id',
      ),
    ),
    'primary key' => array(
      'id',
    ),
  );
  db_create_table('search_api_task', $table);

  // Collect old tasks.
  $tasks = array();
  foreach (variable_get('search_api_tasks', array()) as $server => $indexes) {
    foreach ($indexes as $index => $old_tasks) {
      if (in_array('clear all', $old_tasks)) {
        $tasks[] = array(
          'server_id' => $server,
          'type' => 'deleteItems',
        );
      }
      if (in_array('remove', $old_tasks)) {
        $tasks[] = array(
          'server_id' => $server,
          'type' => 'removeIndex',
          'index_id' => $index,
        );
      }
    }
  }
  variable_del('search_api_tasks');
  $select = db_select('search_api_index', 'i')
    ->fields('i', array(
    'machine_name',
    'server',
  ));
  $select
    ->innerJoin('search_api_server', 's', 'i.server = s.machine_name AND s.enabled = 0');
  $index_ids = array();
  foreach ($select
    ->execute() as $index) {
    $index_ids[] = $index->machine_name;
    $tasks[] = array(
      'server_id' => $index->server,
      'type' => 'removeIndex',
      'index_id' => $index->machine_name,
    );
  }
  if ($index_ids) {
    db_update('search_api_index')
      ->fields(array(
      'enabled' => 0,
      'server' => NULL,
    ))
      ->condition('machine_name', $index_ids)
      ->execute();
  }
  if ($tasks) {
    $insert = db_insert('search_api_task')
      ->fields(array(
      'server_id',
      'type',
      'index_id',
      'data',
    ));
    foreach ($tasks as $task) {
      $task += array(
        'index_id' => NULL,
        'data' => NULL,
      );
      $insert
        ->values($task);
    }
    $insert
      ->execute();
  }
}