These are the 2 functions I altered in QubitDigitalObject:
public function importFromURI($uri, $options = array())
{
include_once sfConfig::get('sf_root_dir').'/lib/helper/NLWQubitHelper.php';
$damsUri = getDerivativesPath($uri);
$filename = $this->getFilenameFromUri($damsUri);
// Set general properties that don't require downloading the asset
$this->usageId = QubitTerm::EXTERNAL_URI_ID;
$this->name = $filename;
$this->path = $uri;
$this->setMimeAndMediaType();
// If not creating derivatives right now, don't download the resource
if (!$this->createDerivatives)
{
return;
}
// Download the remote resource bitstream
$contents = $this->downloadExternalObject($damsUri, $options);
$this->saveAndAttachFileContent($filename, $contents);
}
public function getLocalPath()
{
include_once sfConfig::get('sf_root_dir').'/lib/helper/NLWQubitHelper.php';
$myPath = $this->localPath;
$myUsage = $this->usageId;
$llgcId = $this->getFilenameFromUri($this->path);
if (null === $this->localPath && QubitTerm::EXTERNAL_URI_ID == $this->usageId)
{
$uri = getDerivativesPath($this->path);
$filename = $this->getFilenameFromUri($uri);;
$contents = $this->downloadExternalObject($uri);
$this->localPath = Qubit::saveTemporaryFile($filename, $contents);
}
if (null === $this->localPath && QubitTerm::EXTERNAL_FILE_ID == $this->usageId)
{
if (false === $contents = $this->file_get_contents_if_not_empty($this->path))
{
throw new sfException(sprintf('Error reading file or file is empty.', $filepath));
}
$this->localPath = Qubit::saveTemporaryFile($filename, $contents);
}
return $this->localPath;
}
This is the helper method. which is probably quite specific to us but might help other Fedora users:
<?php
function getFilenameFromHandle($uri)
{
$uriComponents = parse_url($uri);
$filename = basename($uriComponents['path']);
if (1 > strlen($filename))
{
writelog("ERROR", "Couldn't parse filename from {$uri}");
}
return $filename;
}
function makeRequest($ch, $expectCode) {
$success = false;
$result = false;
$tryCount = 1;
while ($success != true && $tryCount <= 10) {
if ($tryCount > 1) {
$preinfo = curl_getinfo($ch);
}
$tryCount++;
$result = curl_exec($ch);
$info = curl_getinfo($ch);
if ($result) {
$success = true;
if ($info['http_code'] != $expectCode) {
$success = false;
writelog("ERROR", "HTTP Request returned {$info['http_code']} but expected $expectCode for URL: {$info['url']}")
sleep($tryCount * 2);
}
} else {
if ($tryCount < 10) {
sleep($tryCount * 2);
writelog("ERROR" ,"FAILED HTTP Request $tryCount times, retrying.");
} else {
writelog("FATAL" ,"FAILED HTTP Request Max retries reached. Failing permanently.");
$result = null;
}
}
}
return ["result" => $result, "info" => $info];
}
function file_get_contents_retry($url) {
$proxy = getProxy();
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
return makeRequest($ch, 200);
}
function testDamsPath($url){
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $httpcode;
}
function getProxy() {
return array(
'http' => array(
'proxy' => '**********',
)
);
}
function getFileUrl($documentID, $username, $password, $hostUrl, $datastream) {
return 'http://' . $username . ':' . $password . '@' . $hostUrl . '/fedora/objects/llgc-id:' . $documentID . '/datastreams/' . $datastream . '/content';
}
function getMetsFileContents($documentID, $username, $password, $hostUrl) {
$opts = getProxy();
stream_context_set_default($opts);
$context = stream_context_create($opts);
libxml_set_streams_context($context);
$metsLocation = getFileUrl($documentID, $username, $password, $hostUrl, "METS");
$xmlFile = file_get_contents_retry($metsLocation);
if ($xmlFile["info"]["http_code"] != 200) {
$arr = array('error' => 'No document found for ID');
http_response_code(404);
exit;
}
return $xmlFile["result"];
}
function getChildHandle($xpath) {
$file_elements = $xpath->evaluate("/METS:mets/METS:structMap/METS:div/METS:div/METS:mptr");
if ($file_elements->length == 0) {
$arr = array('error' => 'No child handles found');
http_response_code(404);
echo json_encode($arr);
exit;
}
$handles = [];
foreach($file_elements as $file_element) {
$handles[] = $file_element->getAttribute("xlink:href");
}
return $handles[0];
}
function getMetsXPath($metsContents) {
$xml_doc = new DOMDocument();
$xml_doc->loadXML($metsContents);
$xpath = new DOMXPath($xml_doc);
$xpath->registerNamespace("METS", "
http://www.loc.gov/METS/");
$xpath->registerNamespace("xlink", "
http://www.w3.org/1999/xlink");
$xpath->registerNamespace("premis", "info:lc/xmlns/premis-v2");
return $xpath;
}
function getDamsPath($llgc_id){
$dams1st = "
http://dams.llgc.org.uk/iiif/2.0/image/";
$dams2nd = "/full/300,/0/default.jpg";
$damsPath = $dams1st.$llgc_id.$dams2nd;
return $damsPath;
}
function getConfig() {
$config = (require '/var/www/html/config/nlw_config.php');
$username = $config["fedoraUsername"];
$password = $config["fedoraPassword"];
$hostUrl = $config["fedoraHostUrl"];
return ["username" => $username, "password" => $password,
"hostUrl" => $hostUrl];
}
function getDerivativesPath($handlePath){
$llgc_id = getFilenameFromHandle($handlePath);
$damsPath = getDamsPath($llgc_id);
$response = testDamsPath($damsPath);
if ($response == 200) {
return $damsPath;
}
else{
//find the 1st child of this object
$config = getConfig();
$metsFileContents = getMetsFileContents($llgc_id, $config["username"], $config["password"], $config["hostUrl"]);
$xpath = getMetsXPath($metsFileContents);
$childHandle = getChildHandle($xpath);
$newLlgcId = substr(explode('
http://hdl.handle.net/10107/',$childHandle)[1], 0, -2);
$damsPath = getDamsPath($newLlgcId);
return $damsPath;
}
}
?>