Skip to content

Commit

Permalink
Setup OAI-PMH provider endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbaldessin committed Apr 22, 2020
1 parent f3b6635 commit 8700d1a
Show file tree
Hide file tree
Showing 5 changed files with 1,010 additions and 411 deletions.
20 changes: 20 additions & 0 deletions app/Http/Controllers/OaiPmhController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Http\Controllers;

use Psr\Http\Message\ServerRequestInterface;

use App\Repositories\Product;

class OaiPmhController extends Controller
{
public function index(ServerRequestInterface $request)
{
$repository = new Product();
// $provider = new \Picturae\OaiPmh\Provider($repository);
$provider = new \Picturae\OaiPmh\Provider($repository, $request);
$response = $provider->getResponse();

return $response;
}
}
223 changes: 223 additions & 0 deletions app/Repositories/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php

namespace App\Repositories;

use DateTime;
use OpenSkos2\OaiPmh\Concept as OaiConcept;
use Picturae\OaiPmh\Exception\IdDoesNotExistException;
use Picturae\OaiPmh\Implementation\MetadataFormatType as ImplementationMetadataFormatType;
use Picturae\OaiPmh\Implementation\RecordList as OaiRecordList;
use Picturae\OaiPmh\Implementation\Repository\Identity as ImplementationIdentity;
use Picturae\OaiPmh\Implementation\Set;
use Picturae\OaiPmh\Implementation\SetList;
use Picturae\OaiPmh\Interfaces\MetadataFormatType;
use Picturae\OaiPmh\Interfaces\Record;
use Picturae\OaiPmh\Interfaces\RecordList;
use Picturae\OaiPmh\Interfaces\Repository as OaiRepository;
use Picturae\OaiPmh\Interfaces\Repository\Identity;
use Picturae\OaiPmh\Interfaces\SetList as InterfaceSetList;

class Product implements OaiRepository
{
/**
* @return string the base URL of the repository
*/
public function getBaseUrl()
{
// return 'http://my-data-provider/oai-pmh';
return route('api.oai-pmh');
}

/**
* @return string
* the finest harvesting granularity supported by the repository. The legitimate values are
* YYYY-MM-DD and YYYY-MM-DDThh:mm:ssZ with meanings as defined in ISO8601.
*/
public function getGranularity()
{
return 'YYYY-MM-DD';
}

/**
* @return Identity
*/
public function identify()
{
return new ImplementationIdentity();
}

/**
* @return InterfaceSetList
*/
public function listSets()
{
$items = [];
$items[] = new Set('my:spec', 'Title of spec');
return new SetList($items);
}

/**
* @param string $token
* @return InterfaceSetList
*/
public function listSetsByToken($token)
{
$params = $this->decodeResumptionToken($token);
return $this->listSets();
}

/**
* @param string $metadataFormat
* @param string $identifier
* @return Record
*/
public function getRecord($metadataFormat, $identifier)
{
// Fetch record
$record = $this->getSomeRecord($identifier);

// Throw exception if it does not exists
if (!record) {
throw new IdDoesNotExistException('No matching identifier ' . $identifier, $exc->getCode(), $exc);
}

return new Record($record);
}

/**
* @param string $metadataFormat metadata format of the records to be fetch or null if only headers are fetched
* (listIdentifiers)
* @param DateTime $from
* @param DateTime $until
* @param string $set name of the set containing this record
* @return RecordList
*/
public function listRecords($metadataFormat = null, DateTime $from = null, DateTime $until = null, $set = null)
{
$items = [];
$items[] = new OaiConcept($concept);

// Show token only if more records exists then are shown
$token = $this->encodeResumptionToken($this->limit, $from, $until, $metadataFormat, $set);

return new OaiRecordList($items, $token);
}

/**
* @param string $token
* @return RecordList
*/
public function listRecordsByToken($token)
{
$params = $this->decodeResumptionToken($token);

$records = $this->GetRecords($params);

// Only show if there are more records available else $token = null;
$token = $this->encodeResumptionToken(
$params['offset'] + 100,
$params['from'],
$params['until'],
$params['metadataPrefix'],
$params['set']
);

return new OaiRecordList($items, $token);
}

/**
* @param string $identifier
* @return MetadataFormatType[]
*/
public function listMetadataFormats($identifier = null)
{
$formats = [];
$formats[] = new ImplementationMetadataFormatType(
'oai_dc',
'http://www.openarchives.org/OAI/2.0/oai_dc.xsd',
'http://www.openarchives.org/OAI/2.0/oai_dc/'
);

$formats[] = new ImplementationMetadataFormatType(
'oai_rdf',
'http://www.openarchives.org/OAI/2.0/rdf.xsd',
'http://www.w3.org/2004/02/skos/core#'
);

return $formats;
}

/**
* Decode resumption token
* possible properties are:
*
* ->offset
* ->metadataPrefix
* ->set
* ->from (timestamp)
* ->until (timestamp)
*
* @param string $token
* @return array
*/
private function decodeResumptionToken($token)
{
$params = (array) json_decode(base64_decode($token));

if (!empty($params['from'])) {
$params['from'] = new \DateTime('@' . $params['from']);
}

if (!empty($params['until'])) {
$params['until'] = new \DateTime('@' . $params['until']);
}

return $params;
}

/**
* Get resumption token
*
* @param int $offset
* @param DateTime $from
* @param DateTime $util
* @param string $metadataPrefix
* @param string $set
* @return string
*/
private function encodeResumptionToken(
$offset = 0,
DateTime $from = null,
DateTime $util = null,
$metadataPrefix = null,
$set = null
) {
$params = [];
$params['offset'] = $offset;
$params['metadataPrefix'] = $metadataPrefix;
$params['set'] = $set;
$params['from'] = null;
$params['until'] = null;

if ($from) {
$params['from'] = $from->getTimestamp();
}

if ($util) {
$params['until'] = $util->getTimestamp();
}

return base64_encode(json_encode($params));
}

/**
* Get earliest modified timestamp
*
* @return DateTime
*/
private function getEarliestDateStamp()
{
// Fetch earliest timestamp
return new DateTime();
}
}
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@
"league/flysystem": "^1.0",
"league/flysystem-memory": "^1.0",
"nothingworks/blade-svg": "^0.3",
"nyholm/psr7": "^1.2",
"orangehill/iseed": "^2.6",
"picturae/oai-pmh": "^0.5.20",
"seld/signal-handler": "^1.1",
"spatie/laravel-image-optimizer": "^1.3"
"spatie/laravel-image-optimizer": "^1.3",
"symfony/psr-http-message-bridge": "~1.0",
"zendframework/zend-diactoros": "^2.2"
},
"require-dev": {
"barryvdh/laravel-debugbar": "^3.1",
Expand Down
Loading

0 comments on commit 8700d1a

Please sign in to comment.