Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PHP warnings when indexing in solr #114

Merged
merged 2 commits into from
Apr 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 67 additions & 66 deletions src/Plugin/search_api/processor/EDTFYear.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,85 +150,86 @@ public function addFieldValues(ItemInterface $item) {
&& !$entity->get($field_name)->isEmpty()) {
$edtf = trim($entity->get($field_name)->value);
}
else {
continue;
}

if ($edtf) {
if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) {
if ($this->configuration['ignore_undated'] && $edtf == "XXXX") {
continue;
if ($edtf != "nan" && empty(EDTFUtils::validate($edtf))) {
if ($this->configuration['ignore_undated'] && $edtf == "XXXX") {
continue;
}
try {
$parser = EdtfFactory::newParser();
$years = [];
// Sets.
if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) {
$dates = $parser->parse($edtf)->getEdtfValue();
$years = array_map(function ($date) {
return $date->getYear();
}, $dates->getDates());
}
try {
$parser = EdtfFactory::newParser();
$years = [];
// Sets.
if (strpos($edtf, '[') !== FALSE || strpos($edtf, '{') !== FALSE) {
$dates = $parser->parse($edtf)->getEdtfValue();
$years = array_map(function ($date) {
return $date->getYear();
}, $dates->getDates());
else {
// Open start dates with `../`.
if (substr($edtf, 0, 3) === '../') {
if ($this->configuration['ignore_open_start']) {
$edtf = substr($edtf, 3);
}
else {
$edtf = str_replace('../', $this->configuration['open_start_year'] . '/', $edtf);
}
}
else {
// Open start dates with `../`.
if (substr($edtf, 0, 3) === '../') {
if ($this->configuration['ignore_open_start']) {
$edtf = substr($edtf, 3);
}
else {
$edtf = str_replace('../', $this->configuration['open_start_year'] . '/', $edtf);
}
// Open start dates with `/`.
if (substr($edtf, 0, 1) === '/') {
if ($this->configuration['ignore_open_start']) {
$edtf = substr($edtf, 1);
}
// Open start dates with `/`.
if (substr($edtf, 0, 1) === '/') {
if ($this->configuration['ignore_open_start']) {
$edtf = substr($edtf, 1);
}
else {
$edtf = str_replace('/', $this->configuration['open_start_year'] . '/', $edtf);
}
else {
$edtf = str_replace('/', $this->configuration['open_start_year'] . '/', $edtf);
}
// Open end dates with `/..`.
if (substr($edtf, -3) === '/..') {
if ($this->configuration['ignore_open_end']) {
$edtf = substr($edtf, 0, -3);
}
else {
$end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year'];
$edtf = str_replace('/..', '/' . $end_year, $edtf);
}
}
// Open end dates with `/..`.
if (substr($edtf, -3) === '/..') {
if ($this->configuration['ignore_open_end']) {
$edtf = substr($edtf, 0, -3);
}
// Open end dates with `/`.
if (substr($edtf, -1) === '/') {
if ($this->configuration['ignore_open_end']) {
$edtf = substr($edtf, 0, -1);
}
else {
$end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year'];
$edtf = str_replace('/', '/' . $end_year, $edtf);
}
else {
$end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year'];
$edtf = str_replace('/..', '/' . $end_year, $edtf);
}
$parsed = $parser->parse($edtf)->getEdtfValue();
$years = range(intval(date('Y', $parsed->getMin())), intval(date('Y', $parsed->getMax())));
}
foreach ($years as $year) {
if (is_numeric($year)) {
$fields = $item->getFields(FALSE);
$fields = $this->getFieldsHelper()
->filterForPropertyPath($fields, NULL, 'edtf_year');
foreach ($fields as $field) {
$field->addValue($year);
}
// Open end dates with `/`.
if (substr($edtf, -1) === '/') {
if ($this->configuration['ignore_open_end']) {
$edtf = substr($edtf, 0, -1);
}
else {
$end_year = (empty($this->configuration['open_end_year'])) ? date('Y') : $this->configuration['open_end_year'];
$edtf = str_replace('/', '/' . $end_year, $edtf);
}
}
$parsed = $parser->parse($edtf)->getEdtfValue();
$years = range(intval(date('Y', $parsed->getMin())), intval(date('Y', $parsed->getMax())));
}
catch (\Throwable $e) {
\Drupal::logger('controlled_access_terms')
->warning(t("Could not parse EDTF value '@edtf' for indexing @type/@id",
[
'@edtf' => $edtf,
'@type' => $entity->getEntityTypeId(),
'@id' => $entity->id(),
]));
foreach ($years as $year) {
if (is_numeric($year)) {
$fields = $item->getFields(FALSE);
$fields = $this->getFieldsHelper()
->filterForPropertyPath($fields, NULL, 'edtf_year');
foreach ($fields as $field) {
$field->addValue($year);
}
}
}
}
catch (\Throwable $e) {
\Drupal::logger('controlled_access_terms')
->warning(t("Could not parse EDTF value '@edtf' for indexing @type/@id",
[
'@edtf' => $edtf,
'@type' => $entity->getEntityTypeId(),
'@id' => $entity->id(),
]));
}
}
}
}
Expand Down
Loading