/** * Displays information about a search server. * * @param \Drupal\search_api\ServerInterface $search_api_server * The server to display. * * @return array * An array suitable for drupal_render(). */ public function page(ServerInterface $search_api_server) { // Build the search server information. $render = array('view' => array('#theme' => 'search_api_server', '#server' => $search_api_server), '#attached' => array('library' => array('search_api/drupal.search_api.admin_css'))); // Check if the server is enabled. if ($search_api_server->status()) { // Attach the server status form. $render['form'] = $this->formBuilder()->getForm('Drupal\\search_api\\Form\\ServerStatusForm', $search_api_server); } return $render; }
/** * Builds the form for the basic server properties. * * @param \Drupal\search_api\ServerInterface $server * The server that is being created or edited. */ public function buildEntityForm(array &$form, FormStateInterface $form_state, ServerInterface $server) { $form['#attached']['library'][] = 'search_api/drupal.search_api.admin_css'; $form['name'] = array('#type' => 'textfield', '#title' => $this->t('Server name'), '#description' => $this->t('Enter the displayed name for the server.'), '#default_value' => $server->label(), '#required' => TRUE); $form['id'] = array('#type' => 'machine_name', '#default_value' => $server->id(), '#maxlength' => 50, '#required' => TRUE, '#machine_name' => array('exists' => array($this->getStorage(), 'load'), 'source' => array('name')), '#disabled' => !$server->isNew()); $form['status'] = array('#type' => 'checkbox', '#title' => $this->t('Enabled'), '#description' => $this->t('Only enabled servers can index items or execute searches.'), '#default_value' => $server->status()); $form['description'] = array('#type' => 'textarea', '#title' => $this->t('Description'), '#description' => $this->t('Enter a description for the server.'), '#default_value' => $server->getDescription()); $backend_options = $this->getBackendOptions(); if ($backend_options) { if (count($backend_options) == 1) { $server->set('backend', key($backend_options)); } $form['backend'] = array('#type' => 'radios', '#title' => $this->t('Backend'), '#description' => $this->t('Choose a backend to use for this server.'), '#options' => $backend_options, '#default_value' => $server->getBackendId(), '#required' => TRUE, '#ajax' => array('callback' => array(get_class($this), 'buildAjaxBackendConfigForm'), 'wrapper' => 'search-api-backend-config-form', 'method' => 'replace', 'effect' => 'fade')); } else { drupal_set_message($this->t('There are no backend plugins available for the Search API. Please install a <a href=":url">module that provides a backend plugin</a> to proceed.', array(':url' => Url::fromUri('https://www.drupal.org/node/1254698')->toString())), 'error'); $form = array(); } }
/** * {@inheritdoc} */ public function execute(ServerInterface $server = NULL) { $select = $this->database->select('search_api_task', 't'); $select->fields('t')->condition('t.type', array('addIndex', 'updateIndex', 'removeIndex', 'deleteItems', 'deleteAllIndexItems'), 'IN'); if ($server) { if (!$server->status()) { return FALSE; } $select->condition('t.server_id', $server->id()); } else { // By ordering by the server, we can later just load them when we reach // them while looping through the tasks. It is very unlikely there will be // tasks for more than one or two servers, so a *_load_multiple() probably // wouldn't bring any significant advantages, but complicate the code. $select->orderBy('t.server_id'); } // Sometimes the order of tasks might be important, so make sure to order by // the task ID (which should be in order of insertion). $select->orderBy('t.id'); $tasks = $select->execute(); $executed_tasks = array(); $failing_servers = array(); foreach ($tasks as $task) { if (isset($failing_servers[$task->server_id])) { continue; } if (!$server || $server->id() != $task->server_id) { $server = $this->loadServer($task->server_id); if (!$server) { $failing_servers[$task->server_id] = TRUE; continue; } } if (!$server->status()) { continue; } $index = NULL; if ($task->index_id) { $index = $this->loadIndex($task->index_id); } try { switch ($task->type) { case 'addIndex': if ($index) { $server->getBackend()->addIndex($index); } break; case 'updateIndex': if ($index) { if ($task->data) { $index->original = unserialize($task->data); } $server->getBackend()->updateIndex($index); } break; case 'removeIndex': if ($index) { $server->getBackend()->removeIndex($index ? $index : $task->index_id); $this->delete(NULL, $server, $index); } break; case 'deleteItems': if ($index && !$index->isReadOnly()) { $ids = unserialize($task->data); $server->getBackend()->deleteItems($index, $ids); } break; case 'deleteAllIndexItems': if ($index && !$index->isReadOnly()) { $server->getBackend()->deleteAllIndexItems($index); } break; default: // This should never happen. continue 2; } $executed_tasks[] = $task->id; } catch (SearchApiException $e) { // If a task fails, we don't want to execute any other tasks for that // server (since order might be important). watchdog_exception('search_api', $e); $failing_servers[$task->server_id] = TRUE; } } // Delete all successfully executed tasks. if ($executed_tasks) { $this->delete($executed_tasks); } // Return TRUE if no tasks failed (i.e., if we didn't mark any server as // failing). return !$failing_servers; }