Skip to content
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
jobs:
build:

runs-on: ubuntu-22.04
runs-on: ubuntu-24.04

strategy:
matrix:
Expand All @@ -18,7 +18,7 @@ jobs:
name: PHP ${{ matrix.php-versions }} Test

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5

- name: Setup PHP ${{ matrix.php-versions }}
uses: shivammathur/setup-php@v2
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"ext-dom": "*",
"ext-fileinfo": "*",
"ext-libxml": "*",
"ext-mbstring": "*",
"ext-zip": "*",
"opus4-repo/opus4-common": "dev-master as 4.8.1",
"opus4-repo/opus4-app-common": "dev-main",
Expand Down
7 changes: 7 additions & 0 deletions src/ArrayImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@

use Opus\Common\Document;

/**
* Imports documents from array.
*
* TODO What is the use case, besides an easy way to test import mechanisms.
* TODO Interface?
* TODO support multiple documents?
*/
class ArrayImport
{
/**
Expand Down
43 changes: 43 additions & 0 deletions src/ImportRuleConditionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS 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 2 of the Licence, or any later version.
* OPUS 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 OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2023, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace Opus\Import;

use Opus\Common\DocumentInterface;

interface ImportRuleConditionInterface
{
/**
* @param DocumentInterface $document
* @return bool
*/
public function applies($document);
}
36 changes: 36 additions & 0 deletions src/ImportRuleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS 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 2 of the Licence, or any later version.
* OPUS 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 OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2023, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace Opus\Import;

interface ImportRuleInterface
{
}
137 changes: 137 additions & 0 deletions src/ImportRules.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS 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 2 of the Licence, or any later version.
* OPUS 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 OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2023, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace Opus\Import;

use Opus\Common\ConfigTrait;
use Opus\Common\DocumentInterface;
use Zend_Config_Ini;

use function class_exists;
use function filter_var;
use function is_array;
use function is_readable;

use const FILTER_VALIDATE_BOOLEAN;

class ImportRules
{
use ConfigTrait;

public const IMPORT_RULE_CLASS_PREFIX = 'Opus\\Import\\Rules\\';

/** @var ImportRuleInterface[] */
private $rules = [];

/**
* Loads rules from configuration.
*/
public function init()
{
$config = $this->getConfig();

if (
! isset($config->sword->enableImportRules) ||
! filter_var($config->sword->enableImportRules, FILTER_VALIDATE_BOOLEAN)
) {
// TODO does this belong here? There should not be anything SWORD specific here!
return; // don't load any rules
}

$rulesConfig = null;

if (isset($config->import->rulesConfigFile)) {
$rulesConfigFile = $config->import->rulesConfigFile;
if (is_readable($rulesConfigFile)) {
$rulesConfig = new Zend_Config_Ini($rulesConfigFile);
$rulesConfig = $rulesConfig->toArray();
}
}

// Get rules from main configuration as fallback
if ($rulesConfig === null && isset($config->import->rules)) {
$rulesConfig = $config->import->rules->toArray();
}

if (is_array($rulesConfig)) {
foreach ($rulesConfig as $name => $options) {
$type = $options['type'];

$rule = $this->createRule($type, $options);

if ($rule !== null) {
$this->rules[] = $rule;
}
}
}
}

/**
* @return ImportRuleInterface[]
*/
public function getRules()
{
return $this->rules;
}

/**
* @param string $type
* @param array $options
* @return ImportRuleInterface|null
*/
public function createRule($type, $options)
{
if (class_exists($type)) {
$ruleClass = $type;
} else {
$ruleClass = self::IMPORT_RULE_CLASS_PREFIX . $type;
if (! class_exists($ruleClass)) {
// TODO throw exception
return null;
}
}

$rule = new $ruleClass();
$rule->setOptions($options);

return $rule;
}

/**
* @param DocumentInterface $document
*/
public function apply($document)
{
foreach ($this->getRules() as $rule) {
$rule->apply($document);
}
}
}
27 changes: 24 additions & 3 deletions src/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ class Importer
/** @var XmlDocument */
private $xmlDocument;

/** @var ImportRules */
private $importRules;

/** @var bool */
private $updateExistingDocuments = true;

Expand Down Expand Up @@ -319,6 +322,9 @@ public function run()
continue;
}

$importRules = $this->getImportRules();
$importRules->apply($doc);

try {
// TODO post "import" processing before storing!
$newDocId = $doc->store();
Expand Down Expand Up @@ -713,9 +719,11 @@ protected function handleKeywords($node, $doc)
{
foreach ($node->childNodes as $childNode) {
if ($childNode instanceof DOMElement) {
$s = Subject::new();
$s->setLanguage(trim($childNode->getAttribute('language')));
$s->setType($childNode->getAttribute('type'));
$s = Subject::new();
$language = $childNode->getAttribute('language');
$s->setLanguage($language ?: 'deu');
$type = $childNode->getAttribute('type');
$s->setType($type ?: 'uncontrolled');
$s->setValue(trim($childNode->textContent));
$doc->addSubject($s);
}
Expand Down Expand Up @@ -1128,6 +1136,19 @@ public function getDocument(): DocumentInterface
return $this->document;
}

/**
* @return ImportRules
*/
public function getImportRules()
{
if ($this->importRules === null) {
$this->importRules = new ImportRules();
$this->importRules->init();
}

return $this->importRules;
}

protected function setSingleDocImport(bool $singleDoc): self
{
$this->singleDocImport = $singleDoc;
Expand Down
80 changes: 80 additions & 0 deletions src/Rules/AbstractImportRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS 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 2 of the Licence, or any later version.
* OPUS 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 OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2023, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

namespace Opus\Import\Rules;

use Opus\Common\DocumentInterface;
use Opus\Import\ImportRuleConditionInterface;
use Opus\Import\ImportRuleInterface;
use Opus\Import\Rules\Conditions\AccountCondition;
use Opus\Import\Rules\Conditions\KeywordCondition;

/**
* TODO add base class for common code
*/
abstract class AbstractImportRule implements ImportRuleInterface
{
/** @var ImportRuleConditionInterface */
private $condition;

/**
* @param array $options
*/
public function setOptions($options)
{
if (isset($options['condition'])) {
$condition = $options['condition'];
// TODO support multiple conditions
if (isset($condition['account'])) {
$this->condition = new AccountCondition($condition);
}
if (isset($condition['keyword'])) {
$this->condition = new KeywordCondition($condition);
}
}
}

/**
* @return ImportRuleConditionInterface
*/
public function getCondition()
{
return $this->condition;
}

/**
* @param DocumentInterface $document
*/
public function apply($document)
{
// TODO condition check in base class?
}
}
Loading