You are here

function drush_search_api_set_index_server in Search API 7

Same name and namespace in other branches
  1. 8 search_api.drush.inc \drush_search_api_set_index_server()

Set the server for a given index.

File

./search_api.drush.inc, line 552
Drush commands for SearchAPI.

Code

function drush_search_api_set_index_server($index_id = NULL, $server_id = NULL) {
  if (search_api_drush_static(__FUNCTION__)) {
    return;
  }

  // Make sure we have parameters to work with.
  if (empty($index_id) || empty($server_id)) {
    drush_log(dt('You must specify both an index and server.'), 'error');
    return;
  }

  // Fetch current index and server data.
  $indexes = search_api_drush_get_index($index_id);
  $servers = search_api_drush_get_server($server_id);
  if (empty($indexes) || empty($servers)) {

    // If the specified index or server can't be found, just return. An
    // appropriate error message should have been printed already.
    return;
  }

  // Set the new server on the index.
  $success = FALSE;
  $index = reset($indexes);
  $server = reset($servers);
  try {
    $success = $index
      ->update(array(
      'server' => $server->machine_name,
    ));
  } catch (SearchApiException $e) {
    drush_log($e
      ->getMessage(), 'error');
  }
  if ($success === FALSE) {
    drush_log(dt('There was an error setting index !index to use server !server.', array(
      '!index' => $index->name,
      '!server' => $server->name,
    )), 'error');
  }
  elseif (!$success) {
    drush_log(dt('Index !index was already using server !server.', array(
      '!index' => $index->name,
      '!server' => $server->name,
    )), 'ok');
  }
  else {
    drush_log(dt('Index !index has been set to use server !server and items have been queued for indexing.', array(
      '!index' => $index->name,
      '!server' => $server->name,
    )), 'ok');
  }
}