コード例 #1
0
 /**
  * Tests whether module dependencies are handled correctly.
  */
 public function testModuleDependency()
 {
     // Test with all types of plugins at once.
     $datasources['search_api_test_dependencies'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('search_api_test_dependencies', array('index' => $this->index));
     $datasources['entity:user'] = \Drupal::getContainer()->get('plugin.manager.search_api.datasource')->createInstance('entity:user', array('index' => $this->index));
     $this->index->setDatasources($datasources);
     $processor = \Drupal::getContainer()->get('plugin.manager.search_api.processor')->createInstance('search_api_test_dependencies');
     $this->index->addProcessor($processor);
     $tracker = \Drupal::getContainer()->get('plugin.manager.search_api.tracker')->createInstance('search_api_test_dependencies');
     $this->index->setTracker($tracker);
     $this->index->save();
     // Check the dependencies were calculated correctly.
     $dependencies = $this->index->getDependencies();
     $this->assertContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency correctly inserted');
     // When the index resets the tracker, it needs to know the ID of the default
     // tracker.
     \Drupal::configFactory()->getEditable('search_api.settings')->set('default_tracker', 'default')->save();
     // Disabling modules in Kernel tests normally doesn't trigger any kind of
     // reaction, just removes it from the list of modules (e.g., to avoid
     // calling of a hook). Therefore, we have to trigger that behavior
     // ourselves.
     \Drupal::getContainer()->get('config.manager')->uninstall('module', 'search_api_test_dependencies');
     // Reload the index and check it's still there.
     $this->reloadIndex();
     $this->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed');
     // Make sure the dependency has been removed.
     $dependencies = $this->index->getDependencies();
     $dependencies += array('module' => array());
     $this->assertNotContains('search_api_test_dependencies', $dependencies['module'], 'Module dependency removed from index');
     // Make sure all the plugins have been removed.
     $this->assertNotContains('search_api_test_dependencies', $this->index->getDatasources(), 'Datasource was removed');
     $this->assertArrayNotHasKey('search_api_test_dependencies', $this->index->getProcessors(), 'Processor was removed');
     $this->assertEquals('default', $this->index->getTrackerId(), 'Tracker was reset');
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $values = $form_state->getValues();
     $new_settings = array();
     // Store processor settings.
     // @todo Go through all available processors, enable/disable with method on
     //   processor plugin to allow reaction.
     /** @var \Drupal\search_api\Processor\ProcessorInterface $processor */
     $processors = $this->entity->getProcessors(FALSE);
     foreach ($processors as $processor_id => $processor) {
         if (empty($values['status'][$processor_id])) {
             continue;
         }
         $new_settings[$processor_id] = array('processor_id' => $processor_id, 'weights' => array(), 'settings' => array());
         $processor_values = $values['processors'][$processor_id];
         if (!empty($processor_values['weights'])) {
             $new_settings[$processor_id]['weights'] = $processor_values['weights'];
         }
         if (isset($form['settings'][$processor_id])) {
             $processor_form_state = new SubFormState($form_state, array('processors', $processor_id, 'settings'));
             $processor->submitConfigurationForm($form['settings'][$processor_id], $processor_form_state);
             $new_settings[$processor_id]['settings'] = $processor->getConfiguration();
         }
     }
     // Sort the processors so we won't have unnecessary changes.
     ksort($new_settings);
     if (!$this->entity->getOption('processors', array()) !== $new_settings) {
         $this->entity->setOption('processors', $new_settings);
         $this->entity->save();
         $this->entity->reindex();
         drupal_set_message($this->t('The indexing workflow was successfully edited. All content was scheduled for reindexing so the new settings can take effect.'));
     } else {
         drupal_set_message($this->t('No values were changed.'));
     }
 }
コード例 #3
0
ファイル: Index.php プロジェクト: curveagency/intranet
 /**
  * Reacts to changes in processor configuration.
  *
  * @param \Drupal\search_api\IndexInterface $original
  *   The previous version of the index.
  */
 protected function reactToProcessorChanges(IndexInterface $original)
 {
     $old_processors = $original->getProcessors();
     $new_processors = $this->getProcessors();
     // Only actually do something when the processor settings are changed.
     if ($old_processors != $new_processors) {
         $requires_reindex = FALSE;
         // Loop over all new settings and check if the processors were already set
         // in the original entity.
         foreach ($new_processors as $key => $processor) {
             // The processor is new, because it wasn't configured in the original
             // entity.
             if (!isset($old_processors[$key])) {
                 if ($processor->requiresReindexing(NULL, $processor->getConfiguration())) {
                     $requires_reindex = TRUE;
                     break;
                 }
             }
         }
         if (!$requires_reindex) {
             // Loop over all original settings and check if one of them has been
             // removed or changed.
             foreach ($old_processors as $key => $old_processor) {
                 $new_processor = isset($new_processors[$key]) ? $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();
         }
     }
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function getProcessors($only_enabled = TRUE)
 {
     return $this->entity->getProcessors($only_enabled);
 }
コード例 #5
0
ファイル: Database.php プロジェクト: nB-MDSO/mdso-d8blog
 /**
  * Converts a value between two search types.
  *
  * @param mixed $value
  *   The value to convert.
  * @param string $type
  *   The type to convert to. One of the keys from
  *   search_api_default_field_types().
  * @param string $original_type
  *   The value's original type.
  * @param \Drupal\search_api\IndexInterface $index
  *   The index for which this conversion takes place.
  *
  * @return mixed
  *   The converted value.
  *
  * @throws \Drupal\search_api\SearchApiException
  *   Thrown if $type is unknown.
  */
 protected function convert($value, $type, $original_type, IndexInterface $index)
 {
     if (!isset($value)) {
         // For text fields, we have to return an array even if the value is NULL.
         return Utility::isTextType($type, array('text', 'tokenized_text')) ? array() : NULL;
     }
     switch ($type) {
         case 'text':
             // For dates, splitting the timestamp makes no sense.
             if ($original_type == 'date') {
                 $value = $this->getDateFormatter()->format($value, 'custom', 'Y y F M n m j d l D');
             }
             $ret = array();
             foreach (preg_split('/[^\\p{L}\\p{N}]+/u', $value, -1, PREG_SPLIT_NO_EMPTY) as $v) {
                 if ($v) {
                     if (strlen($v) > 50) {
                         $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Database search servers currently cannot index such words correctly – the word was therefore trimmed to the allowed length. Ensure you are using a tokenizer preprocessor.', array('%word' => $v));
                         $v = Unicode::truncateBytes($v, 50);
                     }
                     $ret[] = array('value' => $v, 'score' => 1);
                 }
             }
             // This used to fall through the tokenized case
             return $ret;
         case 'tokenized_text':
             while (TRUE) {
                 foreach ($value as $i => $v) {
                     // Check for over-long tokens.
                     $score = $v['score'];
                     $v = $v['value'];
                     if (strlen($v) > 50) {
                         $words = preg_split('/[^\\p{L}\\p{N}]+/u', $v, -1, PREG_SPLIT_NO_EMPTY);
                         if (count($words) > 1 && max(array_map('strlen', $words)) <= 50) {
                             // Overlong token is due to bad tokenizing.
                             // Check for "Tokenizer" preprocessor on index.
                             if (empty($index->getProcessors()['tokenizer'])) {
                                 $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. It is recommended to enable the "Tokenizer" preprocessor for indexes using database servers. Otherwise, the backend class has to use its own, fixed tokenizing.');
                             } else {
                                 $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing, due to bad tokenizing. Please check your settings for the "Tokenizer" preprocessor to ensure that data is tokenized correctly.');
                             }
                         }
                         $tokens = array();
                         foreach ($words as $word) {
                             if (strlen($word) > 50) {
                                 $this->getLogger()->warning('An overlong word (more than 50 characters) was encountered while indexing: %word.<br />Database search servers currently cannot index such words correctly – the word was therefore trimmed to the allowed length.', array('%word' => $word));
                                 $word = Unicode::truncateBytes($word, 50);
                             }
                             $tokens[] = array('value' => $word, 'score' => $score);
                         }
                         array_splice($value, $i, 1, $tokens);
                         // Restart the loop looking through all the tokens.
                         continue 2;
                     }
                 }
                 break;
             }
             return $value;
         case 'string':
         case 'uri':
             // For non-dates, PHP can handle this well enough.
             if ($original_type == 'date') {
                 return date('c', $value);
             }
             if (strlen($value) > 255) {
                 $value = Unicode::truncateBytes($value, 255);
                 $this->getLogger()->warning('An overlong value (more than 255 characters) was encountered while indexing: %value.<br />Database search servers currently cannot index such values correctly – the value was therefore trimmed to the allowed length.', array('%value' => $value));
             }
             return $value;
         case 'integer':
         case 'duration':
         case 'decimal':
             return 0 + $value;
         case 'boolean':
             return $value ? 1 : 0;
         case 'date':
             if (is_numeric($value) || !$value) {
                 return 0 + $value;
             }
             return strtotime($value);
         default:
             throw new SearchApiException(new FormattableMarkup('Unknown field type @type. Database search module might be out of sync with Search API.', array('@type' => $type)));
     }
 }