Commit 0c5022c5 authored by Penny Leach's avatar Penny Leach
Browse files

Leap2A support for files inside enclosures



As of 2010-07, <content src="" is no longer supported, and enclosure
links must be used instead.  This patch retains BC, both for enclosures
that point to other artefacts, and for files inside <content src.
Signed-off-by: default avatarPenny Leach <penny@mjollnir.org>
parent acf82951
......@@ -181,23 +181,14 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
if (!isset($blogpostentry->link)) {
continue;
}
$blogpost = null;
foreach ($blogpostentry->link as $blogpostlink) {
$blogpost = null;
if ($importer->curie_equals($blogpostlink['rel'], '', 'enclosure') && isset($blogpostlink['href'])) {
if (!$blogpost) {
$artefactids = $importer->get_artefactids_imported_by_entryid((string)$blogpostentry->id);
$blogpost = new ArtefactTypeBlogPost($artefactids[0]);
}
$importer->trace("Attaching file $blogpostlink[href] to blog post $blogpostentry->id", PluginImportLeap::LOG_LEVEL_VERBOSE);
$artefactids = $importer->get_artefactids_imported_by_entryid((string)$blogpostlink['href']);
if (isset($artefactids[0])) {
$blogpost->attach($artefactids[0]);
} else { // it may be just an attached file, with no Leap2A element in its own right ....
if ($id = self::attach_linked_file($blogpostentry, $blogpostlink, $importer)) {
$blogpost->attach($id);
$newartefactmapping[(string)$blogpostlink['href']][] = $id;
}
}
if (!$blogpost) {
$artefactids = $importer->get_artefactids_imported_by_entryid((string)$blogpostentry->id);
$blogpost = new ArtefactTypeBlogPost($artefactids[0]);
}
if ($id = $importer->create_attachment($entry, $blogpostlink, $blogpost)) {
$newartefactmapping[$link['href']][] = $id;
}
if ($blogpost) {
$blogpost->commit();
......@@ -215,15 +206,8 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
}
$blogpost = new ArtefactTypeBlogPost($blogpostids[0]);
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], '', 'enclosure') && isset($link['href'])) {
if (isset($artefactids[0])) {
$blogpost->attach($artefactids[0]);
} else {
if ($id = self::attach_linked_file($entry, $link, $importer)) {
$blogpost->attach($id);
$newartefactmapping[(string)$link['href']][] = $id;
}
}
if ($id = $importer->create_attachment($entry, $link, $blogpost)) {
$newartefactmapping[$link['href']][] = $id;
}
}
$blogpost->commit();
......
......@@ -194,7 +194,7 @@ class LeapImportComment extends LeapImportArtefactPlugin {
*/
public static function setup_relationships(SimpleXMLElement $entry, PluginImportLeap $importer) {
$comment = null;
$newartefacts = array(); // save any newly created extra ones (eg enclosures)
$referentid = self::get_referent_entryid($entry, $importer);
// Link artefact comments; view comments are done later
......@@ -206,21 +206,18 @@ class LeapImportComment extends LeapImportArtefactPlugin {
// Attachments
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], '', 'enclosure') && isset($link['href'])) {
if (!$comment) {
$comment = self::get_comment_instance($entry, $importer);
}
$importer->trace("Attaching file $link[href] to comment $entry->id", PluginImportLeap::LOG_LEVEL_VERBOSE);
$artefactids = $importer->get_artefactids_imported_by_entryid((string)$link['href']);
if (isset($artefactids[0])) {
$comment->attach($artefactids[0]);
}
if (!$comment) {
$comment = self::get_comment_instance($entry, $importer);
}
if ($id = $importer->create_attachment($entry, $link, $comment)) {
$newartefacts[] = $id;
}
}
if ($comment) {
$comment->commit();
}
return $newartefacts;
}
/**
......
......@@ -102,13 +102,30 @@ class LeapImportFile extends LeapImportArtefactPlugin {
* if it's of rdf:type rdf:resource. This may be more strict than necessary
* - possibly just having the content ouf of line should be enough.
*
* In the 2010-07 version of Leap2A, an entry is *also* a file, if it's in an enclosure link.
* However, since we have to support BC, those might actually be entries too, so we have to check
* to see if what's in there is actually something that exists as a key in the array of entry ids.
*
* @param SimpleXMLElement $entry The entry to check
* @param PluginImportLeap $importer The importer
* @return boolean Whether the entry is a file
*/
private static function is_file(SimpleXMLElement $entry, PluginImportLeap $importer) {
return PluginImportLeap::is_rdf_type($entry, $importer, 'resource')
&& isset($entry->content['src']);
if (PluginImportLeap::is_rdf_type($entry, $importer, 'resource')
&& isset($entry->content['src'])) {
return true;
}
else {
// go through all the links and look for enclsures
$filesfound = 0;
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], '', 'enclosure') && isset($link['href']) && !$importer->entry_exists((string)$link['href'])) {
$filesfound++;
}
}
return ($filesfound == 1);
}
return false;
}
/**
......@@ -216,7 +233,20 @@ class LeapImportFile extends LeapImportArtefactPlugin {
// This means that it could have UTF8 characters in it, and the PHP
// documentation doesn't sound hopeful that urldecode will work with
// UTF8 characters
$pathname = urldecode((string)$entry->content['src']);
$pathname = false;
if (isset($entry->content['src'])) {
$pathname = urldecode((string)$entry->content['src']);
} else {
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], '', 'enclosure') && isset($link['href']) && !$importer->entry_exists((string)$link['href'])) {
$pathname = urldecode((string)$link['href']);
}
}
}
if (!$pathname) {
$importer->trace("WARNING: couldn't find a file for $entry->id ");
return;
}
// TODO: might want to make it easier to get at the directory where the import files are
$dir = dirname($importer->get('filename'));
......@@ -245,7 +275,7 @@ class LeapImportFile extends LeapImportArtefactPlugin {
// This API sucks, but that's not my problem
if (!$id = ArtefactTypeFile::save_file($pathname, $data, $importer->get('usrobj'), true)) {
$importer->trace("WARNING: the file for entry $entry->id does not exist in the import (path={$entry->content['src']})");
$importer->trace("WARNING: the file for entry $entry->id does not exist in the import (path={$pathname})");
return;
}
......
......@@ -755,11 +755,11 @@ class LeapExportElement {
* this does not attach the file to the expot, you have to use the
* {@link add_attachment} method on the exporter object.
*
* @param string $filename the relative path of the file.
* @param string $filename the relative path of the file (NOT including the filesdir)
*/
public function add_enclosure_link($filename) {
$this->links[$filename] = (object)array(
'id' => $filename,
'id' => $this->exporter->get('filedir') . $filename,
'type' => 'enclosure',
'file' => true
);
......
......@@ -1200,6 +1200,65 @@ class PluginImportLeap extends PluginImport {
public function get_namespaces() {
return $this->namespaces;
}
/**
* helper function to create attachments between entries.
* The 2010-07 version of leap2a says that linked *entries* should use related relation,
* and directly linked files (attachments) should use enclosures.
* However, for BC we should support both.
* This function supports both and additionally creates the File artefacts for attachments, then links them.
*
* @param SimpleXMLElement $entry the entry we want to attach things *to*
* @param SimpleXMLElement $link the link to inspect
* @param ArtefactType $artefact the artefact that has been created from the entry.
*
* @return void|int the id of a *newly created* attached artefact
*/
public function create_attachment(SimpleXMLElement $entry, SimpleXMLElement $link, ArtefactType $artefact) {
if (($this->curie_equals($link['rel'], '', 'enclosure') || $this->curie_equals($link['rel'], '', 'related')) && isset($link['href'])) {
$this->trace("Attaching file $link[href] to comment $entry->id", PluginImportLeap::LOG_LEVEL_VERBOSE);
$artefactids = $this->get_artefactids_imported_by_entryid((string)$link['href']);
if (isset($artefactids[0])) {
$artefact->attach($artefactids[0]);
} else { // it may be just an attached file, with no Leap2A element in its own right ....
if ($id = $this->create_linked_file($entry, $link)) {
$artefact->attach($id);
$newartefactmapping[(string)$link['href']][] = $id;
return $id;
}
}
}
}
/**
* Attaches a file to a blogpost entry that was just linked directly, rather than having a Leap2a entry
* See http://wiki.leapspecs.org/2A/files
*
* @param SimpleXMLElement $entry
* @param SimpleXMLElement $link
*/
private function create_linked_file(SimpleXMLElement $entry, SimpleXMLElement $link) {
$this->trace($link);
$pathname = urldecode((string)$link['href']);
$dir = dirname($this->get('filename'));
$pathname = $dir . DIRECTORY_SEPARATOR . $pathname;
if (!file_exists($pathname)) {
return false;
}
// Note: this data is passed (eventually) to ArtefactType->__construct,
// which calls strtotime on the dates for us
require_once('file.php');
$data = (object)array(
'title' => (string)$entry->title . ' ' . get_string('attachment'),
'owner' => $this->get('usr'),
'filetype' => file_mime_type($pathname),
);
return ArtefactTypeFile::save_file($pathname, $data, $this->get('usrobj'), true);
}
public function entry_exists($entryid) {
return array_key_exists($entryid, $this->strategylisting);
}
}
......
......@@ -577,6 +577,7 @@ $string['artefactnotpublishable'] = 'Artefact %s is not publishable in view %s';
$string['belongingto'] = 'Belonging to';
$string['allusers'] = 'All users';
$string['attachment'] = 'Attachment';
// Upload manager
$string['quarantinedirname'] = 'quarantine';
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment