t('Debug OAI'), 'page callback' => 'oai_debug', //'page arguments' => array('glossaire_auto_admin_settings'), 'access callback'=>'user_access', 'access arguments' => array('oieau_admin'), 'file' => 'oai.inc', 'file path' => drupal_get_path('module', 'oai'), ); $items['admin/config/oieau/oai'] = array( 'title'=>'OAI', 'page callback' => 'drupal_get_form', 'page arguments' => array('oai_admin_settings'), 'access callback'=>'user_access', 'access arguments' => array('oieau_admin'), 'type' => MENU_LOCAL_TASK, 'file' => 'oai.inc', 'file path' => drupal_get_path('module', 'oai') ); $items['admin/config/oieau/oai/export-all'] = array ( 'title' => 'Force the export of all contents to OAI the store', 'page callback' => 'oai_admin_export_all', 'access callback'=>'user_access', 'access arguments' => array('oieau_admin'), 'type' => MENU_LOCAL_TASK, 'file' => 'oai.inc', 'file path' => drupal_get_path('module', 'oai') ); $items['admin/config/oieau/oai/export-pending'] = array ( 'title' => 'Export all pending contents to OAI the store', 'page callback' => 'oai_admin_export_pending', 'access callback'=>'user_access', 'access arguments' => array('oieau_admin'), 'type' => MENU_LOCAL_TASK, 'file' => 'oai.inc', 'file path' => drupal_get_path('module', 'oai') ); $items['admin/config/oieau/oai/export-flush'] = array ( 'title' => 'Flush OAI cache', 'page callback' => 'oai_flush_cache_page', 'access callback'=>'user_access', 'access arguments' => array('oieau_admin'), 'type' => MENU_LOCAL_TASK, 'file' => 'oai.inc', 'file path' => drupal_get_path('module', 'oai') ); $items['admin/config/oieau/oai/export-delete'] = array ( 'title' => 'Delete OAI cache', 'page callback' => 'oai_delete_cache_page', 'access callback'=>'user_access', 'access arguments' => array('oieau_admin'), 'type' => MENU_LOCAL_TASK, 'file' => 'oai.inc', 'file path' => drupal_get_path('module', 'oai') ); return $items; } function oai_node_export_xml($node, $format='DC'){ $conf = oai_get_conf(); $filename = 'node_'.$node->nid.'.xml'; // Export des fichiers selon le format souhaité if ($format == 'DC'){ $file_path = $conf['oai_store_path'].'/dc'.'/oieau_dc_'.$filename; //file_prepare_directory($dir_path, FILE_CREATE_DIRECTORY); $xml = oai_xml_dc($node); $xml->AsXML($file_path); //entrepôt spécial du portail doc $eaufrance = oai_is_eaufrance($node); if($eaufrance){ $file_path_eaufrance = $conf['oai_store_path'].'/dc_eaufrance'.'/oieau_dc_eaufrance_'.$filename; $xml = oai_xml_dc($node, 'eaufrance'); $xml->AsXML($file_path_eaufrance); } } elseif($format == 'PSE'){ $file_path = $conf['oai_store_path'].'/pse'.'/oieau_pse_'.$filename; //file_prepare_directory($dir_path, FILE_CREATE_DIRECTORY); $xml = oai_xml_pse($node); $xml->AsXML($file_path); //entrepôt spécial du portail doc $eaufrance = oai_is_eaufrance($node); if($eaufrance){ $file_path_eaufrance = $conf['oai_store_path'].'/pse_eaufrance'.'/oieau_pse_eaufrance_'.$filename; $xml = oai_xml_pse($node, 'eaufrance'); $xml->AsXML($file_path_eaufrance); } } return $file_path; } //Fonction de chargement de la configuration du module function oai_get_conf(){ return variable_get('oai_configurations', array()); } //Fonction d'enregistrement de la configuration du module function oai_set_conf($conf){ return variable_set('oai_configurations', $conf); } // Récupération du label du noeud à partir de l'identifiant function oai_getLabelById($nid, $type=NULL, $all=true, $fullDescription=false){ if ($type != ''){ $result = db_query( "SELECT * FROM {node} n WHERE n.nid = :nid AND n.type= :type", array(':nid'=>$nid, ":type"=> $type) ); }else{ $result = db_query( "SELECT * FROM {node} n WHERE n.nid = :nid", array(':nid'=>$nid) ); } $r = $result->fetchAll(); if(!empty($r)){ if($all){ foreach($r as $key=>$obj){ if($fullDescription) $name[$key] = $obj; else $name[$key] = $obj->title; } } else{ if($fullDescription) $name = $r[0]; else $name = $r[0]->title; } } else{ $name=false; } return $name; } // Récupération du label du vocabulaire à partir de l'identifiant function oai_taxonomy_getLabelById($tid, $vid=NULL, $all=true, $fullDescription=false){ if ($vid != ''){ $result = db_query( "SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid AND t.vid=:vid", array(':tid'=>$tid, ":vid"=> $vid) ); }else{ $result = db_query( "SELECT * FROM {taxonomy_term_data} t WHERE t.tid = :tid", array(':tid'=>$tid) ); } $r = $result->fetchAll(); if(!empty($r)){ if($all){ foreach($r as $key=>$obj){ if($fullDescription) $name[$key] = $obj; else $name[$key] = $obj->name; } } else{ if($fullDescription) $name = $r[0]; else $name = $r[0]->name; } } else{ $name=false; } return $name; } // Recherche du vid de la taxonomy // $label = (string) Nom du vocabulaire function oai_taxonomy_getVidByLabel($label){ $result = db_query( "SELECT * FROM {taxonomy_vocabulary} t WHERE t.machine_name LIKE :label", array(':label'=>$label) ); $r = $result->fetchAll(); if(!empty($r)){ $vid = $r[0]->vid; }else{ $vid = false; } return $vid; } function oai_xml_dc($node, $finalite = null){ //dsm($node); /* if($node->language!=''){$lang = $node->language;} else{$lang = 'und';} */ $lang = 'und'; $sxml = ""; $xml = new SimpleXMLElement($sxml); $title = $node->title .($node->field_version_doc_oieau[$lang][0]['value']?' - '.$node->field_version_doc_oieau[$lang][0]['value']:''); $xml->addChild('dc:title', $title, 'http://purl.org/dc/elements/1.1/'); if (!empty($node->field_dc_description[$lang][0]['value'])){ $xml->addChild('dc:description', str_replace('&', 'et', $node->field_dc_description[$lang][0]['value']), 'http://purl.org/dc/elements/1.1/'); } //===Mots clés if($node->field_dc_subject[$lang][0]['value']!=''){ $xml->addChild('dc:subject', $node->field_dc_subject[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } if(!empty($node->field_themes_oieau[$lang])){ $vid = oai_taxonomy_getVidByLabel('themes_oieau'); foreach($node->field_themes_oieau[$lang] as $subject){ $theme = oai_taxonomy_getLabelById(intval($subject['tid']), $vid, false, false); if($theme!=''){ $xml->addChild('dc:subject', $theme, 'http://purl.org/dc/elements/1.1/'); } } } if(!empty($node->field_keywords_dpsir[$lang])){ $vid = oai_taxonomy_getVidByLabel('dpsir'); foreach($node->field_keywords_dpsir[$lang] as $subject){ $theme = oai_taxonomy_getLabelById(intval($subject['tid']), $vid, false, false); if($theme!=''){ $xml->addChild('dc:subject', $theme, 'http://purl.org/dc/elements/1.1/'); } } } //===Auteurs if(!empty($node->field_dc_creator[$lang])){ foreach($node->field_dc_creator[$lang] as $creator){ $xml->addChild('dc:creator', $creator['value'], 'http://purl.org/dc/elements/1.1/'); } } if($node->field_dc_publisher[$lang][0]['value']!=''){ foreach($node->field_dc_publisher[$lang] as $publisher){ if($publisher['value']!='') $xml->addChild('dc:publisher', $publisher['value'], 'http://purl.org/dc/elements/1.1/'); } } if($node->field_dc_contributor[$lang][0]['value']!=''){ foreach($node->field_dc_contributor[$lang] as $contributor){ if($contributor['value']!='') $xml->addChild('dc:contributor', $contributor['value'], 'http://purl.org/dc/elements/1.1/'); } } if(isset($node->field_dc_source[$lang])){ $xml->addChild('dc:source', $node->field_dc_source[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } //===Dates if(isset($node->field_dc_created[$lang]) && $node->field_dc_created[$lang][0]['value']!=''){ $xml->addChild('dc:created', substr($node->field_dc_created[$lang][0]['value'],0, 10), 'http://purl.org/dc/elements/1.1/'); } if(isset($node->field_dc_modified[$lang]) && $node->field_dc_modified[$lang][0]['value']!=''){ $xml->addChild('dc:modified', substr($node->field_dc_modified[$lang][0]['value'],0, 10), 'http://purl.org/dc/elements/1.1/'); } if(isset($node->field_dc_accepted[$lang]) && $node->field_dc_accepted[$lang][0]['value']!=''){ $xml->addChild('dc:accepted', substr($node->field_dc_accepted[$lang][0]['value'],0, 10), 'http://purl.org/dc/elements/1.1/'); } //===Type & format if(isset($node->field_dc_type[$lang]) && $node->field_dc_type[$lang][0]['value']!=''){ $xml->addChild('dc:type', $node->field_dc_type[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } if(isset($node->field_dc_format[$lang]) && $node->field_dc_format[$lang][0]['value']!=''){ $xml->addChild('dc:format', $node->field_dc_format[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } //Identifier $xml->addChild('dc:identifier', $node->field_urn_oieau[$lang][0]['url'], 'http://purl.org/dc/elements/1.1/'); if($node->field_document_distant[$lang]){ foreach($node->field_document_distant[$lang] as $ext){ if($finalite =='eaufrance'){ if(strtolower(substr( $ext['url'],-4))=='.pdf'){ $ext_doc = $xml->addChild('dc:identifier', $ext['url'], 'http://purl.org/dc/elements/1.1/'); //$ext_doc->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } else{ $ext_doc = $xml->addChild('dc:identifier', $ext['url'], 'http://purl.org/dc/elements/1.1/'); //$ext_doc->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } } if(isset($node->field_file[$lang])){ foreach($node->field_file[$lang] as $file){ $f = file_load($file['fid']); if($f->uri!=''){ $url = file_create_url($f->uri); if($finalite =='eaufrance'){ if(strtolower(substr( $url,-4))=='.pdf'){ $file = $xml->addChild('dc:identifier', $url, 'http://purl.org/dc/elements/1.1/'); //$ext_doc->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } else{ $file = $xml->addChild('dc:identifier', $url, 'http://purl.org/dc/elements/1.1/'); //$file->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } } } if(isset($node->field_dc_coverage[$lang])){ $xml->addChild('dc:coverage', $node->field_dc_coverage[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } if(isset($node->field_dc_rigths[$lang])){ $xml->addChild('dc:rights', $node->field_dc_rigths[$lang][0]['url'], 'http://purl.org/dc/elements/1.1/'); } $xml->addChild('dc:language', 'fra', 'http://purl.org/dc/elements/1.1/'); return $xml; } function oai_xml_pse($node, $finalite = null){ $lang = 'und'; $sxml = ''; $xml = new SimpleXMLElement($sxml); $xml->addChild('dc:title', $node->title, 'http://purl.org/dc/elements/1.1/'); if (!empty($node->field_dc_description[$lang][0]['value'])){ $resume = $xml->addChild('dc:description', str_replace('&', 'et', $node->field_dc_description[$lang][0]['value']), 'http://purl.org/dc/elements/1.1/'); $resume->addAttribute('xsi:type', 'oai_pse:Resume', 'http://www.w3.org/2001/XMLSchema-instance'); } //===Dates if(isset($node->field_dc_created[$lang]) && $node->field_dc_created[$lang][0]['value']!=''){ $xml->addChild('dc:created', substr($node->field_dc_created[$lang][0]['value'],0, 10), 'http://purl.org/dc/elements/1.1/'); } if(isset($node->field_dc_modified[$lang]) && $node->field_dc_modified[$lang][0]['value']!=''){ $xml->addChild('dc:modified', substr($node->field_dc_modified[$lang][0]['value'],0, 10), 'http://purl.org/dc/elements/1.1/'); } if(isset($node->field_dc_accepted[$lang]) && $node->field_dc_accepted[$lang][0]['value']!=''){ $xml->addChild('dc:accepted', substr($node->field_dc_accepted[$lang][0]['value'],0, 10), 'http://purl.org/dc/elements/1.1/'); $xml->addChild('dct:issued', substr($node->field_dc_accepted[$lang][0]['value'],0, 10), 'http://purl.org/dc/terms/'); } //===Mots clés if($node->field_dc_subject[$lang][0]['value']!=''){ $th = $xml->addChild('dc:subject', $node->field_dc_subject[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); //$th->addAttribute('xsi:type', 'oai_pse:Theme', 'http://www.w3.org/2001/XMLSchema-instance'); } if(!empty($node->field_themes_oieau[$lang])){ $vid = oai_taxonomy_getVidByLabel('themes_oieau'); foreach($node->field_themes_oieau[$lang] as $subject){ $theme = oai_taxonomy_getLabelById(intval($subject['tid']), $vid, false, false); if($theme!=''){ $xml->addChild('dc:subject', $theme, 'http://purl.org/dc/elements/1.1/'); } } } if(!empty($node->field_keywords_dpsir[$lang])){ $vid = oai_taxonomy_getVidByLabel('dpsir'); foreach($node->field_keywords_dpsir[$lang] as $subject){ $theme = oai_taxonomy_getLabelById(intval($subject['tid']), $vid, false, false); if($theme!=''){ $xml->addChild('dc:subject', $theme, 'http://purl.org/dc/elements/1.1/'); } } } //===Auteurs if(!empty($node->field_dc_creator[$lang])){ foreach($node->field_dc_creator[$lang] as $creator){ $xml->addChild('dc:creator', $creator['value'], 'http://purl.org/dc/elements/1.1/'); } } if($node->field_dc_publisher[$lang][0]['value']!=''){ foreach($node->field_dc_publisher[$lang] as $publisher){ if($publisher['value']!='') $xml->addChild('dc:publisher', $publisher['value'], 'http://purl.org/dc/elements/1.1/'); } } //Métadonnées $pub = $xml->addChild('dc:publisher', "Sandre", 'http://purl.org/dc/elements/1.1/'); $pub->addAttribute('xsi:type', 'oai_pse:MetaDiffuseur', 'http://www.w3.org/2001/XMLSchema-instance'); if($node->field_dc_contributor[$lang][0]['value']!=''){ foreach($node->field_dc_contributor[$lang] as $contributor){ if($contributor['value']!='') $xml->addChild('dc:contributor', $contributor['value'], 'http://purl.org/dc/elements/1.1/'); } } if(isset($node->field_dc_source[$lang])){ $xml->addChild('dc:source', $node->field_dc_source[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } //Identifier if($node->field_urn_oieau[$lang][0]['url']!=''){ //URN $xml->addChild('dc:identifier', $node->field_urn_oieau[$lang][0]['title'], 'http://purl.org/dc/elements/1.1/'); //Lien URN notice $metahtml = $xml->addChild('dc:identifier', $node->field_urn_oieau[$lang][0]['url'], 'http://purl.org/dc/elements/1.1/'); $metahtml->addAttribute('xsi:type', 'oai_pse:MetaHTML', 'http://www.w3.org/2001/XMLSchema-instance'); } else{ $metahtml = $xml->addChild('dc:identifier', url('node/'.$node->nid, array('absolute'=>true)), 'http://purl.org/dc/elements/1.1/'); $metahtml->addAttribute('xsi:type', 'oai_pse:MetaHTML', 'http://www.w3.org/2001/XMLSchema-instance'); } //Cas particulier de la finalité "eaufrance" on ne garde que les pdf if($node->field_document_distant[$lang]){ foreach($node->field_document_distant[$lang] as $ext){ if($finalite =='eaufrance'){ if(strtolower(substr( $ext['url'],-4))=='.pdf'){ $ext_doc = $xml->addChild('dc:identifier', $ext['url'], 'http://purl.org/dc/elements/1.1/'); $ext_doc->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } else{ $ext_doc = $xml->addChild('dc:identifier', $ext['url'], 'http://purl.org/dc/elements/1.1/'); $ext_doc->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } } if(isset($node->field_file[$lang])){ foreach($node->field_file[$lang] as $file){ $f = file_load($file['fid']); if($f->uri!=''){ $url = file_create_url($f->uri); if($finalite =='eaufrance'){ if(strtolower(substr( $url,-4))=='.pdf'){ $ext_doc = $xml->addChild('dc:identifier', $url, 'http://purl.org/dc/elements/1.1/'); $ext_doc->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } else{ $file = $xml->addChild('dc:identifier', $url, 'http://purl.org/dc/elements/1.1/'); $file->addAttribute('xsi:type', 'dct:URI', 'http://www.w3.org/2001/XMLSchema-instance'); } } } } //Couverture geo if(isset($node->field_dc_coverage[$lang])){ if($finalite =='eaufrance' && strtolower($node->field_dc_coverage[$lang][0]['value'])=='france entière'){ $spatial = $xml->addChild('dct:spatial', 'FRA', 'http://purl.org/dc/terms/'); $spatial->addAttribute('xsi:type', 'oai_pse:CodeNational', 'http://www.w3.org/2001/XMLSchema-instance'); } else{ $xml->addChild('dc:coverage', $node->field_dc_coverage[$lang][0]['value'], 'http://purl.org/dc/elements/1.1/'); } } $language = $xml->addChild('dc:language', 'fra', 'http://purl.org/dc/elements/1.1/'); $language->addAttribute('xsi:type', 'dct:ISO639-3', 'http://www.w3.org/2001/XMLSchema-instance'); return $xml; } function oai_entity_insert($entity, $type) { // For our purposes there's really no difference between insert and update. return oai_entity_update($entity, $type); } function oai_entity_update($entity, $type){ list($id, $vid, $bundle) = entity_extract_ids($type, $entity); if($type=='node'){ if (oai_entity_should_export($entity, $type)) { //Generation de la notice OAI $path_dc = oai_node_export_xml($entity, 'DC'); $path_pse = oai_node_export_xml($entity, 'PSE'); // If we haven't seen this entity before it may not be there, so merge // instead of update. db_merge('oai') ->key(array( 'entity_type' => $type, 'entity_id' => $id, )) ->fields(array( 'bundle' => $bundle, 'status' => 1, 'changed' => REQUEST_TIME, 'dc_path' => $path_dc, 'pse_path' =>$path_pse, )) ->execute(); } else{ db_merge('oai') ->key(array( 'entity_type' => $type, 'entity_id' => $id, )) ->fields(array( 'bundle' => $bundle, 'status' => -1, 'changed' => REQUEST_TIME, 'dc_path' => '', 'pse_path' =>'', )) ->execute(); } } } function oai_entity_delete($entity, $entity_type) { list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity); if($entity_type=='node'){ //Delete files in data store $r = db_select('oai', 'oai') ->fields('oai') ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->execute() ->fetchAssoc(); if($r){ unlink($r['dc_path']); unlink($r['pse_path']); } //Delete the line in the DB db_delete('oai') ->condition('entity_type', $entity_type) ->condition('entity_id', $id) ->execute(); } } //Permet de savoir une notice est exportable ou pas function oai_entity_should_export($entity, $type) { $conf = oai_get_conf(); list($id, $vid, $bundle) = entity_extract_ids($type, $entity); if ($bundle && isset($conf['content_type']) && in_array($bundle, $conf['content_type'])) { return TRUE; //Vérification de la phase de validation de la notice doc //Si validée alors on peut l'exporter /* if(isset($entity->field_phase_notice)){ if(!empty($entity->field_phase_notice) && $entity->field_phase_notice['und'][0]['value']=='Validée'){ //On vérifie que le champ "field_dc_identifier" commence par OIE/ ou par INIST/ IE/ ou RIOB if(isset($entity->field_dc_identifier)){ if(!empty($entity->field_dc_identifier) && $entity->field_dc_identifier['und'][0]['value']!='') { $dc_identifier_type = explode('/', str_replace('-', '/',strtoupper( $entity->field_dc_identifier['und'][0]['value']))); if(in_array($dc_identifier_type[0], array('OIE', 'INIST', 'IE', 'RIOB'))){return TRUE;} else{return FALSE;} } else{return FALSE;} } else{ return TRUE; } } else{return FALSE;} } else{ return TRUE; } */ } return FALSE; } //Permet de savoir si une notice doit être intégrée à l'entrepot eaufrance pour le portail documentaire function oai_is_eaufrance($node){ //Pour aller dans eaufrance, le type de document doit être un dictionnaire et il doit être "valide" //Si dico if(isset($node->field_dc_type) && !empty($node->field_dc_type) && in_array( strtolower($node->field_dc_type['und'][0]['value']), array('dictionnaire'))){ if($node->field_statut_doc_oieau['und'][0]['value']=='Validé'){ return true; } } return false; }