2024-01-03 16:54:13 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* OAI-PMH 2.0 Data Provider
|
2024-01-22 15:06:07 +01:00
|
|
|
* Copyright (C) 2024 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
2024-01-03 16:54:13 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2024-01-22 15:06:07 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2024-01-03 16:54:13 +01:00
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2024-01-22 15:06:07 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2024-01-03 16:54:13 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace OCC\OaiPmh2;
|
|
|
|
|
|
|
|
use DOMDocument;
|
|
|
|
use DOMElement;
|
|
|
|
use DOMException;
|
|
|
|
use DOMNode;
|
|
|
|
use GuzzleHttp\Psr7\Uri;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An OAI-PMH XML response object.
|
|
|
|
*
|
|
|
|
* @author Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
|
2024-07-13 18:49:54 +02:00
|
|
|
* @package OAIPMH2
|
2024-01-03 16:54:13 +01:00
|
|
|
*/
|
2024-09-30 19:37:29 +02:00
|
|
|
final class Response
|
2024-01-03 16:54:13 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* This holds the DOMDocument of the OAI-PMH XML response.
|
|
|
|
*/
|
2024-09-30 19:37:29 +02:00
|
|
|
private DOMDocument $dom;
|
2024-01-03 16:54:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This holds the root node of the OAI-PMH XML response.
|
|
|
|
*/
|
2024-09-30 19:37:29 +02:00
|
|
|
private DOMElement $rootNode;
|
2024-01-07 21:36:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add XSL processing instructions to XML response document.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function addProcessingInstructions(): void
|
|
|
|
{
|
|
|
|
$uri = $this->serverRequest->getUri();
|
|
|
|
$basePath = $uri->getPath();
|
2024-09-30 19:37:29 +02:00
|
|
|
if (str_ends_with(haystack: $basePath, needle: 'index.php')) {
|
|
|
|
$basePath = pathinfo(path: $basePath, flags: PATHINFO_DIRNAME);
|
2024-01-07 21:36:37 +01:00
|
|
|
}
|
|
|
|
$stylesheet = Uri::composeComponents(
|
2024-09-30 19:37:29 +02:00
|
|
|
scheme: $uri->getScheme(),
|
|
|
|
authority: $uri->getAuthority(),
|
|
|
|
path: rtrim(string: $basePath, characters: '/') . '/resources/stylesheet.xsl',
|
|
|
|
query: null,
|
|
|
|
fragment: null
|
2024-01-07 21:36:37 +01:00
|
|
|
);
|
|
|
|
$xslt = $this->dom->createProcessingInstruction(
|
2024-09-30 19:37:29 +02:00
|
|
|
target: 'xml-stylesheet',
|
|
|
|
data: sprintf(
|
|
|
|
format: 'type="text/xsl" href="%s"',
|
|
|
|
values: $stylesheet
|
2024-01-07 21:36:37 +01:00
|
|
|
)
|
|
|
|
);
|
2024-09-30 19:37:29 +02:00
|
|
|
$this->dom->appendChild(node: $xslt);
|
2024-01-07 21:36:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and append request element.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function appendRequest(): void
|
|
|
|
{
|
|
|
|
$uri = $this->serverRequest->getUri();
|
|
|
|
$baseUrl = Uri::composeComponents(
|
2024-09-30 19:37:29 +02:00
|
|
|
scheme: $uri->getScheme(),
|
|
|
|
authority: $uri->getAuthority(),
|
|
|
|
path: $uri->getPath(),
|
|
|
|
query: null,
|
|
|
|
fragment: null
|
|
|
|
);
|
|
|
|
$request = $this->createElement(
|
|
|
|
localName: 'request',
|
|
|
|
value: $baseUrl,
|
|
|
|
appendToRoot: true
|
2024-01-07 21:36:37 +01:00
|
|
|
);
|
2024-07-13 21:37:20 +02:00
|
|
|
/** @var array<string, string> */
|
|
|
|
$params = $this->serverRequest->getAttributes();
|
|
|
|
foreach ($params as $param => $value) {
|
2024-01-07 21:36:37 +01:00
|
|
|
$request->setAttribute(
|
2024-09-30 19:37:29 +02:00
|
|
|
qualifiedName: $param,
|
|
|
|
value: htmlspecialchars(
|
|
|
|
string: $value,
|
|
|
|
flags: ENT_XML1 | ENT_COMPAT,
|
|
|
|
encoding: 'UTF-8'
|
|
|
|
)
|
2024-01-07 21:36:37 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and append response date element.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function appendResponseDate(): void
|
|
|
|
{
|
2024-09-30 19:37:29 +02:00
|
|
|
$this->createElement(
|
|
|
|
localName: 'responseDate',
|
|
|
|
value: gmdate(format: 'Y-m-d\TH:i:s\Z'),
|
|
|
|
appendToRoot: true
|
|
|
|
);
|
2024-01-07 21:36:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create and append root element.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function appendRootElement(): void
|
|
|
|
{
|
2024-09-30 19:37:29 +02:00
|
|
|
$this->rootNode = $this->dom->createElement(localName: 'OAI-PMH');
|
2024-01-07 21:36:37 +01:00
|
|
|
$this->rootNode->setAttribute(
|
2024-09-30 19:37:29 +02:00
|
|
|
qualifiedName: 'xmlns',
|
|
|
|
value: 'http://www.openarchives.org/OAI/2.0/'
|
2024-01-07 21:36:37 +01:00
|
|
|
);
|
|
|
|
$this->rootNode->setAttribute(
|
2024-09-30 19:37:29 +02:00
|
|
|
qualifiedName: 'xmlns:xsi',
|
|
|
|
value: 'http://www.w3.org/2001/XMLSchema-instance'
|
2024-01-07 21:36:37 +01:00
|
|
|
);
|
|
|
|
$this->rootNode->setAttribute(
|
2024-09-30 19:37:29 +02:00
|
|
|
qualifiedName: 'xsi:schemaLocation',
|
|
|
|
value: 'http://www.openarchives.org/OAI/2.0/ https://www.openarchives.org/OAI/2.0/OAI-PMH.xsd'
|
2024-01-07 21:36:37 +01:00
|
|
|
);
|
2024-09-30 19:37:29 +02:00
|
|
|
$this->dom->appendChild(node: $this->rootNode);
|
2024-01-07 21:36:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the DOM document.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function createDocument(): void
|
|
|
|
{
|
2024-09-30 19:37:29 +02:00
|
|
|
$this->dom = new DOMDocument(version: '1.0', encoding: 'UTF-8');
|
2024-01-07 21:36:37 +01:00
|
|
|
$this->dom->preserveWhiteSpace = false;
|
|
|
|
$this->addProcessingInstructions();
|
|
|
|
}
|
|
|
|
|
2024-01-03 16:54:13 +01:00
|
|
|
/**
|
|
|
|
* Create a new XML element.
|
|
|
|
*
|
|
|
|
* @param string $localName The local name for the element
|
|
|
|
* @param string $value The optional value for the element
|
|
|
|
* @param bool $appendToRoot Append the new element to the root node?
|
|
|
|
*
|
|
|
|
* @return DOMElement The newly created element
|
|
|
|
*/
|
|
|
|
public function createElement(string $localName, string $value = '', bool $appendToRoot = false): DOMElement
|
|
|
|
{
|
|
|
|
$node = $this->dom->createElement(
|
2024-09-30 19:37:29 +02:00
|
|
|
localName: $localName,
|
|
|
|
value: htmlspecialchars(
|
|
|
|
string: $value,
|
|
|
|
flags: ENT_XML1,
|
|
|
|
encoding: 'UTF-8'
|
|
|
|
)
|
2024-01-03 16:54:13 +01:00
|
|
|
);
|
|
|
|
if ($appendToRoot) {
|
2024-09-30 19:37:29 +02:00
|
|
|
$this->rootNode->appendChild(node: $node);
|
2024-01-03 16:54:13 +01:00
|
|
|
}
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Import XML data into response document.
|
|
|
|
*
|
|
|
|
* @param string $data The XML data
|
|
|
|
*
|
|
|
|
* @return DOMNode The imported XML node
|
|
|
|
*
|
2024-09-30 19:37:29 +02:00
|
|
|
* @throws DOMException if the data cannot be imported
|
2024-01-03 16:54:13 +01:00
|
|
|
*/
|
|
|
|
public function importData(string $data): DOMNode
|
|
|
|
{
|
2024-09-30 19:37:29 +02:00
|
|
|
$document = new DOMDocument(version: '1.0', encoding: 'UTF-8');
|
2024-01-03 16:54:13 +01:00
|
|
|
$document->preserveWhiteSpace = false;
|
2024-09-30 19:37:29 +02:00
|
|
|
if ($document->loadXML(source: $data) === true) {
|
2024-01-03 16:54:13 +01:00
|
|
|
/** @var DOMElement */
|
|
|
|
$rootNode = $document->documentElement;
|
2024-09-30 19:37:29 +02:00
|
|
|
$node = $this->dom->importNode(node: $rootNode, deep: true);
|
2024-01-03 16:54:13 +01:00
|
|
|
return $node;
|
|
|
|
} else {
|
|
|
|
throw new DOMException(
|
2024-09-30 19:37:29 +02:00
|
|
|
message: 'Could not import the XML data. Most likely it is not well-formed.',
|
|
|
|
code: 500
|
2024-01-03 16:54:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an OAI-PMH XML response.
|
|
|
|
*
|
|
|
|
* @param ServerRequestInterface $serverRequest The PSR-7 HTTP Server Request
|
|
|
|
*/
|
2024-09-30 19:37:29 +02:00
|
|
|
public function __construct(private ServerRequestInterface $serverRequest)
|
2024-01-03 16:54:13 +01:00
|
|
|
{
|
2024-01-07 21:36:37 +01:00
|
|
|
$this->createDocument();
|
|
|
|
$this->appendRootElement();
|
|
|
|
$this->appendResponseDate();
|
|
|
|
$this->appendRequest();
|
2024-01-03 16:54:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize the OAI-PMH XML response.
|
|
|
|
*
|
|
|
|
* @return string The XML output
|
|
|
|
*/
|
|
|
|
public function __toString(): string
|
|
|
|
{
|
|
|
|
$this->dom->formatOutput = true;
|
|
|
|
return (string) $this->dom->saveXML();
|
|
|
|
}
|
|
|
|
}
|