]> git.parisson.com Git - telemeta.git/commitdiff
eztelemeta: implement OAI-PMH item fetching
authorolivier <>
Thu, 9 Apr 2009 19:16:47 +0000 (19:16 +0000)
committerolivier <>
Thu, 9 Apr 2009 19:16:47 +0000 (19:16 +0000)
tools/eztelemeta/datatypes/eztelemetaitem/eztelemetaitemtype.php
tools/eztelemeta/design/standard/override/templates/embed/eztelemetaitem.tpl
tools/eztelemeta/design/standard/templates/content/datatype/edit/eztelemetaitem.tpl
tools/eztelemeta/design/standard/templates/content/datatype/view/eztelemetaitem.tpl

index 835eba701f92ac6f2730d2756b5fd62eb65173be..6a64179dadd47a8483f6fcdbaa683a3a42ec84cb 100755 (executable)
@@ -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");
index 76950bcd083759d14a80b2c7a574270d124a9488..acffddd8b5e3cd429eb6d0c8437a70df4150930c 100755 (executable)
@@ -1,7 +1,12 @@
 <div class="view-embed">
     <div class="content-media">
     {let attribute=$object.data_map.item}
-        {literal}<i>{/literal}Telemeta Item {$attribute.data_text}{literal}</i>{/literal}
+        Telemeta Item:
+        <dl>
+        <dt>Server:</dt><dd>{$attribute.content.url}</dd>
+        <dt>Identifier:</dt><dd>{$attribute.content.id}</dd>
+        <dt>Title:</dt><dd>{$attribute.content.title|wash}</dd>
+        </dl>
     {/let}
     </div>
 </div>
index 3bdf364aa4fcf5b465686d68395adbc56e564eed..f71f44774a8a5371955a57f058ad762ebb8b14fa 100755 (executable)
@@ -1,3 +1,12 @@
-<input type="text" size="32"
-       name="ContentObjectAttribute_itemid_{$attribute.id}"
-       value="{$attribute.itemid}" />
+{* DO NOT EDIT THIS FILE! Use an override template instead. *}
+{default attribute_base=ContentObjectAttribute}
+
+<div class="block">
+<label>{'Telemeta URL'|i18n( 'design/standard/content/datatype' )}:</label>
+<input class="box ezcc-{$attribute.object.content_class.identifier} ezcca-{$attribute.object.content_class.identifier}_{$attribute.contentclass_attribute_identifier}" type="text" name="{$attribute_base}_url_{$attribute.id}" value="{$attribute.content.url}" />
+</div>       
+
+<div class="block">
+<label>{'Item Identifier'|i18n( 'design/standard/content/datatype' )}:</label>
+<input class="box ezcc-{$attribute.object.content_class.identifier} ezcca-{$attribute.object.content_class.identifier}_{$attribute.contentclass_attribute_identifier}" type="text" name="{$attribute_base}_itemid_{$attribute.id}" value="{$attribute.content.id}" />
+</div>       
index 1a97213279c0283e75f630647b91a5fe2ad128e0..a143026ea216ddedd6dc7be0fc7858d6270931c4 100755 (executable)
@@ -1 +1,6 @@
-Telemeta Item {$attribute.data_text}
+Telemeta Item:
+<dl>
+<dt>Server:</dt><dd>{$attribute.content.url}</dd>
+<dt>Identifier:</dt><dd>{$attribute.content.id}</dd>
+<dt>Title:</dt><dd>{$attribute.content.title|wash}</dd>
+</dl>