diff -r 009efee954f7 -r 17f10b56c079 server/src/app/Models/Document.php --- a/server/src/app/Models/Document.php Thu Jun 02 18:16:17 2016 +0200 +++ b/server/src/app/Models/Document.php Thu Jun 02 18:24:19 2016 +0200 @@ -17,119 +17,28 @@ * Model class for Document. Inherit from EasyRd\Resource * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}} */ -class Document extends RdfModelResource implements JsonSerializable { +class Document extends DocumentResult { 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 $publishers = null; private $mediaArray = null; - private $issued = null; - private $modified = null; + private $transcript = false; private $contributors = null; private $subjects = null; - public function getProvidedCHO() { - if(is_null($this->providedCHO)) { - $this->providedCHO = $this->get(""); - } - return $this->providedCHO; - } - - private function clearMemoizationCache() { - $this->providedCHO = null; - $this->title = false; - $this->lang = null; - $this->langResolved = null; + protected function clearMemoizationCache() { + parent::clearMemoizationCache(); $this->publishers = null; $this->mediaArray = null; - $this->issued = null; - $this->modified = null; $this->contributors = null; $this->subjects = null; - } - - public function getId() { - if(is_null($this->id)) { - $ids = $this->getProvidedCHO()->all(''); - 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; + $this->transcript = false; } - public function getLanguage() { - if(is_null($this->lang)) { - try { - $langs = $this->getProvidedCHO()->all(''); - 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($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(''); - } 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 getPublishers() { if(is_null($this->publishers)) { @@ -142,82 +51,47 @@ return $this->publishers; } - public function getIssued() { - if(is_null($this->issued)) { - try { - $this->issued = $this->getProvidedCHO()->getLiteral(""); - } catch(\Exception $e) { - $this->issued = null; - } - } - return $this->issued; - } + private function parseWebResources() { + + $this->mediaArray = []; + $this->transcript = null; - public function getIssuedValue() { - $issued = $this->getIssued(); - return is_null($issued)?null:$issued->getValue(); - } + $master = $this->get(''); + $masterUrl = is_null($master)?null:$master->getUri(); + + foreach($this->graph->allOfType("") 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"); + } - public function getModified() { - if(is_null($this->modified)) { - try { - $this->modified = $this->getProvidedCHO()->getLiteral(""); - if(is_null($this->modified)) { - $this->modified = $this->getIssued(); - } - } catch(\Exception $e) { - $this->modified = null; - } + 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"); + } } - return $this->modified; - } - public function getModifiedValue() { - $modified = $this->getModified(); - return is_null($modified)?null:$modified->getValue(); - } - - 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 getMediaArray() { if(is_null($this->mediaArray)) { - //TODO: add media type - $this->mediaArray = []; - - $master = $this->get(''); - $masterUrl = is_null($master)?null:$master->getUri(); - - foreach($this->graph->allOfType("") as $webResource) { - $extent = $webResource->getLiteral(""); - $extent = is_null($extent)?null:$extent->getValue(); - $extent_ms = Utils::iso8601IntervalToMillis($extent); - $format = $webResource->getLiteral("dc11:format"); - - $this->mediaArray[$webResource->getUri()] = [ - 'url' => $webResource->getUri(), - 'format' => is_null($format)?null:$format->getValue(), - 'extent' => $extent, - 'extent_ms' => $extent_ms, - 'master' => (($webResource->getUri() === $masterUrl)?true:false) - ]; - } + $this->parseWebResources(); } return $this->mediaArray; } @@ -240,21 +114,10 @@ } public function getTranscript() { - $res = null; - foreach($this->graph->allOfType("") as $webResource) { - $format = $webResource->getLiteral("dc11:format")->getValue(); - - if((0 === strpos($format, 'application/xml')) || - (0 === strpos($format, 'application/pdf')) ) { - $conformsTo = $webResource->getResource(""); - $res = [ - 'url' => $webResource->getUri(), - 'format' => $format, - 'conforms-to' => $conformsTo?$conformsTo->getUri():null, - ]; - } + if($this->transcript === false) { + $this->parseWebResources(); } - return $res; + return $this->transcript; } public function getContributors() { @@ -393,24 +256,28 @@ * clone also the innerDocumenent */ public function __clone() { - - $this->graph = new Graph($this->graph->getUri(), $this->graph->toRdfPhp()); + if(!is_null($this->graph)) { + $this->graph = new Graph($this->graph->getUri(), $this->graph->toRdfPhp()); + } } public function jsonSerialize() { - if(!$this->graph) { - return [ - 'id' => $this->getId(), - ]; - } else { - $mediaArray = array_map( - function($m) { - $f = Utils::processLiteralResourceOrString($m['format']); - $res = $m; - $res['format'] = $f; - return $res;}, - $this->getMediaArray() - ); + + $res = parent::jsonSerialize(); + if($this->graph) { + + $mediaArray = is_null($this->getMediaArray())? + null: + array_combine( + array_map(function($m) { return $m->getUrl(); }, $this->getMediaArray()), + array_map( + function($m) { + return $m->jsonSerialize();}, + $this->getMediaArray() + ) + ); + + $transcript = is_null($this->getTranscript())?null:$this->getTranscript()->jsonSerialize(); $publishers = array_map( function($v) { return Utils::processLiteralResourceOrString($v); }, @@ -427,25 +294,17 @@ $this->getSubjects() ); - $res = [ - 'id' => $this->getId(), - 'uri' => $this->getUri(), - 'title' => $this->getTitleValue(), - 'language' => $this->getLanguageValue(), - 'modified' => $this->getModifiedValue(), + $res = array_merge($res, [ 'publishers' => $publishers, 'contributors' => $contributors, 'subjects' => $subjects, - 'transcript' => $this->getTranscript(), + 'transcript' => $transcript, 'mediaArray'=> $mediaArray - ]; + ]); - if($this->language_resolved) { - $res['language_resolved'] = $this->getLanguageResolved(); - } + } + return $res; - return $res; - } } }