View source
<?php
namespace Drupal\search_api\Entity;
use Drupal\Component\Plugin\DependentPluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\search_api\Datasource\DatasourceInterface;
use Drupal\search_api\Event\IndexingItemsEvent;
use Drupal\search_api\Event\ItemsIndexedEvent;
use Drupal\search_api\Event\ReindexScheduledEvent;
use Drupal\search_api\Event\SearchApiEvents;
use Drupal\search_api\IndexInterface;
use Drupal\search_api\Item\FieldInterface;
use Drupal\search_api\LoggerTrait;
use Drupal\search_api\Processor\ProcessorInterface;
use Drupal\search_api\Query\QueryInterface;
use Drupal\search_api\Query\ResultSetInterface;
use Drupal\search_api\SearchApiException;
use Drupal\search_api\ServerInterface;
use Drupal\search_api\Tracker\TrackerInterface;
use Drupal\search_api\Utility\Utility;
use Drupal\Core\TempStore\TempStoreException;
use Drupal\views\Views;
class Index extends ConfigEntityBase implements IndexInterface {
use InstallingTrait;
use LoggerTrait;
protected $id;
protected $name;
protected $description;
protected $read_only = FALSE;
protected $field_settings = [];
protected $fieldInstances;
protected $options = [];
protected $datasource_settings = [];
protected $datasourceInstances;
protected $tracker_settings = NULL;
protected $trackerInstance;
protected $server;
protected $serverInstance;
protected $processor_settings = [];
protected $processorInstances;
protected $properties = [];
protected $batchTracking = 0;
public function id() {
return $this->id;
}
public function getDescription() {
return $this->description;
}
public function isReadOnly() {
return $this->read_only;
}
public function getOption($name, $default = NULL) {
return $this->options[$name] ?? $default;
}
public function getOptions() {
return $this->options;
}
public function setOption($name, $option) {
$this->options[$name] = $option;
return $this;
}
public function setOptions(array $options) {
$this->options = $options;
return $this;
}
public function getDatasources() {
if ($this->datasourceInstances === NULL) {
$this->datasourceInstances = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createDatasourcePlugins($this, array_keys($this->datasource_settings));
}
return $this->datasourceInstances;
}
public function getDatasourceIds() {
return array_keys($this
->getDatasources());
}
public function isValidDatasource($datasource_id) {
$datasources = $this
->getDatasources();
return !empty($datasources[$datasource_id]);
}
public function getDatasource($datasource_id) {
$datasources = $this
->getDatasources();
if (empty($datasources[$datasource_id])) {
$index_label = $this
->label();
throw new SearchApiException("The datasource with ID '{$datasource_id}' could not be retrieved for index '{$index_label}'.");
}
return $datasources[$datasource_id];
}
public function addDatasource(DatasourceInterface $datasource) {
if ($this->datasourceInstances === NULL) {
$this
->getDatasources();
}
$this->datasourceInstances[$datasource
->getPluginId()] = $datasource;
return $this;
}
public function removeDatasource($datasource_id) {
if ($this->datasourceInstances === NULL) {
$this
->getDatasources();
}
unset($this->datasourceInstances[$datasource_id]);
return $this;
}
public function setDatasources(array $datasources = NULL) {
$this->datasourceInstances = $datasources;
return $this;
}
public function getEntityTypes($return_bool = FALSE) {
$types = [];
foreach ($this
->getDatasources() as $datasource_id => $datasource) {
if ($type = $datasource
->getEntityTypeId()) {
$types[$datasource_id] = $type;
}
}
return $types;
}
public function hasValidTracker() {
return (bool) \Drupal::getContainer()
->get('plugin.manager.search_api.tracker')
->getDefinition($this
->getTrackerId(), FALSE);
}
public function getTrackerId() {
if ($this->trackerInstance) {
return $this->trackerInstance
->getPluginId();
}
if (empty($this->tracker_settings)) {
return \Drupal::config('search_api.settings')
->get('default_tracker');
}
reset($this->tracker_settings);
return key($this->tracker_settings);
}
public function getTrackerInstance() {
if (!$this->trackerInstance) {
$tracker_id = $this
->getTrackerId();
$configuration = [];
if (!empty($this->tracker_settings[$tracker_id])) {
$configuration = $this->tracker_settings[$tracker_id];
}
$this->trackerInstance = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createTrackerPlugin($this, $tracker_id, $configuration);
}
return $this->trackerInstance;
}
public function setTracker(TrackerInterface $tracker) {
$this->trackerInstance = $tracker;
return $this;
}
public function hasValidServer() {
return $this->serverInstance || $this->server !== NULL && Server::load($this->server);
}
public function isServerEnabled() {
return $this
->hasValidServer() && $this
->getServerInstance()
->status();
}
public function getServerId() {
return $this->server;
}
public function getServerInstance() {
if (!$this->serverInstance && $this->server) {
$this->serverInstance = Server::load($this->server);
if (!$this->serverInstance) {
$index_label = $this
->label();
throw new SearchApiException("The server with ID '{$this->server}' could not be retrieved for index '{$index_label}'.");
}
}
return $this->serverInstance;
}
public function setServer(ServerInterface $server = NULL) {
$this->serverInstance = $server;
$this->server = $server ? $server
->id() : NULL;
return $this;
}
public function getProcessors() {
if ($this->processorInstances !== NULL) {
return $this->processorInstances;
}
$this->processorInstances = [];
$processors = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createProcessorPlugins($this);
foreach ($processors as $processor_id => $processor) {
if (isset($this->processor_settings[$processor_id]) || $processor
->isLocked()) {
$this->processorInstances[$processor_id] = $processor;
}
}
return $this->processorInstances;
}
public function getProcessorsByStage($stage, array $overrides = []) {
$processors = $this
->getProcessors();
$processor_weights = [];
foreach ($processors as $name => $processor) {
if ($processor
->supportsStage($stage)) {
$processor_weights[$name] = $processor
->getWeight($stage);
}
}
$plugin_helper = \Drupal::getContainer()
->get('search_api.plugin_helper');
foreach ($overrides as $name => $config) {
$processor = $plugin_helper
->createProcessorPlugin($this, $name, $config);
if ($processor
->supportsStage($stage)) {
$processors[$name] = $processor;
$processor_weights[$name] = $processor
->getWeight($stage);
}
else {
unset($processor_weights[$name]);
}
}
asort($processor_weights);
$return_processors = [];
foreach ($processor_weights as $name => $weight) {
$return_processors[$name] = $processors[$name];
}
return $return_processors;
}
public function isValidProcessor($processor_id) {
$processors = $this
->getProcessors();
return !empty($processors[$processor_id]);
}
public function getProcessor($processor_id) {
$processors = $this
->getProcessors();
if (empty($processors[$processor_id])) {
$index_label = $this
->label();
throw new SearchApiException("The processor with ID '{$processor_id}' could not be retrieved for index '{$index_label}'.");
}
return $processors[$processor_id];
}
public function addProcessor(ProcessorInterface $processor) {
if ($this->processorInstances === NULL) {
$this
->getProcessors();
}
$this->processorInstances[$processor
->getPluginId()] = $processor;
return $this;
}
public function removeProcessor($processor_id) {
if ($this->processorInstances === NULL) {
$this
->getProcessors();
}
unset($this->processorInstances[$processor_id]);
return $this;
}
public function setProcessors(array $processors) {
$this->processorInstances = $processors;
return $this;
}
public function alterIndexedItems(array &$items) {
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_ALTER_ITEMS) as $processor) {
$processor
->alterIndexedItems($items);
}
}
public function preprocessIndexItems(array $items) {
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_PREPROCESS_INDEX) as $processor) {
$processor
->preprocessIndexItems($items);
}
}
public function preprocessSearchQuery(QueryInterface $query) {
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_PREPROCESS_QUERY) as $processor) {
$processor
->preprocessSearchQuery($query);
}
}
public function postprocessSearchResults(ResultSetInterface $results) {
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_POSTPROCESS_QUERY) as $processor) {
$processor
->postprocessSearchResults($results);
}
}
public function addField(FieldInterface $field) {
$field_id = $field
->getFieldIdentifier();
$reserved = \Drupal::getContainer()
->get('search_api.fields_helper')
->isFieldIdReserved($field_id);
if ($reserved) {
throw new SearchApiException("'{$field_id}' is a reserved value and cannot be used as the machine name of a normal field.");
}
$old_field = $this
->getField($field_id);
if ($old_field && $old_field != $field) {
throw new SearchApiException("Cannot add field with machine name '{$field_id}': machine name is already taken.");
}
$this->fieldInstances[$field_id] = $field;
return $this;
}
public function renameField($old_field_id, $new_field_id) {
if (!isset($this
->getFields()[$old_field_id])) {
throw new SearchApiException("Could not rename field with machine name '{$old_field_id}': no such field.");
}
$reserved = \Drupal::getContainer()
->get('search_api.fields_helper')
->isFieldIdReserved($new_field_id);
if ($reserved) {
throw new SearchApiException("'{$new_field_id}' is a reserved value and cannot be used as the machine name of a normal field.");
}
if (isset($this
->getFields()[$new_field_id])) {
throw new SearchApiException("'{$new_field_id}' already exists and can't be used as a new field id.");
}
$this->fieldInstances[$new_field_id] = $this->fieldInstances[$old_field_id];
unset($this->fieldInstances[$old_field_id]);
$this->fieldInstances[$new_field_id]
->setFieldIdentifier($new_field_id);
return $this;
}
public function removeField($field_id) {
$field = $this
->getField($field_id);
if (!$field) {
return $this;
}
if ($field
->isIndexedLocked()) {
throw new SearchApiException("Cannot remove field with machine name '{$field_id}': field is locked.");
}
unset($this->fieldInstances[$field_id]);
return $this;
}
public function setFields(array $fields) {
$this->fieldInstances = $fields;
}
public function getFields($include_server_defined = FALSE) {
if (!isset($this->fieldInstances)) {
$this->fieldInstances = [];
foreach ($this->field_settings as $key => $field_info) {
$this->fieldInstances[$key] = \Drupal::getContainer()
->get('search_api.fields_helper')
->createField($this, $key, $field_info);
}
}
$fields = $this->fieldInstances;
if ($include_server_defined && $this
->hasValidServer()) {
$fields += $this
->getServerInstance()
->getBackendDefinedFields($this);
}
return $fields;
}
public function getField($field_id) {
$fields = $this
->getFields();
return $fields[$field_id] ?? NULL;
}
public function getFieldsByDatasource($datasource_id) {
$datasource_fields = [];
foreach ($this
->getFields() as $field_id => $field) {
if ($field
->getDatasourceId() === $datasource_id) {
$datasource_fields[$field_id] = $field;
}
}
return $datasource_fields;
}
public function getFulltextFields() {
$fulltext_fields = [];
foreach ($this
->getFields() as $key => $field) {
if (\Drupal::getContainer()
->get('search_api.data_type_helper')
->isTextType($field
->getType())) {
$fulltext_fields[] = $key;
}
}
return $fulltext_fields;
}
public function getFieldRenames() {
$renames = [];
foreach ($this
->getFields() as $field_id => $field) {
if ($field
->getOriginalFieldIdentifier() != $field_id) {
$renames[$field
->getOriginalFieldIdentifier()] = $field_id;
}
}
return $renames;
}
public function discardFieldChanges() {
$this->fieldInstances = NULL;
return $this;
}
public function getPropertyDefinitions($datasource_id) {
if (!isset($this->properties[$datasource_id])) {
if (isset($datasource_id)) {
$datasource = $this
->getDatasource($datasource_id);
$properties = $datasource
->getPropertyDefinitions();
}
else {
$datasource = NULL;
$properties = [];
}
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_ADD_PROPERTIES) as $processor) {
$properties += $processor
->getPropertyDefinitions($datasource);
}
$this->properties[$datasource_id] = $properties;
}
return $this->properties[$datasource_id];
}
public function loadItem($item_id) {
$items = $this
->loadItemsMultiple([
$item_id,
]);
return $items ? reset($items) : NULL;
}
public function loadItemsMultiple(array $item_ids) {
$items_by_datasource = [];
foreach ($item_ids as $item_id) {
list($datasource_id, $raw_id) = Utility::splitCombinedId($item_id);
$items_by_datasource[$datasource_id][$raw_id] = $item_id;
}
$items = [];
foreach ($items_by_datasource as $datasource_id => $raw_ids) {
try {
$datasource = $this
->getDatasource($datasource_id);
$datasource_items = $datasource
->loadMultiple(array_keys($raw_ids));
foreach ($datasource_items as $raw_id => $item) {
$id = $raw_ids[$raw_id];
$items[$id] = $item;
unset($items_by_datasource[$datasource_id][$raw_id]);
}
} catch (SearchApiException $e) {
$this
->logException($e);
unset($items_by_datasource[$datasource_id]);
}
}
$items_by_datasource = array_filter($items_by_datasource);
if ($items_by_datasource) {
$missing_ids = array_reduce(array_map('array_values', $items_by_datasource), 'array_merge', []);
$args['%index'] = $this
->label();
$args['@items'] = '"' . implode('", "', $missing_ids) . '"';
$this
->getLogger()
->warning('Could not load the following items on index %index: @items.', $args);
foreach ($items_by_datasource as $datasource_id => $raw_ids) {
$this
->trackItemsDeleted($datasource_id, array_keys($raw_ids));
}
}
return $items;
}
public function indexItems($limit = '-1', $datasource_id = NULL) {
if ($this
->hasValidTracker() && !$this
->isReadOnly()) {
$tracker = $this
->getTrackerInstance();
$next_set = $tracker
->getRemainingItems($limit, $datasource_id);
if (!$next_set) {
return 0;
}
$items = $this
->loadItemsMultiple($next_set);
if (!$items) {
return 0;
}
try {
return count($this
->indexSpecificItems($items));
} catch (SearchApiException $e) {
$variables['%index'] = $this
->label();
$this
->logException($e, '%type while trying to index items on index %index: @message in %function (line %line of %file)', $variables);
}
}
return 0;
}
public function indexSpecificItems(array $search_objects) {
if (!$search_objects || $this->read_only) {
return [];
}
if (!$this->status) {
$index_label = $this
->label();
throw new SearchApiException("Couldn't index values on index '{$index_label}' (index is disabled)");
}
$items = [];
foreach ($search_objects as $item_id => $object) {
$items[$item_id] = \Drupal::getContainer()
->get('search_api.fields_helper')
->createItemFromObject($this, $object, $item_id);
}
$rejected_ids = array_keys($items);
$rejected_ids = array_combine($rejected_ids, $rejected_ids);
$this
->alterIndexedItems($items);
$description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.indexing_items" event instead. See https://www.drupal.org/node/3059866';
\Drupal::moduleHandler()
->alterDeprecated($description, 'search_api_index_items', $this, $items);
$event = new IndexingItemsEvent($this, $items);
\Drupal::getContainer()
->get('event_dispatcher')
->dispatch(SearchApiEvents::INDEXING_ITEMS, $event);
$items = $event
->getItems();
foreach ($items as $item) {
$item
->getFields();
}
$this
->preprocessIndexItems($items);
foreach ($items as $item_id => $item) {
unset($rejected_ids[$item_id]);
}
if ($rejected_ids) {
$this
->getServerInstance()
->deleteItems($this, $rejected_ids);
}
$indexed_ids = [];
if ($items) {
$indexed_ids = $this
->getServerInstance()
->indexItems($this, $items);
}
$processed_ids = array_merge(array_values($rejected_ids), array_values($indexed_ids));
if ($processed_ids) {
if ($this
->hasValidTracker()) {
$this
->getTrackerInstance()
->trackItemsIndexed($processed_ids);
}
$this
->setHasReindexed(FALSE);
$description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.items_indexed" event instead. See https://www.drupal.org/node/3059866';
\Drupal::moduleHandler()
->invokeAllDeprecated($description, 'search_api_items_indexed', [
$this,
$processed_ids,
]);
$dispatcher = \Drupal::getContainer()
->get('event_dispatcher');
$dispatcher
->dispatch(SearchApiEvents::ITEMS_INDEXED, new ItemsIndexedEvent($this, $processed_ids));
Cache::invalidateTags([
'search_api_list:' . $this->id,
]);
}
if (function_exists('drush_backend_batch_process') && batch_get()) {
\Drupal::getContainer()
->get('entity.memory_cache')
->deleteAll();
}
return $processed_ids;
}
public function isBatchTracking() {
return (bool) $this->batchTracking;
}
public function startBatchTracking() {
$this->batchTracking++;
return $this;
}
public function stopBatchTracking() {
if (!$this->batchTracking) {
throw new SearchApiException('Trying to leave "batch tracking" mode on index "' . $this
->label() . '" which was not entered first.');
}
$this->batchTracking--;
return $this;
}
public function trackItemsInserted($datasource_id, array $ids) {
$this
->trackItemsInsertedOrUpdated($datasource_id, $ids, __FUNCTION__);
}
public function trackItemsUpdated($datasource_id, array $ids) {
$this
->trackItemsInsertedOrUpdated($datasource_id, $ids, __FUNCTION__);
}
protected function trackItemsInsertedOrUpdated($datasource_id, array $ids, $tracker_method) {
if ($this
->hasValidTracker() && $this
->status()) {
$item_ids = [];
foreach ($ids as $id) {
$item_ids[] = Utility::createCombinedId($datasource_id, $id);
}
$this
->getTrackerInstance()
->{$tracker_method}($item_ids);
if (!$this
->isReadOnly() && $this
->getOption('index_directly') && !$this->batchTracking) {
\Drupal::getContainer()
->get('search_api.post_request_indexing')
->registerIndexingOperation($this
->id(), $item_ids);
}
}
}
public function trackItemsDeleted($datasource_id, array $ids) {
if (!$this
->status()) {
return;
}
$item_ids = [];
foreach ($ids as $id) {
$item_ids[] = Utility::createCombinedId($datasource_id, $id);
}
if ($this
->hasValidTracker()) {
$this
->getTrackerInstance()
->trackItemsDeleted($item_ids);
}
if (!$this
->isReadOnly() && $this
->hasValidServer()) {
$this
->getServerInstance()
->deleteItems($this, $item_ids);
}
}
public function reindex() {
if ($this
->status() && !$this
->isReindexing()) {
$this
->setHasReindexed();
$this
->getTrackerInstance()
->trackAllItemsUpdated();
$description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.reindex_scheduled" event instead. See https://www.drupal.org/node/3059866';
\Drupal::moduleHandler()
->invokeAllDeprecated($description, 'search_api_index_reindex', [
$this,
FALSE,
]);
$dispatcher = \Drupal::getContainer()
->get('event_dispatcher');
$dispatcher
->dispatch(SearchApiEvents::REINDEX_SCHEDULED, new ReindexScheduledEvent($this, FALSE));
}
}
public function clear() {
if (!$this
->status()) {
return;
}
$invoke_hook = FALSE;
if (!$this
->isReindexing()) {
$invoke_hook = TRUE;
$this
->setHasReindexed();
$this
->getTrackerInstance()
->trackAllItemsUpdated();
}
if (!$this
->isReadOnly()) {
$invoke_hook = TRUE;
$this
->getServerInstance()
->deleteAllIndexItems($this);
}
if ($invoke_hook) {
$description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.reindex_scheduled" event instead. See https://www.drupal.org/node/3059866';
\Drupal::moduleHandler()
->invokeAllDeprecated($description, 'search_api_index_reindex', [
$this,
!$this
->isReadOnly(),
]);
$dispatcher = \Drupal::getContainer()
->get('event_dispatcher');
$dispatcher
->dispatch(SearchApiEvents::REINDEX_SCHEDULED, new ReindexScheduledEvent($this, !$this
->isReadOnly()));
}
}
public function rebuildTracker() {
if (!$this
->status()) {
return;
}
$index_task_manager = \Drupal::getContainer()
->get('search_api.index_task_manager');
$index_task_manager
->stopTracking($this);
$index_task_manager
->startTracking($this);
$this
->setHasReindexed();
$description = 'This hook is deprecated in search_api:8.x-1.14 and is removed from search_api:2.0.0. Please use the "search_api.reindex_scheduled" event instead. See https://www.drupal.org/node/3059866';
\Drupal::moduleHandler()
->invokeAllDeprecated($description, 'search_api_index_reindex', [
$this,
FALSE,
]);
$dispatcher = \Drupal::getContainer()
->get('event_dispatcher');
$dispatcher
->dispatch(SearchApiEvents::REINDEX_SCHEDULED, new ReindexScheduledEvent($this, FALSE));
$index_task_manager
->addItemsBatch($this);
}
public function isReindexing() {
$key = "search_api.index.{$this->id()}.has_reindexed";
return \Drupal::state()
->get($key, FALSE);
}
protected function setHasReindexed($has_reindexed = TRUE) {
if ($this
->isReindexing() !== $has_reindexed) {
$key = "search_api.index.{$this->id()}.has_reindexed";
\Drupal::state()
->set($key, $has_reindexed);
}
return $this;
}
public function query(array $options = []) {
if (!$this
->status()) {
throw new SearchApiException('Cannot search on a disabled index.');
}
return \Drupal::getContainer()
->get('search_api.query_helper')
->createQuery($this, $options);
}
public function postCreate(EntityStorageInterface $storage) {
parent::postCreate($storage);
$config = \Drupal::config('search_api.settings');
$this->options += [
'cron_limit' => $config
->get('default_cron_limit'),
'index_directly' => TRUE,
];
}
public function preSave(EntityStorageInterface $storage) {
if ($this
->isSyncing() || $this
->isInstallingFromExtension()) {
parent::preSave($storage);
return;
}
$overrides = Utility::getConfigOverrides($this);
if ($this
->status() && !isset($overrides['status'])) {
if (!array_key_exists('server', $overrides)) {
if (!$this
->isServerEnabled()) {
$this
->disable();
}
}
else {
$server_id = $overrides['server'];
$server = $server_id !== NULL ? Server::load($server_id) : NULL;
if (!$server || !$server
->status()) {
$this
->disable();
}
}
}
$config = \Drupal::config('search_api.settings');
$this->options += [
'cron_limit' => $config
->get('default_cron_limit'),
'index_directly' => TRUE,
];
$this->properties = [];
foreach ($this
->getFields() as $field_id => $field) {
$field
->setIndexedLocked(FALSE);
$field
->setTypeLocked(FALSE);
$field
->setHidden(FALSE);
$datasource_id = $field
->getDatasourceId();
$property = NULL;
if ($datasource_id === NULL || $this
->isValidDatasource($datasource_id)) {
$properties = $this
->getPropertyDefinitions($datasource_id);
$property = \Drupal::getContainer()
->get('search_api.fields_helper')
->retrieveNestedProperty($properties, $field
->getPropertyPath());
}
if (!$property) {
$this
->removeField($field_id);
}
}
foreach ($this
->getProcessors() as $processor_id => $processor) {
if (!$processor
->supportsIndex($this)) {
$this
->removeProcessor($processor_id);
}
}
$processor_overrides = !empty($overrides['processor_settings']) ? $overrides['processor_settings'] : [];
foreach ($this
->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE, $processor_overrides) as $processor) {
$processor
->preIndexSave();
}
$this
->writeChangesToSettings();
parent::preSave($storage);
}
protected function writeChangesToSettings() {
$fields = $this
->getFields();
$field_dependencies = $this
->getFieldDependencies();
$field_dependencies += array_fill_keys(array_keys($fields), []);
$this->field_settings = [];
foreach ($fields as $field_id => $field) {
$field
->setDependencies($field_dependencies[$field_id]);
$this->field_settings[$field_id] = $field
->getSettings();
}
$processors = $this
->getProcessors();
$this->processor_settings = [];
foreach ($processors as $processor_id => $processor) {
$this->processor_settings[$processor_id] = $processor
->getConfiguration();
}
$tracker = $this
->getTrackerInstance();
$tracker_id = $tracker
->getPluginId();
$this->tracker_settings = [
$tracker_id => $tracker
->getConfiguration(),
];
$this->datasource_settings = [];
foreach ($this
->getDatasources() as $plugin_id => $datasource) {
$this->datasource_settings[$plugin_id] = $datasource
->getConfiguration();
}
return $this;
}
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
if (!$update) {
$this
->setHasReindexed();
}
try {
$original = $update ? $this->original : static::create([
'status' => FALSE,
]);
$index_task_manager = \Drupal::getContainer()
->get('search_api.index_task_manager');
if ($this
->status() && $original
->status()) {
$this
->reactToServerSwitch($original);
$this
->reactToDatasourceSwitch($original);
$this
->reactToTrackerSwitch($original);
$this
->reactToProcessorChanges($original);
}
elseif (!$this
->status() && $original
->status()) {
if ($this
->hasValidTracker()) {
$index_task_manager
->stopTracking($original);
}
if ($original
->isServerEnabled()) {
$original
->getServerInstance()
->removeIndex($original);
}
}
elseif ($this
->status() && !$original
->status()) {
$this
->getServerInstance()
->addIndex($this);
if ($this
->hasValidTracker()) {
$index_task_manager
->startTracking($this);
}
}
if (!$index_task_manager
->isTrackingComplete($this)) {
$use_batch = \Drupal::state()
->get('search_api_use_tracking_batch', TRUE);
if (!$use_batch || Utility::isRunningInCli()) {
$index_task_manager
->addItemsAll($this);
}
elseif (!defined('MAINTENANCE_MODE') || !in_array(MAINTENANCE_MODE, [
'install',
'update',
])) {
$index_task_manager
->addItemsBatch($this);
}
}
if (\Drupal::moduleHandler()
->moduleExists('views')) {
Views::viewsData()
->clear();
Cache::invalidateTags([
'extension:views',
]);
\Drupal::cache('discovery')
->delete('views:wizard');
}
Cache::invalidateTags($this
->getCacheTags());
$this->properties = [];
} catch (SearchApiException $e) {
$this
->logException($e);
}
}
protected function reactToServerSwitch(IndexInterface $original) {
assert($this
->status() && $original
->status(), '::reactToServerSwitch should only be called when the index is enabled');
if ($this
->getServerId() != $original
->getServerId()) {
if ($original
->hasValidServer()) {
$original
->getServerInstance()
->removeIndex($this);
}
if ($this
->hasValidServer()) {
$this
->getServerInstance()
->addIndex($this);
}
$this
->reindex();
}
elseif ($this
->hasValidServer()) {
$this
->getServerInstance()
->updateIndex($this);
}
}
protected function reactToDatasourceSwitch(IndexInterface $original) {
assert($this
->status() && $original
->status(), '::reactToDatasourceSwitch should only be called when the index is enabled');
$new_datasource_ids = $this
->getDatasourceIds();
$original_datasource_ids = $original
->getDatasourceIds();
if ($new_datasource_ids != $original_datasource_ids) {
$added = array_diff($new_datasource_ids, $original_datasource_ids);
$removed = array_diff($original_datasource_ids, $new_datasource_ids);
$index_task_manager = \Drupal::getContainer()
->get('search_api.index_task_manager');
$index_task_manager
->stopTracking($this, $removed);
if ($this
->hasValidServer()) {
$server = $this
->getServerInstance();
foreach ($removed as $datasource_id) {
$server
->deleteAllIndexItems($this, $datasource_id);
}
}
$index_task_manager
->startTracking($this, $added);
}
}
protected function reactToTrackerSwitch(IndexInterface $original) {
assert($this
->status() && $original
->status(), '::reactToTrackerSwitch should only be called when the index is enabled');
if ($this
->getTrackerId() != $original
->getTrackerId()) {
$index_task_manager = \Drupal::getContainer()
->get('search_api.index_task_manager');
if ($original
->hasValidTracker()) {
$index_task_manager
->stopTracking($original);
}
if ($this
->hasValidTracker()) {
$index_task_manager
->startTracking($this);
}
}
}
protected function reactToProcessorChanges(IndexInterface $original) {
$old_processors = $original
->getProcessors();
$new_processors = $this
->getProcessors();
$requires_reindex = FALSE;
foreach ($new_processors as $key => $processor) {
if (!isset($old_processors[$key])) {
if ($processor
->requiresReindexing(NULL, $processor
->getConfiguration())) {
$requires_reindex = TRUE;
break;
}
}
}
if (!$requires_reindex) {
foreach ($old_processors as $key => $old_processor) {
$new_processor = $new_processors[$key] ?? NULL;
$old_config = $old_processor
->getConfiguration();
$new_config = $new_processor ? $new_processor
->getConfiguration() : NULL;
if (!$new_processor || $old_config != $new_config) {
if ($old_processor
->requiresReindexing($old_config, $new_config)) {
$requires_reindex = TRUE;
break;
}
}
}
}
if ($requires_reindex) {
$this
->reindex();
}
}
public static function preDelete(EntityStorageInterface $storage, array $entities) {
parent::preDelete($storage, $entities);
$index_task_manager = \Drupal::getContainer()
->get('search_api.index_task_manager');
foreach ($entities as $index) {
if ($index
->status()) {
$index_task_manager
->stopTracking($index);
if ($index
->hasValidServer()) {
$index
->getServerInstance()
->removeIndex($index);
}
}
}
}
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
if (\Drupal::moduleHandler()
->moduleExists('views')) {
Views::viewsData()
->clear();
Cache::invalidateTags([
'extension:views',
]);
\Drupal::cache('discovery')
->delete('views:wizard');
}
$temp_store = \Drupal::service('tempstore.shared')
->get('search_api_index');
foreach ($entities as $entity) {
try {
$temp_store
->delete($entity
->id());
} catch (TempStoreException $e) {
}
}
}
public function calculateDependencies() {
$dependencies = $this
->getDependencyData();
$this->dependencies = array_intersect_key($this->dependencies, [
'enforced' => TRUE,
]);
$this->dependencies += array_map('array_keys', $dependencies);
return $this;
}
protected function getDependencyData() {
$dependency_data = [];
$original_dependencies = $this->dependencies;
parent::calculateDependencies();
unset($this->dependencies['enforced']);
foreach ($this->dependencies as $dependency_type => $list) {
foreach ($list as $name) {
$dependency_data[$dependency_type][$name]['always']['index'][$this->id] = $this;
}
}
$this->dependencies = $original_dependencies;
$type_dependencies = [];
foreach ($this
->getFields() as $field_id => $field) {
foreach ($field
->getDependencies() as $dependency_type => $names) {
foreach ($names as $name) {
$dependency_data[$dependency_type][$name]['always']['fields'][$field_id] = $field;
}
}
$type = $field
->getType();
if (!isset($type_dependencies[$type])) {
$type_dependencies[$type] = [];
$data_type = $field
->getDataTypePlugin();
if ($data_type && !$data_type
->isDefault()) {
$definition = $data_type
->getPluginDefinition();
$type_dependencies[$type]['module'][] = $definition['provider'];
if (!empty($definition['config_dependencies'])) {
$type_dependencies[$type] = NestedArray::mergeDeep($type_dependencies[$type], $definition['config_dependencies']);
}
if ($data_type instanceof DependentPluginInterface) {
$type_dependencies[$type] = NestedArray::mergeDeep($type_dependencies[$type], $data_type
->calculateDependencies());
}
}
}
foreach ($type_dependencies[$type] as $dependency_type => $list) {
foreach ($list as $name) {
$dependency_data[$dependency_type][$name]['optional']['fields'][$field_id] = $field;
}
}
}
if ($this
->hasValidServer()) {
$name = $this
->getServerInstance()
->getConfigDependencyName();
$dependency_data['config'][$name]['optional']['index'][$this->id] = $this;
}
$plugins = $this
->getAllPlugins();
foreach ($plugins as $plugin_type => $type_plugins) {
foreach ($type_plugins as $plugin_id => $plugin) {
$definition = $plugin
->getPluginDefinition();
$dependency_data['module'][$definition['provider']]['always'][$plugin_type][$plugin_id] = $plugin;
if (isset($definition['config_dependencies'])) {
foreach ($definition['config_dependencies'] as $dependency_type => $list) {
foreach ($list as $name) {
$dependency_data[$dependency_type][$name]['always'][$plugin_type][$plugin_id] = $plugin;
}
}
}
foreach ($plugin
->calculateDependencies() as $dependency_type => $list) {
foreach ($list as $name) {
$dependency_data[$dependency_type][$name]['optional'][$plugin_type][$plugin_id] = $plugin;
}
}
}
}
return $dependency_data;
}
protected function getFieldDependencies() {
$field_dependencies = [];
foreach ($this
->getDatasources() as $datasource_id => $datasource) {
$fields = [];
foreach ($this
->getFieldsByDatasource($datasource_id) as $field_id => $field) {
$fields[$field_id] = $field
->getPropertyPath();
}
$field_dependencies += $datasource
->getFieldDependencies($fields);
}
return $field_dependencies;
}
public function onDependencyRemoval(array $dependencies) {
$changed = parent::onDependencyRemoval($dependencies);
$all_plugins = $this
->getAllPlugins();
$dependency_data = $this
->getDependencyData();
$dependencies = array_filter($dependencies);
$dependency_data = array_intersect_key($dependency_data, $dependencies);
$dependency_data += array_fill_keys(array_keys($dependencies), []);
$call_on_removal = [];
foreach ($dependencies as $dependency_type => $dependency_objects) {
if (in_array($dependency_type, [
'module',
'theme',
])) {
$dependency_objects = array_flip($dependency_objects);
}
$dependency_data[$dependency_type] = array_intersect_key($dependency_data[$dependency_type], $dependency_objects);
foreach ($dependency_data[$dependency_type] as $name => $dependency_sources) {
if (!empty($dependency_sources['always'])) {
foreach ($dependency_sources['always'] as $plugin_type => $plugins) {
if ($plugin_type == 'index') {
continue;
}
$changed = TRUE;
if ($plugin_type == 'fields') {
foreach ($plugins as $field_id => $field) {
if ($field
->isIndexedLocked()) {
$field
->setIndexedLocked(FALSE);
}
$this
->removeField($field_id);
}
}
else {
$all_plugins[$plugin_type] = array_diff_key($all_plugins[$plugin_type], $plugins);
}
}
}
if (!empty($dependency_sources['optional'])) {
$changed = TRUE;
foreach ($dependency_sources['optional'] as $plugin_type => $plugins) {
if ($plugin_type == 'index') {
$this
->setServer(NULL);
continue;
}
if ($plugin_type == 'fields') {
foreach ($plugins as $field) {
$field
->setType($field
->getDataTypePlugin()
->getFallbackType());
}
continue;
}
$plugins = array_intersect_key($plugins, $all_plugins[$plugin_type]);
foreach ($plugins as $plugin_id => $plugin) {
$call_on_removal[$plugin_type][$plugin_id][$dependency_type][$name] = $dependency_objects[$name];
}
}
}
}
}
$updated_config = [];
foreach ($call_on_removal as $plugin_type => $plugins) {
foreach ($plugins as $plugin_id => $plugin_dependencies) {
$removal_successful = $all_plugins[$plugin_type][$plugin_id]
->onDependencyRemoval($plugin_dependencies);
if ($removal_successful) {
$updated_config[$plugin_type][$plugin_id] = $all_plugins[$plugin_type][$plugin_id]
->getConfiguration();
}
else {
unset($all_plugins[$plugin_type][$plugin_id]);
}
}
}
$this->processor_settings = array_intersect_key($this->processor_settings, $all_plugins['processors']);
$this->processorInstances = array_intersect_key($this->processorInstances, $all_plugins['processors']);
$this->datasource_settings = array_intersect_key($this->datasource_settings, $all_plugins['datasources']);
$this->datasourceInstances = array_intersect_key($this->datasourceInstances, $all_plugins['datasources']);
if (empty($all_plugins['tracker'])) {
$default_tracker_id = \Drupal::config('search_api.settings')
->get('default_tracker');
$this->tracker_settings = [
$default_tracker_id => [],
];
$this->trackerInstance = NULL;
}
if (!$this->datasource_settings) {
return FALSE;
}
foreach ($updated_config as $plugin_type => $plugin_configs) {
foreach ($plugin_configs as $plugin_id => $plugin_config) {
switch ($plugin_type) {
case 'processors':
$this->processor_settings[$plugin_id] = $plugin_config;
break;
case 'datasources':
$this->datasource_settings[$plugin_id] = $plugin_config;
break;
case 'tracker':
$this->tracker_settings[$plugin_id] = $plugin_config;
break;
}
}
}
return $changed;
}
protected function getAllPlugins() {
$plugins = [];
if ($this
->hasValidTracker()) {
$plugins['tracker'][$this
->getTrackerId()] = $this
->getTrackerInstance();
}
$plugins['processors'] = $this
->getProcessors();
$plugins['datasources'] = $this
->getDatasources();
return $plugins;
}
public function __sleep() {
if (\Drupal::hasContainer()) {
$this
->writeChangesToSettings();
}
$properties = get_object_vars($this);
unset($properties['datasourceInstances']);
unset($properties['trackerInstance']);
unset($properties['serverInstance']);
unset($properties['processorInstances']);
unset($properties['fieldInstances']);
unset($properties['properties']);
return array_keys($properties);
}
}