Split document model in document and document results
authorymh <ymh.work@gmail.com>
Sat, 06 Aug 2016 21:27:53 +0700
changeset 260 64caee7ce38d
parent 259 6af61adbd570
child 261 02e2396bcbbc
Split document model in document and document results
server/src/app/Models/Document.php
server/src/app/Models/DocumentBase.php
server/src/app/Models/DocumentResult.php
--- a/server/src/app/Models/Document.php	Tue Aug 23 23:05:25 2016 +0200
+++ b/server/src/app/Models/Document.php	Sat Aug 06 21:27:53 2016 +0700
@@ -17,7 +17,7 @@
  * Model class for Document. Inherit from EasyRd\Resource
  * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}}
  */
-class Document extends DocumentResult {
+class Document extends DocumentBase {
 
     public function __construct($uri, $graph = null) {
         //                print($graph->dump('html'));
@@ -42,17 +42,6 @@
     }
 
 
-    public function getPublishers() {
-        if(is_null($this->publishers)) {
-            try {
-                $this->publishers = $this->getProvidedCHO()->all('dc11:publisher');
-            } catch(\Exception $e) {
-               $this->publishers = [];
-            }
-        }
-        return $this->publishers;
-    }
-
     private function parseWebResources() {
 
         $this->mediaArray = [];
@@ -63,29 +52,29 @@
 
         foreach($this->graph->allOfType("<http://www.europeana.eu/schemas/edm/WebResource>") as $webResource) {
             $formatLit = $webResource->getLiteral("dc11:format");
-             $format = is_null($formatLit)?null:$formatLit->getValue();
-             if(is_null($format)) {
-                 throw new ModelsException("parseWebResources: No dc:11 format on web resource");
-             }
+            $format = is_null($formatLit)?null:$formatLit->getValue();
+            if(is_null($format)) {
+                throw new ModelsException("parseWebResources: No dc:11 format on web resource");
+            }
 
-             if(0 === strpos($format, 'audio/') ||
-                0 === strpos($format, 'video/') ||
-                0 === strpos($format, 'Sampling:') ) {
-                 array_push(
-                     $this->mediaArray,
-                     new MediaResource(
-                         $webResource->getUri(),
-                         $this->graph,
-                         (($webResource->getUri() === $masterUrl)?true:false))
-                 );
-             } else if(
-                 0 === strpos($format, 'application/xml') ||
-                 0 === strpos($format, 'application/pdf') ) {
-                 $this->transcript = new TranscriptResource($webResource->getUri(), $this->graph);
-             }
-             else {
-                 throw new ModelsException("parseWebResources: unknown format");
-             }
+            if(0 === strpos($format, 'audio/') ||
+               0 === strpos($format, 'video/') ||
+               0 === strpos($format, 'Sampling:') ) {
+                array_push(
+                    $this->mediaArray,
+                    new MediaResource(
+                        $webResource->getUri(),
+                        $this->graph,
+                        (($webResource->getUri() === $masterUrl)?true:false))
+                );
+            } else if(
+                0 === strpos($format, 'application/xml') ||
+                0 === strpos($format, 'application/pdf') ) {
+                $this->transcript = new TranscriptResource($webResource->getUri(), $this->graph);
+            }
+            else {
+                throw new ModelsException("parseWebResources: unknown format");
+            }
         }
 
     }
@@ -263,6 +252,27 @@
 
     }
 
+    public function getPublishers() {
+        if(is_null($this->publishers)) {
+            try {
+                $this->publishers = $this->getProvidedCHO()->all('dc11:publisher');
+            } catch(\Exception $e) {
+               $this->publishers = [];
+            }
+        }
+        return $this->publishers;
+    }
+
+    public function getPublishersValues() {
+        $publishers = $this->getPublishers();
+        return array_map(
+            function($v) { return Utils::processLiteralResourceOrString($v); },
+            $this->getPublishers()
+        );
+    }
+
+
+
     public function isIsomorphic($doc) {
         return Isomorphic::isomorphic($this->graph, $doc->graph);
     }
@@ -296,10 +306,7 @@
             $transcript = is_null($this->getTranscript())?null:$this->getTranscript()->jsonSerialize();
             $geoInfo = is_null($this->getGeoInfo())?null:$this->getGeoInfo()->jsonSerialize();
 
-            $publishers = array_map(
-                function($v) { return Utils::processLiteralResourceOrString($v); },
-                $this->getPublishers()
-            );
+            $publishers = $this->getPublishersValues();
 
             $contributors = array_map(
                 function($c) { unset($c['nameLiteral']); return $c; },
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/server/src/app/Models/DocumentBase.php	Sat Aug 06 21:27:53 2016 +0700
@@ -0,0 +1,203 @@
+<?php
+namespace CorpusParole\Models;
+
+use Config;
+use CorpusParole\Libraries\Utils;
+use CorpusParole\Libraries\CocoonUtils;
+use CorpusParole\Libraries\RdfModel\RdfModelResource;
+use JsonSerializable;
+use Log;
+use EasyRdf\Literal;
+use EasyRdf\Resource;
+use EasyRdf\Graph;
+
+
+/**
+ */
+class DocumentBase extends RdfModelResource implements JsonSerializable {
+
+    public function __construct($uri, $graph = null) {
+        //print($graph->dump('html'));
+        parent::__construct($uri, $graph);
+    }
+
+    private $id = null;
+
+    // memoization
+    private $providedCHO = null;
+    private $title = false;
+    private $lang = null;
+    private $langResolved = null;
+    private $issued = null;
+    private $modified = null;
+
+
+    public function getProvidedCHO() {
+        if(is_null($this->providedCHO)) {
+            $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>");
+        }
+        return $this->providedCHO;
+    }
+
+    protected function clearMemoizationCache() {
+        $this->providedCHO = null;
+        $this->title = false;
+        $this->lang = null;
+        $this->langResolved = null;
+        $this->issued = null;
+        $this->modified = null;
+    }
+
+    public function getId() {
+        if(is_null($this->id)) {
+            $ids = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/identifier>');
+            foreach ($ids as $id) {
+                if($id instanceof Literal && strpos($id->getValue(), config('corpusparole.corpus_id_scheme')) === 0) {
+                    $this->id = $id->getValue();
+                }
+            }
+            if(is_null($this->id)) {
+                $this->id = CocoonUtils::getIdFromCorpusUri($this->uri);
+            }
+        }
+        return $this->id;
+    }
+
+    public function getLanguage() {
+        if(is_null($this->lang)) {
+            try {
+                $langs = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/language>');
+                if(count($langs) > 0) {
+                    $this->lang = $langs[0];
+                }
+            } catch(\Exception $e) {
+                $this->lang = null;
+            }
+        }
+        return $this->lang;
+    }
+
+    public function getLanguageValue() {
+        $lang = $this->getLanguage();
+        if($lang instanceof Resource) {
+            return $lang->getUri();
+        } else if($lang instanceof Literal) {
+            return $lang->getValue();
+        }
+        return null;
+    }
+
+    public function getLanguageResolved() {
+        return $this->langResolved;
+    }
+
+    public function setLanguageResolved($languageResolved) {
+        $this->langResolved = $languageResolved;
+    }
+
+
+    public function getTitle() {
+        if($this->title === false) {
+            try {
+                $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>');
+            } catch(\Exception $e) {
+                $this->title = null;
+            }
+        }
+        return $this->title;
+    }
+
+    public function setTitle($value, $lang="fr") {
+        $oldTitle = $this->getTitle();
+        if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) {
+            $literalTitle = new Literal($value, $lang, null);
+            $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle);
+            //clear cache
+            $this->title = false;
+        }
+    }
+
+
+
+    public function getTitleValue() {
+        $title = $this->getTitle();
+        return is_null($title)?null:$title->getValue();
+    }
+
+    public function getIssued() {
+        if(is_null($this->issued)) {
+            try {
+                $this->issued = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/issued>");
+            } catch(\Exception $e) {
+                $this->issued = null;
+            }
+        }
+        return $this->issued;
+    }
+
+    public function getIssuedValue() {
+        $issued = $this->getIssued();
+        return is_null($issued)?null:$issued->getValue();
+    }
+
+    public function getModified() {
+        if(is_null($this->modified)) {
+            try {
+                $this->modified = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/modified>");
+                if(is_null($this->modified)) {
+                    $this->modified = $this->getIssued();
+                }
+            } catch(\Exception $e) {
+                $this->modified = null;
+            }
+        }
+        return $this->modified;
+    }
+
+    public function setModified($value = null) {
+        if(is_null($value)) {
+            $value = gmdate(\DateTime::ATOM);
+        } elseif ($value instanceof \DateTime) {
+            $value = $value->format(\DateTime::ATOM);
+        }
+        $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value);
+
+        $modified = $this->getModified();
+        if($value && (!$modified || $modified->getValue() !== $value) ) {
+
+            $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF");
+            $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified);
+
+            $this->modified = null;
+        }
+    }
+
+
+    public function getModifiedValue() {
+        $modified = $this->getModified();
+        return is_null($modified)?null:$modified->getValue();
+    }
+
+
+    public function jsonSerialize() {
+        if(!$this->graph) {
+            return [
+                'id' => $this->getId(),
+            ];
+        } else {
+            $res = [
+                'id' => $this->getId(),
+                'uri' => $this->getUri(),
+                'title' => $this->getTitleValue(),
+                'language' => $this->getLanguageValue(),
+                'modified' => $this->getModifiedValue(),
+                'issued' => $this->getIssuedValue()            ];
+
+            if($this->language_resolved) {
+                $res['language_resolved'] = $this->getLanguageResolved();
+            }
+
+            return $res;
+        }
+    }
+}
--- a/server/src/app/Models/DocumentResult.php	Tue Aug 23 23:05:25 2016 +0200
+++ b/server/src/app/Models/DocumentResult.php	Sat Aug 06 21:27:53 2016 +0700
@@ -14,189 +14,68 @@
 
 /**
  */
