View source
<?php
namespace Drupal\search_api_solr\Entity;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Language\LanguageInterface;
use Drupal\search_api_solr\SolrBackendInterface;
use Drupal\search_api_solr\Utility\Utility as SearchApiSolrUtility;
use Drupal\search_api_solr\SolrFieldTypeInterface;
class SolrFieldType extends AbstractSolrEntity implements SolrFieldTypeInterface {
protected $field_type;
protected $spellcheck_field_type;
protected $collated_field_type;
protected $unstemmed_field_type;
protected $custom_code;
protected $field_type_language_code;
protected $domains;
public function getFieldType() {
return $this->field_type;
}
public function setFieldType(array $field_type) {
$this->field_type = $field_type;
return $this;
}
public function getName() : string {
return $this->field_type['name'];
}
public function getSpellcheckFieldType() {
return $this->spellcheck_field_type;
}
public function setSpellcheckFieldType(array $spellcheck_field_type) {
$this->spellcheck_field_type = $spellcheck_field_type;
return $this;
}
public function getCollatedFieldType() {
return $this->collated_field_type;
}
public function setCollatedFieldType(array $collated_field_type) {
$this->collated_field_type = $collated_field_type;
return $this;
}
public function getUnstemmedFieldType() {
return $this->unstemmed_field_type;
}
public function setUnstemmedFieldType(array $unstemmed_field_type) {
$this->unstemmed_field_type = $unstemmed_field_type;
return $this;
}
public function getCustomCode() {
return $this->custom_code;
}
public function getFieldTypeLanguageCode() {
return $this->field_type_language_code;
}
public function getDomains() {
return empty($this->domains) ? [
'generic',
] : $this->domains;
}
public function getOptions() {
return $this
->getDomains();
}
public static function getAvailableDomains() {
return parent::getAvailableOptions('domains', 'generic', 'search_api_solr.solr_field_type.');
}
public static function getAvailableCustomCodes() {
$custom_codes = [];
$config_factory = \Drupal::configFactory();
foreach ($config_factory
->listAll('search_api_solr.solr_field_type.') as $field_type_name) {
$config = $config_factory
->get($field_type_name);
if ($custom_code = $config
->get('custom_code')) {
$custom_codes[] = $custom_code;
}
}
return array_unique($custom_codes);
}
public function getFieldTypeAsJson(bool $pretty = FALSE) {
$field_type = $this->field_type;
unset($field_type['analyzers']);
foreach ($this->field_type['analyzers'] as $analyzer) {
$type = 'analyzer';
if (!empty($analyzer['type'])) {
if ('multiterm' === $analyzer['type']) {
$type = 'multiTermAnalyzer';
}
else {
$type = $analyzer['type'] . 'Analyzer';
}
unset($analyzer['type']);
}
$field_type[$type] = $analyzer;
}
return $pretty ? json_encode($field_type, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) : Json::encode($field_type);
}
public function setFieldTypeAsJson($field_type) {
$field_type = $this->field_type = Json::decode($field_type);
$analyzers = [
'index' => 'indexAnalyzer',
'query' => 'queryAnalyzer',
'multiterm' => 'multiTermAnalyzer',
'analyzer' => 'analyzer',
];
foreach ($analyzers as $type => $analyzer) {
if (!empty($field_type[$analyzer])) {
unset($this->field_type[$analyzer]);
if ($type != $analyzer) {
$field_type[$analyzer]['type'] = $type;
}
$this->field_type['analyzers'][] = $field_type[$analyzer];
}
}
return $this;
}
public function getSpellcheckFieldTypeAsJson(bool $pretty = FALSE) {
if ($this->spellcheck_field_type) {
return $pretty ? json_encode($this->spellcheck_field_type, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) : Json::encode($this->spellcheck_field_type);
}
return '';
}
public function setSpellcheckFieldTypeAsJson($spellcheck_field_type) {
$this->spellcheck_field_type = Json::decode($spellcheck_field_type);
return $this;
}
public function getCollatedFieldTypeAsJson(bool $pretty = FALSE) {
if ($this->collated_field_type) {
return $pretty ? json_encode($this->collated_field_type, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) : Json::encode($this->collated_field_type);
}
return '';
}
public function setCollatedFieldTypeAsJson($collated_field_type) {
$this->collated_field_type = Json::decode($collated_field_type);
return $this;
}
public function getUnstemmedFieldTypeAsJson(bool $pretty = FALSE) {
if ($this->unstemmed_field_type) {
return $pretty ? json_encode($this->unstemmed_field_type, JSON_PRETTY_PRINT | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT) : Json::encode($this->unstemmed_field_type);
}
return '';
}
public function setUnstemmedFieldTypeAsJson($unstemmed_field_type) {
$this->unstemmed_field_type = Json::decode($unstemmed_field_type);
return $this;
}
public function getAsXml(bool $add_comment = TRUE) : string {
return $this
->getSubFieldTypeAsXml($this->field_type);
}
public function getSpellcheckFieldTypeAsXml($add_comment = TRUE) {
return $this->spellcheck_field_type ? $this
->getSubFieldTypeAsXml($this->spellcheck_field_type, ' spellcheck') : '';
}
public function getCollatedFieldTypeAsXml($add_comment = TRUE) {
return $this->collated_field_type ? $this
->getSubFieldTypeAsXml($this->collated_field_type, ' collated') : '';
}
public function getUnstemmedFieldTypeAsXml($add_comment = TRUE) {
return $this->unstemmed_field_type ? $this
->getSubFieldTypeAsXml($this->unstemmed_field_type, ' unstemmed') : '';
}
protected function getSubFieldTypeAsXml(array $field_type, string $additional_label = '', bool $add_comment = TRUE) {
$formatted_xml_string = $this
->buildXmlFromArray('fieldType', $field_type);
$comment = '';
if ($add_comment) {
$comment = "<!--\n " . $this
->label() . $additional_label . "\n " . $this
->getMinimumSolrVersion() . "\n-->\n";
}
return $comment . $formatted_xml_string;
}
public function getSolrConfigs() {
return $this->solr_configs;
}
public function getDynamicFields(?int $solr_major_version = NULL) {
$dynamic_fields = [];
$prefixes = $this->custom_code ? [
'tc' . $this->custom_code,
'toc' . $this->custom_code,
'tuc' . $this->custom_code,
] : [
't',
'to',
'tu',
];
foreach ($prefixes as $prefix_without_cardinality) {
foreach ([
's',
'm',
] as $cardinality) {
$prefix = $prefix_without_cardinality . $cardinality;
$name = $prefix . SolrBackendInterface::SEARCH_API_SOLR_LANGUAGE_SEPARATOR . $this->field_type_language_code . '_';
$dynamic_fields[] = $dynamic_field = [
'name' => SearchApiSolrUtility::encodeSolrName($name) . '*',
'type' => strpos($prefix, 'tu') === 0 && !empty($this->unstemmed_field_type) ? $this->unstemmed_field_type['name'] : $this->field_type['name'],
'stored' => TRUE,
'indexed' => TRUE,
'multiValued' => 'm' === $cardinality,
'termVectors' => TRUE,
'omitNorms' => strpos($prefix, 'to') === 0,
];
if (LanguageInterface::LANGCODE_NOT_SPECIFIED === $this->field_type_language_code) {
$dynamic_field['name'] = SearchApiSolrUtility::encodeSolrName($prefix) . '_*';
$dynamic_fields[] = $dynamic_field;
}
}
}
if ($spellcheck_field = $this
->getSpellcheckField()) {
$dynamic_fields[] = $spellcheck_field;
if (LanguageInterface::LANGCODE_NOT_SPECIFIED === $this->field_type_language_code) {
$spellcheck_field['name'] = 'spellcheck_*';
$dynamic_fields[] = $spellcheck_field;
}
}
if ($collated_field = $this
->getCollatedField($solr_major_version)) {
$dynamic_fields[] = $collated_field;
if (LanguageInterface::LANGCODE_NOT_SPECIFIED === $this->field_type_language_code) {
$collated_field['name'] = 'sort_*';
$dynamic_fields[] = $collated_field;
}
}
return $dynamic_fields;
}
public function getStaticFields() {
return [];
}
protected function getSpellcheckField() {
$spellcheck_field = NULL;
if ($this->spellcheck_field_type) {
$spellcheck_field = [
'name' => 'spellcheck_' . $this->field_type_language_code . '*',
'type' => $this->spellcheck_field_type['name'],
'stored' => TRUE,
'indexed' => TRUE,
'multiValued' => TRUE,
'termVectors' => TRUE,
'omitNorms' => TRUE,
];
}
return $spellcheck_field;
}
protected function getCollatedField(?int $solr_major_version = NULL) {
$collated_field = NULL;
if ($this->collated_field_type) {
$collated_field = [
'name' => SearchApiSolrUtility::encodeSolrName('sort' . SolrBackendInterface::SEARCH_API_SOLR_LANGUAGE_SEPARATOR . $this->field_type_language_code) . '_*',
'type' => $this->collated_field_type['name'],
'stored' => FALSE,
'indexed' => version_compare($solr_major_version, '5', '<'),
];
if (version_compare($solr_major_version, '5', '>=')) {
$collated_field['docValues'] = TRUE;
}
}
return $collated_field;
}
public function getCopyFields() {
return [];
}
public function getFieldTypeName() {
return isset($this->field_type['name']) ? $this->field_type['name'] : '';
}
public function requiresManagedSchema() {
if (isset($this->field_type['analyzers'])) {
foreach ($this->field_type['analyzers'] as $analyzer) {
if (isset($analyzer['filters'])) {
foreach ($analyzer['filters'] as $filter) {
if (strpos($filter['class'], 'solr.Managed') === 0) {
return TRUE;
}
}
}
}
}
return FALSE;
}
}