server/src/app/Models/Document.php
changeset 4 f55970e41793
parent 3 2b3247d02769
child 18 f2a40bbc27f6
equal deleted inserted replaced
3:2b3247d02769 4:f55970e41793
     2 namespace CorpusParole\Models;
     2 namespace CorpusParole\Models;
     3 
     3 
     4 use Config;
     4 use Config;
     5 use CorpusParole\Libraries\Utils;
     5 use CorpusParole\Libraries\Utils;
     6 use CorpusParole\Libraries\RdfModel\RdfModelResource;
     6 use CorpusParole\Libraries\RdfModel\RdfModelResource;
       
     7 use JsonSerializable;
     7 use Log;
     8 use Log;
       
     9 use EasyRdf\Literal;
       
    10 use EasyRdf\Resource;
       
    11 use EasyRdf\Graph;
       
    12 use EasyRdf\Isomorphic;
       
    13 
     8 
    14 
     9 /**
    15 /**
    10  * Model class for Document. Inherit from EasyRdf_Resource
    16  * Model class for Document. Inherit from EasyRd\Resource
    11  * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}}
    17  * SELECT DISTINCT ?g WHERE {GRAPH ?g {?s ?p ?o}}
    12  */
    18  */
    13 class Document extends RdfModelResource {
    19 class Document extends RdfModelResource implements JsonSerializable {
    14 
    20 
    15     public function __construct($uri, $graph = null) {
    21     public function __construct($uri, $graph = null) {
    16         parent::__construct($uri, $graph, 'foaf:primaryTopic');
    22         parent::__construct($uri, $graph);
    17     }
    23     }
    18 
    24 
    19     private $id = null;
    25     private $id = null;
    20 
    26 
    21     public function getId() {
    27     public function getId() {
    24         }
    30         }
    25         return $this->id;
    31         return $this->id;
    26     }
    32     }
    27 
    33 
    28     public function getTitle() {
    34     public function getTitle() {
    29         return $this->innerDocument->getLiteral('dc11:title');
    35         try {
       
    36             return $this->getLiteral('<http://purl.org/dc/elements/1.1/title>');
       
    37         } catch(\Exception $e) {
       
    38             return null;
       
    39         }
    30     }
    40     }
    31 
    41 
    32     public function getPublishers() {
    42     public function getPublishers() {
    33         return $this->innerDocument->allLiterals('dc11:publisher');
    43         try {
       
    44             return $this->allLiterals('dc11:publisher');
       
    45         } catch(\Exception $e) {
       
    46            return [];
       
    47        }
    34     }
    48     }
    35 
    49 
    36     public function getMediaArray() {
    50     public function getMediaArray() {
    37 
    51 
    38         //TODO: add media type
    52         //TODO: add media type
    39         $res = [];
    53         $res = [];
    40         $formats = $this->innerDocument->allResources("dc:isFormatOf");
    54         $formats = [];
       
    55         try {
       
    56             $formats = $this->allResources("dc:isFormatOf");
       
    57         } catch(\Exception $e) {
       
    58             // do nothing
       
    59         }
    41         foreach ($formats as $f) {
    60         foreach ($formats as $f) {
    42             $uri = $f->getUri();
    61             $uri = $f->getUri();
    43             $mimetype = Utils::get_mimetype($uri);
    62             $mimetype = Utils::get_mimetype($uri);
    44             array_push($res, ["url" => $uri, "format" => $mimetype]);
    63             array_push($res, ["url" => $uri, "format" => $mimetype]);
    45         }
    64         }
    46         array_push($res, ["url" => $this->innerDocument->getUri(), "format" => $this->innerDocument->getLiteral('dc11:format')]);
    65 
       
    66         $format = null;
       
    67         try {
       
    68             $format = $this->getLiteral('dc11:format');
       
    69         } catch(\Exception $e) {
       
    70             // do nothing
       
    71         }
       
    72         array_push($res, ["url" => $this->getUri(), "format" => $format]);
    47         return $res;
    73         return $res;
    48     }
    74     }
    49 
    75 
    50     public function getTypes() {
    76     public function getTypes() {
    51         return $this->innerDocument->all('dc11:type');
    77         return $this->all('dc11:type');
    52     }
    78     }
    53 
    79 
    54     public function getDiscourseTypes() {
    80     public function getDiscourseTypes() {
    55         return array_values(array_filter($this->getTypes(), function($v) {
    81         return array_values(array_filter($this->getTypes(), function($v) {
    56             return $v instanceof \EasyRdf_Literal && $v->getDatatypeUri() === Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
    82             return $v instanceof Literal && $v->getDatatypeUri() === Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
    57         }));
    83         }));
    58     }
    84     }
    59 
    85 
    60     public function getOtherTypes() {
    86     public function getOtherTypes() {
    61         $res = array_values(array_filter($this->getTypes(), function($v) {
    87         $res = array_values(array_filter($this->getTypes(), function($v) {
    62             return $v instanceof \EasyRdf_Resource || $v->getDatatypeUri() !== Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
    88             return $v instanceof Resource || $v->getDatatypeUri() !== Config::get('constants.OLAC_DISCOURSE_TYPE')['uri'];
    63         }));
    89         }));
    64         return $res;
    90         return $res;
    65     }
    91     }
    66 
    92 
    67     public function updateDiscourseTypes(array $discoursesTypes) {
    93     public function updateDiscourseTypes(array $discoursesTypes) {
    68 
    94 
    69         $this->startDelta();
    95         $this->startDelta();
    70 
    96 
    71         foreach($this->getDiscourseTypes() as $discourseType) {
    97         foreach($this->getDiscourseTypes() as $discourseType) {
    72             $this->innerDocument->delete('dc11:type', $discourseType);
    98             $this->delete('dc11:type', $discourseType);
    73             $this->currentDelta->deletedGraph->add($this->innerDocument, 'dc11:type', $discourseType);
    99             $this->currentDelta->getDeletedGraph()->add($this, 'dc11:type', new Literal($discourseType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
    74         }
   100         }
    75         // re-add them
   101         // re-add them
    76 
   102 
    77         foreach($discoursesTypes as $dType) {
   103         foreach($discoursesTypes as $dType) {
    78             $this->innerDocument->add('dc11:type', new \EasyRdf_Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
   104             $this->add('dc11:type', new Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
    79             $this->currentDelta->addedGraph->add($this->innerDocument, 'dc11:type', $discourseType);
   105             $this->currentDelta->getAddedGraph()->add($this, 'dc11:type', new Literal($dType, null, Config::get('constants.OLAC_DISCOURSE_TYPE')['uri']));
    80         }
   106         }
    81     }
   107     }
    82 
   108 
    83     public function isIsomorphic($doc) {
   109     public function isIsomorphic($doc) {
    84         return \EasyRdf_Isomorphic::isomorphic($this->graph, $doc->graph);
   110         return Isomorphic::isomorphic($this->graph, $doc->graph);
    85     }
   111     }
    86 
   112 
    87     /*
   113     /*
    88      * Clone document.
   114      * Clone document.
    89      * clone also the innerDocumenent
   115      * clone also the innerDocumenent
    90      */
   116      */
    91     public function __clone() {
   117     public function __clone() {
    92 
   118 
    93         $this->graph = new \EasyRdf_Graph($this->graph->getUri(), $this->graph->toRdfPhp());
   119         $this->graph = new Graph($this->graph->getUri(), $this->graph->toRdfPhp());
    94         $this->innerDocument = $this->getResource('foaf:primaryTopic');
   120     }
       
   121 
       
   122     public function jsonSerialize() {
       
   123         if(!$this->graph) {
       
   124             return [
       
   125                 'id' => $this->getId(),
       
   126             ];
       
   127         } else {
       
   128             $mediaArray = array_map(
       
   129                 function($m) {
       
   130                     $f = Utils::process_literal_or_string($m['format']);
       
   131                     return ['url' => $m['url'], 'format' => $f];},
       
   132                 $this->getMediaArray()
       
   133             );
       
   134 
       
   135             $publishers = array_map(
       
   136                 function($v) { return Utils::process_literal_or_string($v); },
       
   137                 $this->getPublishers()
       
   138             );
       
   139 
       
   140             return [
       
   141                 'id' => $this->getId(),
       
   142                 'uri' => $this->getUri(),
       
   143                 'title' => $this->getTitle()->getValue(),
       
   144                 'publishers' => $publishers,
       
   145                 'mediaArray'=> $mediaArray
       
   146             ];
       
   147         }
    95     }
   148     }
    96 
   149 
    97 }
   150 }