From: olivier <> Date: Thu, 9 Apr 2009 19:16:47 +0000 (+0000) Subject: eztelemeta: implement OAI-PMH item fetching X-Git-Tag: 1.1~683 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=0090f6ced464bc7e7cefb72728232d6b20bb90a4;p=telemeta.git eztelemeta: implement OAI-PMH item fetching --- diff --git a/tools/eztelemeta/datatypes/eztelemetaitem/eztelemetaitemtype.php b/tools/eztelemeta/datatypes/eztelemetaitem/eztelemetaitemtype.php index 835eba70..6a64179d 100755 --- a/tools/eztelemeta/datatypes/eztelemetaitem/eztelemetaitemtype.php +++ b/tools/eztelemeta/datatypes/eztelemetaitem/eztelemetaitemtype.php @@ -28,10 +28,11 @@ class eZTelemetaItemType extends eZDataType function validateObjectAttributeHTTPInput($http, $base, $attribute) { - $idvar = "{$base}_itemid_" . $attribute->attribute('id'); - touch("/tmp/$idvar"); + $idvar = "{$base}_itemid_" . $attribute->attribute('id'); + $urlvar = "{$base}_url_" . $attribute->attribute('id'); if ($http->hasPostVariable($idvar)) { - $itemId = $http->postVariable($idvar); + $itemId = trim($http->postVariable($idvar)); + $url = trim($http->postVariable($urlvar)); $classAttribute = $attribute->contentClassAttribute(); if ($classAttribute->attribute("is_required")) { if (!$itemId) { @@ -40,40 +41,125 @@ class eZTelemetaItemType extends eZDataType __CLASS__)); return eZInputValidator::STATE_INVALID; } + if (!$url) { + $attribute->setValidationError(ezi18n('content/datatypes', + "A valid Telemeta URL is required", + __CLASS__)); + return eZInputValidator::STATE_INVALID; + } } - if ($itemId != 1000) { - $attribute->setValidationError(ezi18n('content/datatypes', - "Invalid Telemeta Item identifier", - __CLASS__)); + $item = $this->initItem($url, $itemId); + try { + $this->fetchItem($item); + } catch (eZTelemetaError $e) { + $attribute->setValidationError(ezi18n('content/datatypes', $e->getMessage(), __CLASS__)); return eZInputValidator::STATE_INVALID; } } return eZInputValidator::STATE_ACCEPTED; } + function initItem($url, $itemId) + { + return array( + 'id' => $itemId, + 'url' => $url, + 'title' => '', + 'creator' => '', + 'description' => '', + 'rights' => '' + ); + } + + function fetchItem($item) + { + $url = $item['url']; + if (!ereg('^http://', $url)) { + $url = "http://$url"; + } + $url = ereg_replace('/*$', '', $url); + $id = urlencode('item:' . $item['id']); + $request = "$url/oai/?verb=GetRecord&identifier=$id&metadataPrefix=oai_dc"; + + $doc = new DOMDocument(); + if (!$doc->load($request)) { + throw new ezTelemetaError("The Telemeta server couldn't be reached or returned malformed XML (request: $request)"); + } + + $root = $doc->getElementsByTagName('OAI-PMH'); + if (!$root->length) + throw new eZTelemetaError("Retrieved XML document isn't a valid OAI-PMH response"); + + $root = $root->item(0); + $error = $root->getElementsByTagName('error'); + if ($error->length) { + $msg = $error->item(0)->firstChild->nodeValue; + throw new eZTelemetaError("Telemeta OAI-PMH error: $msg"); + } + + $record = $root->getElementsByTagName('GetRecord'); + if (!$record->length) { + throw new eZTelemetaError("Retrieved XML document isn't a valid OAI-PMH response (missing GetRecord tag)"); + } + + $dc = $record->item(0)->getElementsByTagNameNS('*', 'dc')->item(0); + $result = $this->initItem($item['url'], $item['id']); + foreach ($dc->childNodes as $element) { + if ($element->nodeType == XML_ELEMENT_NODE) { + $tag = str_replace('dc:', '', $element->tagName); + if (array_key_exists($tag, $result) and empty($result[$tag])) { + $result[$tag] = trim($element->firstChild->nodeValue); + } + } + } + + if (!$result['title']) { + throw new eZTelemetaError("The retrieved item has no title"); + } + + return array_merge($item, $result); + } + function fetchObjectAttributeHTTPInput($http, $base, $attribute) { - $idvar = "{$base}_itemid_" . $attribute->attribute('id'); + $idvar = "{$base}_itemid_" . $attribute->attribute('id'); + $urlvar = "{$base}_url_" . $attribute->attribute('id'); if ($http->hasPostVariable($idvar)) { - $itemId = $http->postVariable($idvar); - $attribute->setAttribute("data_text", $itemId); + $itemId = trim($http->postVariable($idvar)); + $url = trim($http->postVariable($urlvar)); + $item = $this->initItem($url, $itemId); + try { + $item = $this->fetchItem($item); + } catch (eZTelemetaError $e) { + } + $attribute->setAttribute("data_text", serialize($item)); } return true; } function objectAttributeContent($attribute) { - return $attribute->attribute("data_text"); + $item = unserialize($attribute->attribute("data_text")); + try { + $filled = $this->fetchItem($item); + return $filled; + } catch (eZTelemetaError $e) { + return $item; + } } function metaData($attribute) { - return $attribute->attribute("data_text"); + $data = unserialize($attribute->attribute("data_text")); + return array('title' => $data['title'], 'description' => $data['description']); } function title($attribute, $name = null) { - return "Telemeta Item id " . $attribute->attribute("data_text"); + $data = unserialize($attribute->attribute("data_text")); + if (!$data['title']) + return 'untitled'; + return $data['title']; } function isIndexable() @@ -83,4 +169,13 @@ class eZTelemetaItemType extends eZDataType } +class eZTelemetaError extends ezcBaseException +{ + public function __construct($msg) + { + parent::__construct($msg); + } + +} + eZDataType::register(eZTelemetaItemType::DATA_TYPE_STRING, "eztelemetaitemtype"); diff --git a/tools/eztelemeta/design/standard/override/templates/embed/eztelemetaitem.tpl b/tools/eztelemeta/design/standard/override/templates/embed/eztelemetaitem.tpl index 76950bcd..acffddd8 100755 --- a/tools/eztelemeta/design/standard/override/templates/embed/eztelemetaitem.tpl +++ b/tools/eztelemeta/design/standard/override/templates/embed/eztelemetaitem.tpl @@ -1,7 +1,12 @@
{let attribute=$object.data_map.item} - {literal}{/literal}Telemeta Item {$attribute.data_text}{literal}{/literal} + Telemeta Item: +
+
Server:
{$attribute.content.url}
+
Identifier:
{$attribute.content.id}
+
Title:
{$attribute.content.title|wash}
+
{/let}
diff --git a/tools/eztelemeta/design/standard/templates/content/datatype/edit/eztelemetaitem.tpl b/tools/eztelemeta/design/standard/templates/content/datatype/edit/eztelemetaitem.tpl index 3bdf364a..f71f4477 100755 --- a/tools/eztelemeta/design/standard/templates/content/datatype/edit/eztelemetaitem.tpl +++ b/tools/eztelemeta/design/standard/templates/content/datatype/edit/eztelemetaitem.tpl @@ -1,3 +1,12 @@ - +{* DO NOT EDIT THIS FILE! Use an override template instead. *} +{default attribute_base=ContentObjectAttribute} + +
+ + +
+ +
+ + +
diff --git a/tools/eztelemeta/design/standard/templates/content/datatype/view/eztelemetaitem.tpl b/tools/eztelemeta/design/standard/templates/content/datatype/view/eztelemetaitem.tpl index 1a972132..a143026e 100755 --- a/tools/eztelemeta/design/standard/templates/content/datatype/view/eztelemetaitem.tpl +++ b/tools/eztelemeta/design/standard/templates/content/datatype/view/eztelemetaitem.tpl @@ -1 +1,6 @@ -Telemeta Item {$attribute.data_text} +Telemeta Item: +
+
Server:
{$attribute.content.url}
+
Identifier:
{$attribute.content.id}
+
Title:
{$attribute.content.title|wash}
+