71 lines
3.1 KiB
PHP
71 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OaiSymfony\OAI;
|
|
|
|
use \DOMDocument;
|
|
|
|
use function Symfony\Component\String\b;
|
|
|
|
/**
|
|
* Creates the base XML document containing dynamically generated responses
|
|
* @todo different create functions depending on request type? e.g. GetRecord
|
|
*/
|
|
final class OaiBase
|
|
{
|
|
/**
|
|
* @param string $content The dynamically generated XML response
|
|
* @param string $nodeName The root node name to be imported
|
|
*/
|
|
public static function create(string $content, string $nodeName, string $metadataFormat = 'oai_dc'): string
|
|
{
|
|
$main = new DOMDocument(encoding: 'UTF-8');
|
|
$main->formatOutput = true;
|
|
$xslt = $main->createProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="/xslt/oai2.xsl"');
|
|
$main->appendChild($xslt);
|
|
|
|
$imported = new DOMDocument(encoding: 'UTF-8');
|
|
$imported->loadXML(b($content)->toUnicodeString()->toString(), LIBXML_NOWARNING);
|
|
$node = $imported->getElementsByTagName($nodeName)->item(0);
|
|
|
|
$root = $main->createElement('OAI-PMH');
|
|
$root->setAttribute('xmlns', 'http://www.openarchives.org/OAI/2.0/');
|
|
$root->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
|
|
$root->setAttribute('xsi:schemaLocation', 'http://www.openarchives.org/OAI/2.0/ http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd');
|
|
$responseDate = $main->createElement('responseDate', date("Y-m-d\TH:i:s\Z"));
|
|
$root->appendChild($responseDate);
|
|
$request = $main->createElement('request', 'https://www.yourdomain.com/oai');
|
|
|
|
if ($nodeName !== 'error') {
|
|
$request->setAttribute('verb', $nodeName);
|
|
// TODO dynamic metadata prefix!
|
|
$request->setAttribute('metadataPrefix', $metadataFormat);
|
|
}
|
|
|
|
$root->appendChild($request);
|
|
$main->appendChild($root);
|
|
$node = $main->importNode($node, true);
|
|
$root->appendChild($node);
|
|
|
|
// To avoid error when importing XML from RecordOaiDatacite... (why??)
|
|
if ($metadataFormat === 'oai_datacite') {
|
|
$oai_datacite = $main->getElementsByTagName('oai_datacite')->item(0);
|
|
$resource = $main->getElementsByTagName('resource')->item(0);
|
|
$xmlns = $main->createAttribute('xmlns');
|
|
$xsi = $main->createAttribute('xsi:schemaLocation');
|
|
$xmlnsRes = $main->createAttribute('xmlns');
|
|
$xsiRes = $main->createAttribute('xsi:schemaLocation');
|
|
$xsi->value = "http://schema.datacite.org/oai/oai-1.1/ http://schema.datacite.org/oai/oai-1.1/oai.xsd";
|
|
$xmlns->value = "http://schema.datacite.org/oai/oai-1.1/";
|
|
$oai_datacite->appendChild($xmlns);
|
|
$oai_datacite->appendChild($xsi);
|
|
$xmlnsRes->value = "http://datacite.org/schema/kernel-3";
|
|
$xsiRes->value = "http://datacite.org/schema/kernel-3 http://schema.datacite.org/meta/kernel-3/metadata.xsd";
|
|
$resource->appendChild($xmlnsRes);
|
|
$resource->appendChild($xsiRes);
|
|
}
|
|
|
|
return $main->saveXML();
|
|
}
|
|
} |