Skip to content

Commit b883736

Browse files
authored
Bugfix: Remove invalid XML chars from strings before saving to OAI Record (#16)
* Remove invalid XML chars from strings before saving to OAI Record * Add allowed plugins to composer.json * Linting
1 parent 6e37f67 commit b883736

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,13 @@
2525
}
2626
},
2727
"minimum-stability": "dev",
28-
"prefer-stable": true
28+
"prefer-stable": true,
29+
"config": {
30+
"allow-plugins": {
31+
"composer/installers": true,
32+
"silverstripe/vendor-plugin": true,
33+
"silverstripe/recipe-plugin": true,
34+
"dealerdirect/phpcodesniffer-composer-installer": true
35+
}
36+
}
2937
}

src/Jobs/OaiRecordUpdateJob.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ protected function updateOaiRecord(DataObject $dataObject): void
9696
$errors[] = sprintf('Unsupported OAI field provided: %s', $oaiField);
9797
}
9898

99-
$oaiRecord->{$oaiField} = $dataObject->relField($dataObjectField);
99+
$oaiRecord->{$oaiField} = $this->sanitiseContent($dataObject->relField($dataObjectField));
100100
}
101101

102102
if ($errors) {
@@ -160,4 +160,18 @@ protected function getOaiRecord(): ?OaiRecord
160160
return $oaiRecord;
161161
}
162162

163+
/**
164+
* @param mixed $content
165+
* @return mixed
166+
*/
167+
protected function sanitiseContent($content)
168+
{
169+
if (!is_string($content)) {
170+
return $content;
171+
}
172+
173+
// Attempt to remove any illegal characters
174+
return preg_replace('/[^\x{0009}\x{000a}\x{000d}\x{0020}-\x{D7FF}\x{E000}-\x{FFFD}]+/u', ' ', $content);
175+
}
176+
163177
}

0 commit comments

Comments
 (0)