This repository has been archived on 2023-01-23. You can view files and clone it, but cannot push or open issues or pull requests.
importer-parsers-archive-2022/parsers/FDH-Konvolute.php

136 lines
5.9 KiB
PHP
Raw Permalink Normal View History

2023-01-23 15:21:35 +01:00
<?PHP
/**
* Parser for XML generated through CSVXML.
*
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
* @link https://imports.museum-digital.org/importer/parsers/csvxml.php
*/
declare(strict_types = 1);
/**
* Parse function.
*
* @param array<mixed> $version Instance to import into.
* @param integer $institution_id Institution to import to.
* @param non-empty-string $XMLFolder Folder of the XML files to import.
* @param string $dataFolder Data folder.
* @param integer $sammlung_id Collection ID. Optional.
* @param boolean $visibility Import objects to be directly visible?.
* @param boolean $insertOnly If set to true, only new objects are added,
* old are not updated.
*
* @return void
*/
function parseImportXML(array $version, int $institution_id, string $XMLFolder, string $dataFolder = "", int $sammlung_id = 0, bool $visibility = false, bool $insertOnly = false) {
if (!is_dir(MD_IMPORTER_CONF::$import_dir_xml . "{$XMLFolder}")) throw new MDFileDoesNotExist("The folder to import from ($XMLFolder) does not exist.");
if (empty($dataFolder)) {
throw new Exception("You need to select a data folder to run image-only imports");
}
else $importImages = true;
// Set up writers
$outputHandler = new MDOutputHandler;
$outputHandler->setVerbosity(2);
$objectWriter = new MDObjectWriter($version['mainDB'], $version['nodaDB'], $version['link'], $version['filepath'], $version['dataFolderLink']);
$objectWriter->disableUpdateBaseData = true;
$objectWriter->disableImportAdditionalData = true;
# $objectWriter->disableImportImagesResources = true;
$objectWriter->disableImportTags = true;
$objectWriter->disableImportLiterature = true;
$objectWriter->disableImportHyperlinks = true;
$objectWriter->disableImportSeries = true;
$objectWriter->disableImportCollections = true;
$objectWriter->disableImportObjectRecords = true;
$objectWriter->disableImportTranscriptions = true;
$objectWriter->disableImportMarkings = true;
$objectWriter->disableImportExhibitions = true;
$objectWriter->disableImportReception = true;
$i = 0;
$startAtCounter = 0;
foreach (MD_STD::scandir(MD_IMPORTER_CONF::$import_dir_xml . "{$XMLFolder}") as $xmlFile) {
if (pathinfo($xmlFile, PATHINFO_EXTENSION) !== "txt") continue;
++$i;
if ($i < $startAtCounter) {
continue;
}
$outputHandler->toLog("Starting to process file #{$i}", 2);
// File name is inventory number
$inventory_number = pathinfo($xmlFile, PATHINFO_FILENAME);
$all_images_raw = MD_STD::file_get_contents(MD_IMPORTER_CONF::$import_dir_xml . "{$XMLFolder}" . '/' . $xmlFile);
// Clean away spaces after newlines
while (strpos($all_images_raw, PHP_EOL . " ") !== false) {
$all_images_raw = str_replace(PHP_EOL . " ", PHP_EOL, $all_images_raw);
}
// Clean away tabs after newlines
while (strpos($all_images_raw, PHP_EOL . "\t") !== false) {
$all_images_raw = str_replace(PHP_EOL . "\t", PHP_EOL, $all_images_raw);
}
while (strpos($all_images_raw, "\u{feff}") !== false) {
$all_images_raw = str_replace("\u{feff}", "", $all_images_raw);
}
// All single images are separated by two newlines
$all_images = explode(PHP_EOL . PHP_EOL . '[', $all_images_raw);
$object = new MDObject($version['mainDB'], $version['nodaDB'], $version['language'], $institution_id, $inventory_number, "ignored_anyway", "ignored_anyway", "ignored_anyway", $outputHandler);
$images = [];
foreach ($all_images as $cur_img) {
$img_info = array_diff(explode(PHP_EOL, $cur_img), ['', ' ']);
$filename_base = trim($img_info[0], "[] \t[");
$filename_base = strtr($filename_base, ["[" => ""]);
if (file_exists(MD_IMPORTER_CONF::$import_dir_files . $dataFolder . "/" . $filename_base . '.jpg')) {
$filename = $filename_base . '.jpg';
}
else if (file_exists(MD_IMPORTER_CONF::$import_dir_files . $dataFolder . "/" . $filename_base . '.JPG')) {
$filename = $filename_base . '.JPG';
}
else {
echo MD_IMPORTER_CONF::$import_dir_files . $dataFolder . "/" . $filename_base . '.jpg';
throw new MDFileDoesNotExist("There is no corresponding file for " . $filename_base);
}
// Image title is the first line after the file name. Everything after is the description.
$image_title = $img_info[1];
unset($img_info[0], $img_info[1]);
$img_description = implode(PHP_EOL, $img_info);
$image = new MDImage($version['mainDB'], $image_title, MD_IMPORTER_CONF::$import_dir_files . $dataFolder . "/" . $filename);
$image->set_image_beschreibung($img_description);
/*
if (isset($objectData["image_rights" . $suffix])) $image->set_image_rights($objectData["image_rights" . $suffix]);
if (isset($objectData["image_owner" . $suffix])) $image->set_image_owner($objectData["image_owner" . $suffix]);
if (isset($objectData["image_creator" . $suffix])) $image->set_image_creator($objectData["image_creator" . $suffix]);
*/
$image->set_visible(true);
# if (isset($objectData["image_master_filename" . $suffix])) $image->set_image_master_filename($objectData["image_master_filename" . $suffix]);
$images[] = $image;
usleep(50);
}
$images[0]->set_main_image(true);
foreach ($images as $image) $object->appendImage($image);
$newObjectID = $objectWriter->writeObject($object, true, $insertOnly, $outputHandler);
usleep(IMPORTER_DELAY_PER_OBJECT);
}
}