Commit 2b4f0660 authored by Adrian Schlegel's avatar Adrian Schlegel
Browse files

adapted the LEAP2A import to handle both LEAP2A versions (2009-03 and 2010-07)


Signed-off-by: default avatarAdrian Schlegel <adrian@liip.ch>
parent a91c1c82
......@@ -78,7 +78,7 @@ class LeapImportBlog extends LeapImportArtefactPlugin {
// Get entries that this blog/selection feels are a part of it
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'has_part') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'has_part') && isset($link['href'])) {
$otherrequiredentries[] = (string)$link['href'];
}
}
......
......@@ -90,7 +90,7 @@ class LeapImportComment extends LeapImportArtefactPlugin {
// Check that the entry 'reflects_on' something
$otherentries = array();
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'reflects_on') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'reflects_on') && isset($link['href'])) {
$otherentries[] = (string)$link['href'];
}
}
......@@ -172,7 +172,7 @@ class LeapImportComment extends LeapImportArtefactPlugin {
*/
public static function get_referent_entryid(SimpleXMLElement $entry, PluginImportLeap $importer) {
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'reflects_on') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'reflects_on') && isset($link['href'])) {
return (string)$link['href'];
}
}
......
......@@ -141,7 +141,7 @@ class LeapImportFile extends LeapImportArtefactPlugin {
*/
private static function has_parent_folder(SimpleXMLElement $entry, PluginImportLeap $importer) {
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'is_part_of') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'is_part_of') && isset($link['href'])) {
$potentialfolder = $importer->get_entry_by_id((string)$link['href']);
if ($potentialfolder && self::is_folder($potentialfolder, $importer)) {
return true;
......@@ -169,7 +169,7 @@ class LeapImportFile extends LeapImportArtefactPlugin {
// Get entries that this folder feels are a part of it
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'has_part') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'has_part') && isset($link['href'])) {
$child = $importer->get_entry_by_id((string)$link['href']);
if ($child) {
if (self::is_file($child, $importer) || self::is_folder($child, $importer)) {
......
......@@ -227,6 +227,8 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
* to the author, if there is one
*/
public static function import_author_data(PluginImportLeap $importer, $persondataid) {
$namespaces = $importer->get_namespaces();
$ns = $namespaces[$importer->get_leap2a_namespace()];
if ($persondataid) {
// Grab all the leap:persondata elements and import them
$person = $importer->get_entry_by_id($persondataid);
......@@ -239,9 +241,9 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
}
// Most of the rest of the profile data comes from leap:persondata elements
$persondata = $person->xpath('leap:persondata');
$persondata = $person->xpath($ns.':persondata');
foreach ($persondata as $item) {
$leapattributes = PluginImportLeap::get_attributes($item, PluginImportLeap::NS_LEAP);
$leapattributes = PluginImportLeap::get_attributes($item, $importer->get_leap2a_namespace());
if (isset($leapattributes['field'])) {
self::import_persondata($importer, $item, $leapattributes);
......@@ -259,8 +261,8 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
self::import_namedata($importer, $persondata);
// People can have address info associated with them
$addressdata = $person->xpath('leap:spatial');
if (count($addressdata) == 1) {
$addressdata = $person->xpath($ns.':spatial');
if (is_array($addressdata) && count($addressdata) == 1) {
self::import_addressdata($importer, $addressdata[0]);
}
......@@ -456,7 +458,9 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
private static function import_addressdata(PluginImportLeap $importer, SimpleXMLElement $addressdata) {
// TODO: this xpath doesn't respect the namespace prefix - we should
// look it up from $importer->namespaces[NS_LEAP]
$addresslines = $addressdata->xpath('leap:addressline');
$namespaces = $importer->get_namespaces();
$ns = $namespaces[$importer->get_leap2a_namespace()];
$addresslines = $addressdata->xpath($ns.':addressline');
// We look for 'town' and 'city' deliberately, Mahara has
// separate fields for those. The rest get thrown in the
......@@ -483,11 +487,11 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
}
// Now deal with country
$country = $addressdata->xpath('leap:country');
$country = $addressdata->xpath($ns.':country');
if (count($country) == 1) {
$country = $country[0];
$leapattributes = PluginImportLeap::get_attributes($country, PluginImportLeap::NS_LEAP);
$leapattributes = PluginImportLeap::get_attributes($country, $importer->get_leap2a_namespace());
// Try using countrycode attribute first, but fall back to name if it's not present or
// doesn't represent a country
......@@ -520,7 +524,7 @@ class LeapImportInternal extends LeapImportArtefactPlugin {
);
foreach ($persondata as $item) {
$leapattributes = PluginImportLeap::get_attributes($item, PluginImportLeap::NS_LEAP);
$leapattributes = PluginImportLeap::get_attributes($item, $importer->get_leap2a_namespace());
if (isset($leapattributes['field'])) {
if (in_array($leapattributes['field'], array_keys($namefields))) {
// legal_given_name is allowed to occur any number of times
......
......@@ -190,7 +190,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
// the qualification the user gained in relation to this entry
foreach ($entry->link as $link) {
if (!isset($other_required_entries['achievement'])
&& $importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'supports') && isset($link['href'])) {
&& $importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'supports') && isset($link['href'])) {
if ($potentialqualification = $importer->get_entry_by_id((string)$link['href'])) {
if (PluginImportLeap::is_rdf_type($potentialqualification, $importer, 'achievement')) {
// We have a related achievement!
......@@ -280,7 +280,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
}
break;
case self::STRATEGY_IMPORT_AS_ACHIEVEMENT:
$dates = PluginImportLeap::get_leap_dates($entry);
$dates = PluginImportLeap::get_leap_dates($entry, $importer->get_namespaces(), $importer->get_leap2a_namespace());
$enddate = (isset($dates['end'])) ? self::convert_leap_date_to_resume_date($dates['end']) : '';
$values = array(
......@@ -292,7 +292,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
ArtefactTypeResumeComposite::ensure_composite_value($values, 'certification', $importer->get('usr'));
break;
case self::STRATEGY_IMPORT_AS_EMPLOYMENT:
$dates = PluginImportLeap::get_leap_dates($entry);
$dates = PluginImportLeap::get_leap_dates($entry, $importer->get_namespaces(), $importer->get_leap2a_namespace());
$startdate = (isset($dates['start'])) ? self::convert_leap_date_to_resume_date($dates['start']) : '';
$enddate = (isset($dates['end'])) ? self::convert_leap_date_to_resume_date($dates['end']) : '';
......@@ -313,7 +313,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
ArtefactTypeResumeComposite::ensure_composite_value($values, 'employmenthistory', $importer->get('usr'));
break;
case self::STRATEGY_IMPORT_AS_BOOK:
$dates = PluginImportLeap::get_leap_dates($entry);
$dates = PluginImportLeap::get_leap_dates($entry, $importer->get_namespaces(), $importer->get_leap2a_namespace());
$enddate = (isset($dates['end'])) ? self::convert_leap_date_to_resume_date($dates['end']) : '';
$contribution = $description = '';
......@@ -333,7 +333,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
ArtefactTypeResumeComposite::ensure_composite_value($values, 'book', $importer->get('usr'));
break;
case self::STRATEGY_IMPORT_AS_EDUCATION:
$dates = PluginImportLeap::get_leap_dates($entry);
$dates = PluginImportLeap::get_leap_dates($entry, $importer->get_namespaces(), $importer->get_leap2a_namespace());
$startdate = (isset($dates['start'])) ? self::convert_leap_date_to_resume_date($dates['start']) : '';
$enddate = (isset($dates['end'])) ? self::convert_leap_date_to_resume_date($dates['end']) : '';
......@@ -366,7 +366,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
ArtefactTypeResumeComposite::ensure_composite_value($values, 'educationhistory', $importer->get('usr'));
break;
case self::STRATEGY_IMPORT_AS_MEMBERSHIP:
$dates = PluginImportLeap::get_leap_dates($entry);
$dates = PluginImportLeap::get_leap_dates($entry, $importer->get_namespaces(), $importer->get_leap2a_namespace());
$startdate = (isset($dates['start'])) ? self::convert_leap_date_to_resume_date($dates['start']) : '';
$enddate = (isset($dates['end'])) ? self::convert_leap_date_to_resume_date($dates['end']) : '';
......@@ -397,14 +397,14 @@ class LeapImportResume extends LeapImportArtefactPlugin {
$composites = array();
$person = $importer->get_entry_by_id($persondataid);
$persondata = $person->xpath('leap:persondata');
$persondata = $person->xpath('leap2:persondata');
foreach ($persondata as $item) {
$leapattributes = PluginImportLeap::get_attributes($item, PluginImportLeap::NS_LEAP);
$leapattributes = PluginImportLeap::get_attributes($item, $importer->get_leap2a_namespace());
if (!isset($leapattributes['field'])) {
// 'Field' is required
// http://wiki.cetis.ac.uk/2009-03/Leap2A_personal_data#field
$importer->trace('WARNING: persondata element did not have leap:field attribute');
$importer->trace('WARNING: persondata element did not have leap2:field attribute');
continue;
}
......@@ -491,7 +491,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
* @return string The ID of the organization if there is one, else an empty string
*/
private static function check_for_supporting_organization(PluginImportLeap $importer, $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'is_supported_by') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'is_supported_by') && isset($link['href'])) {
if ($potentialorganization = $importer->get_entry_by_id((string)$link['href'])) {
if (PluginImportLeap::is_rdf_type($potentialorganization, $importer, 'organization')) {
return (string)$link['href'];
......@@ -522,7 +522,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
$found = false;
foreach ($entry->link as $link) {
if ($importer->curie_equals($link['rel'], PluginImportLeap::NS_LEAP, 'is_part_of') && isset($link['href'])) {
if ($importer->curie_equals($link['rel'], $importer->get_leap2a_namespace(), 'is_part_of') && isset($link['href'])) {
$href = (string)$link['href'];
if (isset($cache[$href])) {
$found = true;
......@@ -539,7 +539,7 @@ class LeapImportResume extends LeapImportArtefactPlugin {
}
if ($found) {
$leapattributes = $importer->get_attributes($link, PluginImportLeap::NS_LEAP);
$leapattributes = $importer->get_attributes($link, $importer->get_leap2a_namespace());
$displayorder = (isset($leapattributes['display_order']) && intval($leapattributes['display_order']) > 0)
? $leapattributes['display_order']
: '';
......
......@@ -50,6 +50,8 @@ class PluginImportLeap extends PluginImport {
protected $logtargets = LOG_TARGET_ERRORLOG;
protected $logfile = '';
protected $profile = false;
protected $leap2anamespace = null;
protected $leap2atypenamespace = null;
private $snapshots = array();
......@@ -58,9 +60,11 @@ class PluginImportLeap extends PluginImport {
const NS_ATOM = 'http://www.w3.org/2005/Atom';
const NS_RDF = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
const NS_LEAP = 'http://wiki.cetis.ac.uk/2009-03/LEAP2A_predicates#';
const NS_LEAPTYPE = 'http://wiki.cetis.ac.uk/2009-03/LEAP2A_types#';
const NS_CATEGORIES = 'http://wiki.cetis.ac.uk/2009-03/LEAP2A_categories/';
const NS_LEAP_200903 = 'http://wiki.cetis.ac.uk/2009-03/LEAP2A_predicates#';
const NS_LEAPTYPE_200903 = 'http://wiki.cetis.ac.uk/2009-03/LEAP2A_types#';
const NS_CATEGORIES_200903 = 'http://wiki.cetis.ac.uk/2009-03/LEAP2A_categories/';
const NS_LEAP = 'http://terms.leapspecs.org/';
const NS_CATEGORIES = 'http://wiki.leapspecs.org/2A/categories';
const NS_MAHARA = 'http://wiki.mahara.org/Developer_Area/Import%2F%2FExport/LEAP_Extensions#';
const XHTML_DIV = '<div xmlns="http://www.w3.org/1999/xhtml">';
......@@ -117,6 +121,8 @@ class PluginImportLeap extends PluginImport {
$this->trace("Document loaded, entries: " . count($this->xml->entry));
$this->snapshot('loaded XML');
$this->detect_leap2a_namespace();
$this->ensure_document_valid();
$this->create_strategy_listing();
......@@ -130,6 +136,23 @@ class PluginImportLeap extends PluginImport {
db_commit();
}
/**
* detect the leap2a namespace of the import document by looking for the 'version' element
*
*
*/
private function detect_leap2a_namespace () {
// check for the leap2a version used
$version = $this->xml->xpath('//leap2:version');
if($version) {
$this->leap2anamespace = self::NS_LEAP;
$this->leap2atypenamespace = self::NS_LEAP;
} else {
$this->leap2anamespace = self::NS_LEAP_200903;
$this->leap2atypenamespace = self::NS_LEAPTYPE_200903;
}
}
private function ensure_document_valid() {
// TODO: http://wiki.mahara.org/Developer_Area/Import%2f%2fExport/Import%3a_Implementation_Plan#beginning
//
......@@ -146,7 +169,12 @@ class PluginImportLeap extends PluginImport {
// Check all the namespaces we're gonna need are declared, and warn if
// they're not there
foreach (array(self::NS_ATOM, self::NS_RDF, self::NS_LEAP, self::NS_LEAPTYPE, self::NS_CATEGORIES) as $ns) {
if($this->leap2anamespace == self::NS_LEAP) {
$namespaces = array(self::NS_ATOM, self::NS_RDF, self::NS_LEAP, self::NS_CATEGORIES);
} else {
$namespaces = array(self::NS_ATOM, self::NS_RDF, self::NS_LEAP_200903, self::NS_LEAPTYPE_200903, self::NS_CATEGORIES_200903);
}
foreach ($namespaces as $ns) {
if (!isset($this->namespaces[$ns])) {
$this->trace("WARNING: Namespaces $ns wasn't declared - this will make importing data correctly difficult");
}
......@@ -831,7 +859,8 @@ class PluginImportLeap extends PluginImport {
* @return boolean Whether it's worth checking in more detail
*/
private function artefact_reference_quickcheck($field) {
return (false !== strpos($field, 'rel="leap:has_part"')
return (false !== strpos($field, 'rel="'.$this->leap2anamespace.':has_part"')
|| false !== strpos($field, 'rel="enclosure"'))
&& (
(false !== strpos($field, '<img'))
......@@ -846,7 +875,7 @@ class PluginImportLeap extends PluginImport {
* @return string The fixed field
*/
private function fix_artefact_reference($field) {
$match = '#<((img)|a)([^>]+)rel="(?:leap:has_part|enclosure)" (?:src|href)="([^"]+)"([^>]*)>#';
$match = '#<((img)|a)([^>]+)rel="(?:'.$this->leap2anamespace.':has_part|enclosure)" (?:src|href)="([^"]+)"([^>]*)>#';
$field = preg_replace_callback($match,
array($this, '_fixref'),
$field);
......@@ -973,7 +1002,7 @@ class PluginImportLeap extends PluginImport {
*/
public static function is_rdf_type(SimpleXMLElement $entry, PluginImportLeap $importer, $rdftype) {
$result = $entry->xpath('rdf:type['
. $importer->curie_xpath('@rdf:resource', PluginImportLeap::NS_LEAPTYPE, $rdftype) . ']');
. $importer->curie_xpath('@rdf:resource', $importer->leap2anamespace, $rdftype) . ']');
return isset($result[0]) && $result[0] instanceof SimpleXMLElement;
}
......@@ -1054,7 +1083,7 @@ class PluginImportLeap extends PluginImport {
}
/**
* Look for leap:date elements that are part of an entry (if any) and
* Look for leap2:date elements that are part of an entry (if any) and
* return the values we parse from them
*
* Returned in a structure like so:
......@@ -1082,10 +1111,10 @@ class PluginImportLeap extends PluginImport {
*
* Spec reference: http://wiki.cetis.ac.uk/2009-03/Leap2A_literals#date
*/
public static function get_leap_dates(SimpleXMLElement $entry) {
public static function get_leap_dates(SimpleXMLElement $entry, $namespaces, $ns) {
$dates = array();
foreach (array('start', 'end', 'target') as $point) {
$dateelement = $entry->xpath('leap:date[@leap:point="' . $point . '"]');
$dateelement = $entry->xpath($namespaces[$ns].':date[@'.$namespaces[$ns].':point="' . $point . '"]');
if (count($dateelement) == 1) {
$dateelement = $dateelement[0];
}
......@@ -1096,9 +1125,9 @@ class PluginImportLeap extends PluginImport {
$dates[$point]['value'] = $date;
}
// Parse for leap:label
// Parse for leap2:label
$leapattributes = array();
foreach ($dateelement->attributes(PluginImportLeap::NS_LEAP) as $key => $value) {
foreach ($dateelement->attributes($ns) as $key => $value) {
$leapattributes[$key] = (string)$value;
}
if (isset($leapattributes['label'])) {
......@@ -1127,6 +1156,13 @@ class PluginImportLeap extends PluginImport {
return $attributes;
}
public function get_leap2a_namespace() {
return $this->leap2anamespace;
}
public function get_namespaces() {
return $this->namespaces;
}
}
......
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