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

Work on #451. #454

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
84 changes: 84 additions & 0 deletions src/metadatamanipulators/AddUuidToTemplated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
// src/metadatamanipulators/SimpleReplaceTemplated.php

namespace mik\metadatamanipulators;

use \Monolog\Logger;
use Ramsey\Uuid\Uuid;

/**
* AddUuidToTemplated - Adds a UUID to the output of the Templated metadata parser,
* similar to the AddUuidToMods.php metadata manipulator.
*
* Signature in .ini files is:
*
* metadatamanipulators[] = "AddUuidToTemplated|/tmp/twigtemplates/mytemplate.xml"
*
* The template must contain the variable 'UUID', e.g. <identifier>{{ UUID }}</identifier>.
*/
class AddUuidToTemplated extends MetadataManipulator
{
/**
* Create a new metadata manipulator instance.
*/
public function __construct($settings, $paramsArray, $record_key)
{
parent::__construct($settings, $paramsArray, $record_key);
$this->record_key = $record_key;

// Set up logger.
$this->pathToLog = $this->settings['LOGGING']['path_to_manipulator_log'];
$this->log = new \Monolog\Logger('config');
$this->logStreamHandler = new \Monolog\Handler\StreamHandler(
$this->pathToLog,
Logger::INFO
);
$this->log->pushHandler($this->logStreamHandler);

if (count($paramsArray) == 1) {
$this->templatePath = $paramsArray[0];
$this->templateDirectory = pathinfo($paramsArray[0], PATHINFO_DIRNAME);
$this->templateFilename = pathinfo($paramsArray[0], PATHINFO_BASENAME);
} else {
$this->log->addInfo("AddUuidToTemplated", array('Wrong parameter count' => count($paramsArray)));
}
}

/**
* General manipulate wrapper method.
*
* @param string $input An XML file to be manipulated.
*
* @return string
* Manipulated XML.
*/
public function manipulate($input)
{
if (file_exists($this->templatePath)) {
$uuid4 = Uuid::uuid4();
$uuid_string = $uuid4->toString();

$loader = new \Twig_Loader_Filesystem($this->templateDirectory);
$twig = new \Twig_Environment($loader);
$xml_from_template = $twig->render($this->templateFilename, array('UUID' => $uuid_string));

// Convert $input into DOM, add child from template, reserialize DOM as XML and return it.
$dom = new \DOMDocument;
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$dom->loadXML($input);
$frag = $dom->createDocumentFragment();
$frag->appendXML($xml_from_template);
$dom->documentElement->appendChild($frag);
$this->log->addInfo("AddUuidToTemplated", array('Record key' => $this->record_key,
'Template applied' => $this->templatePath,
'UUID added' => $uuid_string
));
return $dom->saveXML();
} else {
$this->log->addWarning("AddUuidToTemplated", array('Record key' => $this->record_key,
'Template not found' => $this->templatePath
));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<identifier type="uuid">{{ UUID }}</identifier>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace mik\metadataparsers\templated;

use mik\fetchers\Csv;
use mik\tests\MikTestBase;
use mik\writers\CsvSingleFile;

/**
* Class MetadataManipulatorAddUuidToTemplatedTest
* @package mik\metadataparsers\templated
* @coversDefaultClass \mik\metadataparsers\templated\Templated
* @group metadatamanipulators
*/
class MetadataManipulatorAddUuidToTemplatedTest extends MikTestBase
{

/**
* Log path.
* @var string
*/
private $path_to_input_validator_log;

/**
* Path to MODS schema
* @var string
*/
private $path_to_mods_schema;

/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->path_to_temp_dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "mik_templated_temp_dir";
$this->path_to_output_dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "mik_templated_output_dir";
$this->path_to_log = $this->path_to_temp_dir . DIRECTORY_SEPARATOR . "mik.log";
$this->path_to_mods_schema = realpath(
$this->asset_base_dir . DIRECTORY_SEPARATOR . '/../../extras/scripts/mods-3-5.xsd'
);
$this->path_to_manipulator_log = $this->path_to_temp_dir .
DIRECTORY_SEPARATOR . "mik_metadatamanipulator_test.log";
}

/**
* @covers ::metadata
* @covers ::populateTemplate
*/
public function testCreateMetadata()
{
$settings = array(
'FETCHER' => array(
'class' => 'Csv',
'input_file' => $this->asset_base_dir . '/csv/sample_metadata.csv',
'record_key' => 'ID',
'temp_directory' => $this->path_to_temp_dir,
),
'FILE_GETTER' => array(
'validate_input' => false,
'class' => 'CsvSingleFile',
'file_name_field' => 'File',
),
'LOGGING' => array(
'path_to_log' => $this->path_to_log,
'path_to_manipulator_log' => $this->path_to_manipulator_log,
),
'METADATA_PARSER' => array(
'template' => $this->asset_base_dir . '/templated_metadata_parser/templated_mods_twig.xml',
),
'MANIPULATORS' => array(
'metadatamanipulators' => array(
'AddUuidToTemplated|' . $this->asset_base_dir .
'/templated_metadata_parser/adduuidtotemplatedtest.xml'
),
),
);

$parser = new Templated($settings);
$mods = $parser->metadata('postcard_1');

$dom = new \DOMDocument;
$dom->loadXML($mods);

$this->assertTrue(
$dom->schemaValidate($this->path_to_mods_schema),
"MODS document generate by Templated metadata parser did not validate"
);
$identifier_element = '<identifier type="uuid">';
$this->assertContains($identifier_element, $mods, "AddUuidToTemplated metadata manipulator did not work");
}
}