server/src/app/Libraries/Mappers/CocoonAbstractRdfMapper.php
changeset 18 f2a40bbc27f6
child 19 eadaf0b8f02e
equal deleted inserted replaced
17:ac3dc090e987 18:f2a40bbc27f6
       
     1 <?php
       
     2 namespace CorpusParole\Libraries\Mappers;
       
     3 
       
     4 use Config;
       
     5 
       
     6 use CorpusParole\Libraries\Utils;
       
     7 use CorpusParole\Libraries\CocoonUtils;
       
     8 
       
     9 use EasyRdf\RdfNamespace;
       
    10 use EasyRdf\Graph;
       
    11 use EasyRdf\Literal;
       
    12 use EasyRdf\Resource;
       
    13 
       
    14 
       
    15 RdfNamespace::set('edm', 'http://www.europeana.eu/schemas/edm/');
       
    16 RdfNamespace::set('ore', 'http://www.openarchives.org/ore/terms/');
       
    17 RdfNamespace::set('crdo', 'http://crdo.risc.cnrs.fr/schemas/');
       
    18 RdfNamespace::set('olac', 'http://www.language-archives.org/OLAC/1.1/');
       
    19 RdfNamespace::set('skos', 'http://www.w3.org/2004/02/skos/core#');
       
    20 
       
    21 
       
    22 /**
       
    23  *
       
    24  */
       
    25 abstract class CocoonAbstractRdfMapper implements RdfMapper {
       
    26 
       
    27     public function __construct(Graph $inputGraph) {
       
    28         $this->inputGraph = $inputGraph;
       
    29         $this->uri = $inputGraph->getUri();
       
    30         $this->outputGraphes = [];
       
    31     }
       
    32 
       
    33     abstract protected function mapResource($res, $outputGraph);
       
    34     abstract protected function getResourceBaseId($res=null);
       
    35 
       
    36     public function getOutputGraphes() {
       
    37         return $this->outputGraphes;
       
    38     }
       
    39 
       
    40     /**
       
    41      * Map a Cocoon Rdf Graph to a CP graph
       
    42      * @return the new mapped graph
       
    43      */
       
    44     public function mapGraph() {
       
    45 
       
    46         $outputGraph = $this->buildOutputGraph();
       
    47 
       
    48         $resources = $this->inputGraph->allOfType('crdo:Resource');
       
    49         foreach ($resources as $res) {
       
    50             $this->mapResource($res, $outputGraph);
       
    51         }
       
    52     }
       
    53 
       
    54     /**
       
    55      * Build the main outputgraph
       
    56      * @return the new empty graph
       
    57      */
       
    58      protected function buildOutputGraph() {
       
    59 
       
    60          //$soundRes = $this->inputGraph->resource($this->uri)->get('http://purl.org/dc/terms/requires');
       
    61          $id = CocoonUtils::getIdFromUri($this->getResourceBaseId());
       
    62 
       
    63          $outputGraph = new Graph(CocoonUtils::getCorpusUriFromId($id));
       
    64          $this->outputGraphes[$this->getResourceBaseId()] = $outputGraph;
       
    65 
       
    66          return $outputGraph;
       
    67      }
       
    68 
       
    69 
       
    70     protected function propertyOlacRoleMap($targetRes, $prop, $value) {
       
    71         $targetRes->add($prop, $value);
       
    72         $targetRes->add('http://purl.org/dc/elements/1.1/contributor', $value);
       
    73     }
       
    74 
       
    75     protected function propertyIdentityMap($targetRes, $prop, $value) {
       
    76         $targetRes->add($prop, $value);
       
    77     }
       
    78 
       
    79     protected function propertyTrimMap($providedCHOResource, $prop, $value) {
       
    80         $providedCHOResource->add($prop, trim($value));
       
    81     }
       
    82 
       
    83     protected function addSpatialProperties($destRes, $res, $outputGraph) {
       
    84         $spatials = $res->all($this->inputGraph->resource('http://purl.org/dc/terms/spatial'));
       
    85         $lats = $res->all($this->inputGraph->resource('http://www.w3.org/2003/01/geo/wgs84_pos#lat'));
       
    86         $longs = $res->all($this->inputGraph->resource('http://www.w3.org/2003/01/geo/wgs84_pos#long'));
       
    87         $alts = $res->all($this->inputGraph->resource('http://www.w3.org/2003/01/geo/wgs84_pos#alt'));
       
    88 
       
    89         if( count($spatials) > 0 || count($lats) > 0 || count($longs) > 0 || count($alts)) {
       
    90             $placeNode = $outputGraph->newBNode(['edm:Place']);
       
    91             foreach($lats as $latitude) {
       
    92                 $placeNode->addLiteral('http://www.w3.org/2003/01/geo/wgs84_pos#lat', $latitude);
       
    93             }
       
    94             foreach($longs as $longitude) {
       
    95                 $placeNode->addLiteral('http://www.w3.org/2003/01/geo/wgs84_pos#long', $longitude);
       
    96             }
       
    97             foreach($spatials as $s) {
       
    98                 if($s instanceof Literal) {
       
    99                     $placeNode->addLiteral('skos:note', $s);
       
   100                 }
       
   101                 elseif ($s instanceof Resource) {
       
   102                     $placeNode->addResource('owl:sameAs', $s);
       
   103                 }
       
   104             }
       
   105             $destRes->add($outputGraph->resource('http://purl.org/dc/terms/spatial'), $placeNode);
       
   106         }
       
   107     }
       
   108 
       
   109     protected function applyPropertiesToRes($sourceRes, $targetRes, $properties) {
       
   110 
       
   111         foreach( $properties as list($prop, $mappingMethod)) {
       
   112             if( is_null($mappingMethod) ) {
       
   113                 $mappingMethod = 'propertyIdentityMap';
       
   114             }
       
   115 
       
   116             foreach($sourceRes->all($this->inputGraph->resource($prop)) as $value) {
       
   117                 call_user_func(array($this, $mappingMethod), $targetRes, $prop, $value);
       
   118             }
       
   119         }
       
   120     }
       
   121 
       
   122     protected function addDateProperties($targetRes, $res, $outputGraph) {
       
   123         $this->applyPropertiesToRes($res, $targetRes, [
       
   124             ['http://purl.org/dc/terms/available', null],
       
   125             ['http://purl.org/dc/terms/created', null],
       
   126             ['http://purl.org/dc/terms/issued', null],
       
   127             ['http://purl.org/dc/terms/modified', null],
       
   128         ]);
       
   129     }
       
   130 
       
   131     protected function mapCollections($res) {
       
   132         $collections = $res->all('dc:isPartOf');
       
   133         foreach($collections as $coll) {
       
   134             if($coll instanceof Resource) {
       
   135                 $collectionGraph = new Graph($coll->getUri());
       
   136                 $this->outputGraphes[$coll->getUri()] = $collectionGraph;
       
   137                 $collectionGraph->resource($coll->getUri(), 'edm:Collection');
       
   138             }
       
   139         }
       
   140     }
       
   141 
       
   142 
       
   143 }