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

Catch up with fork from BeAPI #9

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Created by .ignore support plugin (hsz.mobi)
### Composer template
composer.phar
/vendor/
composer.lock

# Local dev
.lando.yml
2 changes: 1 addition & 1 deletion CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ This Code of Conduct applies within all community spaces, and also applies when

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [office@opencultureconsulting.com](mailto:office@opencultureconsulting.com). All complaints will be reviewed and investigated promptly and fairly.
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at [humans@beapi.fr](mailto:humans@beapi.fr). All complaints will be reviewed and investigated promptly and fairly.

All community leaders are obligated to respect the privacy and security of the reporter of any incident.

Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Please read the excellent [GitHub Open Source Guide](https://opensource.guide/how-to-contribute/) on *How to Contribute on Open Source*.

If you have any further questions just [open a new issue](https://github.com/opencultureconsulting/oai_pmh/issues/new) and I'll be happy to assist!
If you have any further questions just [open a new issue](https://github.com/BeAPI/oai_pmh/issues/new) and I'll be happy to assist!
106 changes: 106 additions & 0 deletions Classes/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
/**
* Simple OAI-PMH 2.0 Data Provider
* Copyright (C) 2005 Heinrich Stamerjohanns <stamer@uni-oldenburg.de>
* Copyright (C) 2011 Jianfeng Li <jianfeng.li@adelaide.edu.au>
* Copyright (C) 2013 Daniel Neis Araujo <danielneis@gmail.com>
* Copyright (C) 2017 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* Copyright (C) 2020 Amaury BALMER <amaury@beapi.fr>
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCC\OAI2;

class Config
{
private static $instance;

private $config;

/**
* @return Config
*/
public static function getInstance(): Config
{
if (self::$instance === null) {
self::$instance = new static();
}

return self::$instance;
}

private function __construct()
{
// Load configuration
if (is_file(ABSPATH . '/../Configuration/Main.php')) {
$config = include ABSPATH . '/../Configuration/Main.php';
} elseif (is_file(ABSPATH . '/Configuration/Main.php')) {
$config = include ABSPATH . '/Configuration/Main.php';
} else {
throw new \RuntimeException('Missing configuration file');
}

if (1 === $config) {
throw new \RuntimeException('Configuration file must contain a return array');
}

$this->config = $config;

if (true === $config['debug']) {
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
} else {
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);

libxml_use_internal_errors(true);
}
}

private function __clone()
{
}

/**
* @return array
*/
public function getConfig(): array
{
return $this->config;
}

/**
* @param string $key
* @return mixed
*
*/
public function getConfigValue(string $key)
{
return $this->config[$key] ?? null;
}

/**
* Test if a metadata format exist!
*
* @param string $prefix
* @return bool
*/
public function metadataPrefixExists(string $prefix): bool
{
return !empty($this->config['metadataPrefix'][$prefix]);
}
}
226 changes: 226 additions & 0 deletions Classes/Data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
<?php
/**
* Simple OAI-PMH 2.0 Data Provider
* Copyright (C) 2005 Heinrich Stamerjohanns <stamer@uni-oldenburg.de>
* Copyright (C) 2011 Jianfeng Li <jianfeng.li@adelaide.edu.au>
* Copyright (C) 2013 Daniel Neis Araujo <danielneis@gmail.com>
* Copyright (C) 2017 Sebastian Meyer <sebastian.meyer@opencultureconsulting.com>
* Copyright (C) 2020 Amaury BALMER <amaury@beapi.fr>
*
* 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCC\OAI2;

class Data
{
private static $instance;

private $records = [];
private $deleted = [];
private $timestamps = [];
private $sets = [];

private $earliest;

/**
* @return Data
*/
public static function getInstance(): Data
{
if (self::$instance === null) {
self::$instance = new static();
}

return self::$instance;
}

private function __construct()
{
$this->earliest = time();
}

private function __clone()
{
}

public function getDirectoryByMeta($prefix)
{
$config = Config::getInstance();

$directory = rtrim($config->getConfigValue('dataDirectory'), '/') . '/' . $prefix;

// Prepend script's path if dataDir is not an absolute path
if (strpos($directory, '/') !== 0) {
$directory = ABSPATH . $directory;
}

return realpath($directory);
}

public function populateSets()
{
$config = Config::getInstance();

foreach ($config->getConfigValue('metadataPrefix') as $metadataPrefix => $uris) {
$directory = $this->getDirectoryByMeta($metadataPrefix);
if (empty($directory)) {
continue;
}

$all_files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
$xml_files = new \RegexIterator(
$all_files,
'/' . preg_quote($config->getConfigValue('setDefinition'), '/') . '$/'
);

foreach ($xml_files as $fileInfo) {
// Build set name
$setName = str_replace($directory, '', $fileInfo->getPath());
$setName = trim($setName, '/');
$setName = str_replace('/', ':', $setName);

$this->sets[] = $setName;
}
}
}

public function resetRecords()
{
$this->records = array();
$this->deleted = array();
$this->timestamps = array();
$this->earliest = time();
}

public function populateRecords($set = '')
{
$this->resetRecords();

$config = Config::getInstance();

foreach ($config->getConfigValue('metadataPrefix') as $metadataPrefix => $uris) {
$directory = $this->getDirectoryByMeta($metadataPrefix);
if (empty($directory)) {
continue;
}

$all_files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory));
$xml_files = new \RegexIterator($all_files, '/\.xml$/');

foreach ($xml_files as $fileInfo) {
$filePath = $fileInfo->getPathname();
$fileName = $fileInfo->getBasename();

if (basename($filePath) === $config->getConfigValue('setDefinition')) {
continue;
}

// Translate path to setSpec URI identifier
$setName = str_replace($directory, '', $fileInfo->getPath());
$setName = trim($setName, '/');
$setName = str_replace('/', ':', $setName);

// Filter to a set
if (!empty($set) && $setName !== $set) {
continue;
}

// If a record exist on multiple folder, because multisets, keep last not empty file in case or deletion
if (isset($this->records[$metadataPrefix][$fileName]) && !filesize($filePath)) {
continue;
}

$this->records[$metadataPrefix][$fileName] = $filePath;
$this->deleted[$metadataPrefix][$fileName] = !filesize($filePath);
$this->timestamps[$metadataPrefix][$fileName] = filemtime($filePath);

if (filemtime($filePath) < $this->earliest) {
$this->earliest = filemtime($filePath);
}
}

if (isset($this->records[$metadataPrefix])) {
ksort($this->records[$metadataPrefix]);
reset($this->records[$metadataPrefix]);
asort($this->timestamps[$metadataPrefix]);
reset($this->timestamps[$metadataPrefix]);
}
}
}

/**
* @return array
*/
public function getRecords(): array
{
return $this->records;
}

/**
* @return array
*/
public function getDeleted(): array
{
return $this->deleted;
}

/**
* @return array
*/
public function getTimestamps(): array
{
return $this->timestamps;
}

/**
* @return int
*/
public function getEarliest(): int
{
return $this->earliest;
}

/**
* @return array
*/
public function getSets(): array
{
return $this->sets;
}

public function getSetFile(string $set)
{
$config = Config::getInstance();

$setDefinition = $config->getConfigValue('setDefinition');

// Translate setSpec URL identifier to path
$set = str_replace(':', '/', $set);

foreach ($config->getConfigValue('metadataPrefix') as $prefix => $uris) {
$directory = $this->getDirectoryByMeta($prefix);
if (empty($directory)) {
continue;
}

$file = $directory . "/$set/$setDefinition";
if (is_file($file)) {
return $file;
}
}

return false;
}
}
Loading