-class DocumentResult extends RdfModelResource implements JsonSerializable {
+class DocumentResult extends DocumentBase {
 
     public function __construct($uri, $graph = null) {
-        //print($graph->dump('html'));
         parent::__construct($uri, $graph);
     }
 
-    private $id = null;
-
-    // memoization
-    private $providedCHO = null;
-    private $title = false;
-    private $lang = null;
-    private $langResolved = null;
-    private $issued = null;
-    private $modified = null;
-
-    public function getProvidedCHO() {
-        if(is_null($this->providedCHO)) {
-            $this->providedCHO = $this->get("<http://www.europeana.eu/schemas/edm/aggregatedCHO>");
-        }
-        return $this->providedCHO;
-    }
+    private $publishers = false;
+    private $duration = false;
+    private $durationMs = -1;
 
     protected function clearMemoizationCache() {
-        $this->providedCHO = null;
-        $this->title = false;
-        $this->lang = null;
-        $this->langResolved = null;
-        $this->issued = null;
-        $this->modified = null;
-    }
+        parent::clearMemoizationCache();
+        $this->publishers = false;
+        $this->duration = false;
+        $this->$durationMs = -1;
 
-    public function getId() {
-        if(is_null($this->id)) {
-            $ids = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/identifier>');
-            foreach ($ids as $id) {
-                if($id instanceof Literal && strpos($id->getValue(), config('corpusparole.corpus_id_scheme')) === 0) {
-                    $this->id = $id->getValue();
-                }
-            }
-            if(is_null($this->id)) {
-                $this->id = CocoonUtils::getIdFromCorpusUri($this->uri);
-            }
-        }
-        return $this->id;
     }
 
-    public function getLanguage() {
-        if(is_null($this->lang)) {
+    public function getPublishers() {
+        if($this->publishers === false) {
             try {
-                $langs = $this->getProvidedCHO()->all('<http://purl.org/dc/elements/1.1/language>');
-                if(count($langs) > 0) {
-                    $this->lang = $langs[0];
-                }
+                $this->publishers = $this->getProvidedCHO()->getLiteral('dc11:publisher');
             } catch(\Exception $e) {
-                $this->lang = null;
+                $this->publishers = null;
             }
         }
-        return $this->lang;
+        return $this->publishers;
     }
 
-    public function getLanguageValue() {
-        $lang = $this->getLanguage();
-        if($lang instanceof Resource) {
-            return $lang->getUri();
-        } else if($lan instanceof Literal) {
-            return $lang->getValue();
-        }
-        return null;
-    }
-
-    public function getLanguageResolved() {
-        return $this->langResolved;
-    }
-    public function setLanguageResolved($languageResolved) {
-        $this->langResolved = $languageResolved;
-    }
-
-
-    public function getTitle() {
-        if($this->title === false) {
-            try {
-                $this->title = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/elements/1.1/title>');
-            } catch(\Exception $e) {
-                $this->title = null;
-            }
-        }
-        return $this->title;
+    public function getPublishersValue() {
+        $publishers = $this->getPublishers();
+        return is_null($publishers)?null:$publishers->getValue();
     }
 
-    public function setTitle($value, $lang="fr") {
-        $oldTitle = $this->getTitle();
-        if($oldTitle && $oldTitle->getValue() != $value && $oldTitle->getLang() != $lang) {
-            $literalTitle = new Literal($value, $lang, null);
-            $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/elements/1.1/title', $oldTitle, $literalTitle);
-            //clear cache
-            $this->title = false;
-        }
-    }
-
-
-
-    public function getTitleValue() {
-        $title = $this->getTitle();
-        return is_null($title)?null:$title->getValue();
-    }
-
-    public function getIssued() {
-        if(is_null($this->issued)) {
+    public function getDuration() {
+        if($this->duration === false) {
             try {
-                $this->issued = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/issued>");
+                $this->duration = $this->getProvidedCHO()->getLiteral('<http://purl.org/dc/terms/extent>');
             } catch(\Exception $e) {
-                $this->issued = null;
+                $this->duration = null;
             }
         }
-        return $this->issued;
-    }
-
-    public function getIssuedValue() {
-        $issued = $this->getIssued();
-        return is_null($issued)?null:$issued->getValue();
-    }
-
-    public function getModified() {
-        if(is_null($this->modified)) {
-            try {
-                $this->modified = $this->getProvidedCHO()->getLiteral("<http://purl.org/dc/terms/modified>");
-                if(is_null($this->modified)) {
-                    $this->modified = $this->getIssued();
-                }
-            } catch(\Exception $e) {
-                $this->modified = null;
-            }
-        }
-        return $this->modified;
+        return $this->duration;
     }
 
-    public function setModified($value = null) {
-        if(is_null($value)) {
-            $value = gmdate(\DateTime::ATOM);
-        } elseif ($value instanceof \DateTime) {
-            $value = $value->format(\DateTime::ATOM);
+    public function getDurationValue() {
+        if($this->durationMs === -1) {
+            $this->durationMs = Utils::iso8601IntervalToMillis($this->getDuration());
         }
-        $value = preg_replace('/[\+\-]00(\:?)00$/', 'Z', $value);
-
-        $modified = $this->getModified();
-        if($value && (!$modified || $modified->getValue() !== $value) ) {
-
-            $newModified = new Literal($value, null, "http://purl.org/dc/terms/W3CDTF");
-            $this->setSimpleProperty($this->getProvidedCHO(), 'http://purl.org/dc/terms/modified', $modified, $newModified);
-
-            $this->modified = null;
-        }
-    }
-
-
-    public function getModifiedValue() {
-        $modified = $this->getModified();
-        return is_null($modified)?null:$modified->getValue();
+        return $this->durationMs;
     }
 
+    public function jsonSerialize() {
 
-    public function jsonSerialize() {
-        if(!$this->graph) {
-            return [
-                'id' => $this->getId(),
-            ];
-        } else {
-            $res = [
-                'id' => $this->getId(),
-                'uri' => $this->getUri(),
-                'title' => $this->getTitleValue(),
-                'language' => $this->getLanguageValue(),
-                'modified' => $this->getModifiedValue(),
-                'issued' => $this->getIssuedValue()
-            ];
+        $res = parent::jsonSerialize();
 
-            if($this->language_resolved) {
-                $res['language_resolved'] = $this->getLanguageResolved();
-            }
-
-            return $res;
+        if($this->graph) {
+            $res = array_merge($res, [
+                'publishers' => $this->getPublishersValue(),
+                'duration' => $this->getDurationValue()
+            ]);
         }
+        return $res;
     }
 }