Created
January 9, 2016 17:37
-
-
Save mjordan/9f5813dd99281ddb2ff7 to your computer and use it in GitHub Desktop.
Sample MIK metadata manipulator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace mik\metadatamanipulators; | |
use \Monolog\Logger; | |
/** | |
* MJTest - Example metadata manipulator. | |
* | |
* The mappings file must contain a row that adds the following | |
* element to your MODS: | |
* nullN,<extension><foo></foo></extension>. | |
* | |
* If the value of the current object's Caroonist metadata property | |
* is 'Krieger, Bob', add the element. | |
* | |
* This metadata manipulator takes no configuration parameters. | |
*/ | |
class MJTest extends MetadataManipulator | |
{ | |
/** | |
* Create a new metadata manipulator Instance. | |
*/ | |
public function __construct($settings = null, $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); | |
} | |
/** | |
* General manipulate wrapper method. | |
* | |
* @param string $input The XML fragment to be manipulated. We are only | |
* interested in the <extension><foo> fragment added in the | |
* MIK mappings file. | |
* | |
* @return string | |
* One of the manipulated XML fragment, the original input XML if the | |
* input is not the fragment we are interested in, or an empty string | |
* if we don't want to add the fragment to our MODS.xml. | |
*/ | |
public function manipulate($input) | |
{ | |
$dom = new \DomDocument(); | |
$dom->loadxml($input, LIBXML_NSCLEAN); | |
// Test to see if the current fragment is <extension><foo>. | |
$xpath = new \DOMXPath($dom); | |
$foos = $xpath->query("//extension/foo"); | |
if ($foos->length === 1) { | |
$foo = $foos->item(0); | |
// Get the raw metadata (in the CSV toolchain, it will be a serialized object) so | |
// we can make decisions based on any value in it. | |
$raw_metadata_cache_path = $this->settings['FETCHER']['temp_directory'] . DIRECTORY_SEPARATOR . | |
$this->record_key . '.metadata'; | |
$raw_metadata_cache = file_get_contents($raw_metadata_cache_path); | |
$metadata = unserialize($raw_metadata_cache); | |
// In this example, we want to know if this is a Krieger cartoon. | |
if ($metadata->Cartoonist == 'Krieger, Bob') { | |
// If it is, add our snippet. | |
$foo->nodeValue = "This cartoon is by Bob Krieger."; | |
return $dom->saveXML($dom->documentElement); | |
} | |
else { | |
return ''; | |
} | |
} | |
else { | |
// If current fragment is not <extension><foo>, just return it. | |
return $input; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment