=') && extension_loaded('xsl')) { if (substr(phpversion(), 0, 1) == "5") @ini_set("zend.ze1_compatibility_mode", "0"); require_once($include_path.'/xslt-php4-to-php5.inc.php'); } class oai extends connector { //Variables internes pour la progression de la récupération des notices var $callback_progress; //Nom de la fonction de callback progression passée par l'appellant var $current_set; //Set en cours de synchronisation var $total_sets; //Nombre total de sets sélectionnés var $metadata_prefix; //Préfixe du format de données courant var $source_id; //Numéro de la source en cours de synchro var $n_recu; //Nombre de notices reçues var $xslt_transform; //Feuille xslt transmise var $sets_names; //Nom des sets pour faire plus joli !! var $del_old; //Supression ou non des notices dejà existantes //Résultat de la synchro var $error; //Y-a-t-il eu une erreur var $error_message; //Si oui, message correspondant function oai($connector_path="") { parent::connector($connector_path); } function get_id() { return "oai"; } //Est-ce un entrepot ? function is_repository() { return 1; } function unserialize_source_params($source_id) { $params=$this->get_source_params($source_id); if ($params["PARAMETERS"]) { $vars=unserialize($params["PARAMETERS"]); $params["PARAMETERS"]=$vars; } return $params; } function source_get_property_form($source_id) { global $charset; $params=$this->get_source_params($source_id); if ($params["PARAMETERS"]) { //Affichage du formulaire avec $params["PARAMETERS"] $vars=unserialize($params["PARAMETERS"]); foreach ($vars as $key=>$val) { global $$key; $$key=$val; } } $form="
"; if (!$url) $form.="

".$this->msg["rec_addr"]."

"; else { //Intérogation du serveur $oai_p=new oai20($url,$charset,$params["TIMEOUT"]); if ($oai_p->error) { $form.="

".sprintf($this->msg["error_contact_server"],$oai_p->error_message)."

"; } else { $form.="

".$oai_p->repositoryName."

"; if ($oai_p->description) $form.="
".htmlentities($oai_p->description,ENT_QUOTES,$charset)."
"; $form.="
".formatdate($oai_p->earliestDatestamp)."
".$oai_p->adminEmail."
".($oai_p->granularity=="YYYY-MM-DD"?$this->msg["oai_one_day"]:$this->msg["oai_minute"])."
"; if ($oai_p->has_feature("SETS")) { $form.="
"; if (count($oai_p->sets)<80) $combien = count($oai_p->sets); else $combien=80; $form.="
"; } $form.="
".$this->msg["oai_xslt_file"]." "; if ($xsl_transform) $form.="
".sprintf($this->msg["oai_xslt_file_linked"],$xsl_transform["name"])." : ".$this->msg["oai_del_xslt_file"]." "; $form.="
"; if (($oai_p->deletedRecord=="persistent")||($oai_p->deletedRecord=="transient")) { $form.="
"; } } } $form.="
"; return $form; } function make_serialized_source_properties($source_id) { global $url,$clean_base_url,$sets,$formats,$del_deleted,$del_xsl_transform; $t["url"]=stripslashes($url); $t["clean_base_url"]=$clean_base_url; $t["sets"]=$sets; $t["formats"]=$formats; $t["del_deleted"]=$del_deleted; //Vérification du fichier if (($_FILES["xslt_file"])&&(!$_FILES["xslt_file"]["error"])) { $xslt_file_content=array(); $xslt_file_content["name"]=$_FILES["xslt_file"]["name"]; $xslt_file_content["code"]=file_get_contents($_FILES["xslt_file"]["tmp_name"]); $t["xsl_transform"]=$xslt_file_content; } else if ($del_xsl_transform) { $t["xsl_transform"]=""; } else { $oldparams=$this->get_source_params($source_id); if ($oldparams["PARAMETERS"]) { //Anciens paramètres $oldvars=unserialize($oldparams["PARAMETERS"]); } $t["xsl_transform"] = $oldvars["xsl_transform"]; } $this->sources[$source_id]["PARAMETERS"]=serialize($t); } //Récupération des proriétés globales par défaut du connecteur (timeout, retry, repository, parameters) function fetch_default_global_values() { $this->timeout=5; $this->repository=1; $this->retry=3; $this->ttl=1800; $this->parameters=""; } //Formulaire des propriétés générales function get_property_form() { $this->fetch_global_properties(); return ""; } function make_serialized_properties() { $this->parameters=""; } function progress($query,$token) { $callback_progress=$this->callback_progress; if ($token["completeListSize"]) { $percent=($this->current_set/$this->total_sets)+(($token["cursor"]/$token["completeListSize"])/$this->total_sets); $nlu=$this->n_recu; $ntotal="inconnu"; //$nlu=$token["cursor"]; //$ntotal=$token["completeListSize"]; } else { $percent=($this->current_set/$this->total_sets); $nlu=$this->n_recu; $ntotal="inconnu"; } call_user_func($callback_progress,$percent,$nlu,$ntotal); } function rec_record($record) { global $charset,$base_path; $rec=new oai_record($record,$charset,$base_path."/admin/connecteurs/in/oai/xslt",$this->metadata_prefix,$this->xslt_transform,$this->sets_names); $rec_uni=$rec->unimarc; if ($rec->error) echo 'erreur!
'; if (!$rec->error) { //On a un enregistrement unimarc, on l'enregistre $rec_uni_dom=new xml_dom($rec_uni,$charset); if (!$rec_uni_dom->error) { //Initialisation $ref=""; $ufield=""; $usubfield=""; $field_order=0; $subfield_order=0; $value=""; $date_import=$rec->header["DATESTAMP"]; $fs=$rec_uni_dom->get_nodes("unimarc/notice/f"); //Recherche du 001 for ($i=0; $iget_datas($fs[$i]); break; } } //Mise à jour if ($ref) { //Si conservation des anciennes notices, on regarde si elle existe if (!$this->del_old) { $requete="select count(*) from entrepot_source_".$this->source_id." where ref='".addslashes($ref)."'"; $rref=mysql_query($requete); if ($rref) $ref_exists=mysql_result($rref,0,0); } //Si pas de conservation des anciennes notices, on supprime if ($this->del_old) { $requete="delete from entrepot_source_".$this->source_id." where ref='".addslashes($ref)."'"; mysql_query($requete); } //Si pas de conservation ou reférence inexistante if (($this->del_old)||((!$this->del_old)&&(!$ref_exists))) { //Insertion de l'entête $n_header["rs"]=$rec_uni_dom->get_value("unimarc/notice/rs"); $n_header["ru"]=$rec_uni_dom->get_value("unimarc/notice/ru"); $n_header["el"]=$rec_uni_dom->get_value("unimarc/notice/el"); $n_header["bl"]=$rec_uni_dom->get_value("unimarc/notice/bl"); $n_header["hl"]=$rec_uni_dom->get_value("unimarc/notice/hl"); $n_header["dt"]=$rec_uni_dom->get_value("unimarc/notice/dt"); //Récupération d'un ID $requete="insert into external_count (recid, source_id) values('".addslashes($this->get_id()." ".$this->source_id." ".$ref)."', ".$this->source_id.")"; $rid=mysql_query($requete); if ($rid) $recid=mysql_insert_id(); foreach($n_header as $hc=>$code) { $requete="insert into entrepot_source_".$this->source_id." (connector_id,source_id,ref,date_import,ufield,usubfield,field_order,subfield_order,value,i_value,recid) values( '".addslashes($this->get_id())."',".$this->source_id.",'".addslashes($ref)."','".addslashes($date_import)."', '".$hc."','',-1,0,'".addslashes($code)."','',$recid)"; mysql_query($requete); } for ($i=0; $iget_nodes("s",$fs[$i]); if (is_array($ss)) { for ($j=0; $jget_datas($ss[$j]); $subfield_order=$j; $requete="insert into entrepot_source_".$this->source_id." (connector_id,source_id,ref,date_import,ufield,usubfield,field_order,subfield_order,value,i_value,recid) values( '".addslashes($this->get_id())."',".$this->source_id.",'".addslashes($ref)."','".addslashes($date_import)."', '".addslashes($ufield)."','".addslashes($usubfield)."',".$field_order.",".$subfield_order.",'".addslashes($value)."', ' ".addslashes(strip_empty_words($value))." ',$recid)"; mysql_query($requete); } } else { $value=$rec_uni_dom->get_datas($fs[$i]); $requete="insert into entrepot_source_".$this->source_id." (connector_id,source_id,ref,date_import,ufield,usubfield,field_order,subfield_order,value,i_value,recid) values( '".addslashes($this->get_id())."',".$this->source_id.",'".addslashes($ref)."','".addslashes($date_import)."', '".addslashes($ufield)."','".addslashes($usubfield)."',".$field_order.",".$subfield_order.",'".addslashes($value)."', ' ".addslashes(strip_empty_words($value))." ',$recid)"; mysql_query($requete); } } } $this->n_recu++; } } } } function cancel_maj($source_id) { return false; } function break_maj($source_id) { return false; } function form_pour_maj_entrepot($source_id,$sync_form="sync_form") { global $charset; global $form_from; global $form_until; global $form_radio; $source_id=$source_id+0; $params=$this->get_source_params($source_id); $vars=unserialize($params["PARAMETERS"]); $datefrom = 0; $oai_p=new oai20($vars['url'],$charset,$params["TIMEOUT"]); if (!$oai_p->error) $earliestdate = strtotime(substr($oai_p->earliestDatestamp, 0, 10)); $sql = " SELECT MAX(UNIX_TIMESTAMP(date_import)) FROM entrepot_source_".$source_id; $res = mysql_result(mysql_query($sql), 0, 0); $datefrom = $res ? $res : $earliestdate; $latest_date_database_string = $res ? formatdate(date("Y-m-d", $res)) : "".$this->msg["oai_syncinfo_nonotice"].""; $dateuntil = ""; $form = "
"; $form .= " ".$this->msg["oai_get_notices"]."

".$this->msg["oai_last_sync"]."
".$this->msg["oai_between_part1"]."
(facultatif)
".$this->msg["oai_between_part2"]."
(facultatif)

"; $form .= sprintf($this->msg["oai_syncinfo_date_serverearlyiest"], formatdate(date("Y-m-d",$earliestdate))); $form .= "
".sprintf($this->msg["oai_syncinfo_date_baserecent"], $latest_date_database_string); $form .= "
"; return $form; } //Nécessaire pour passer les valeurs obtenues dans form_pour_maj_entrepot au javascript asynchrone function get_maj_environnement($source_id) { global $form_from; global $form_until; global $form_radio; $envt=array(); $envt["form_from"]=$form_from; $envt["form_until"]=$form_until; $envt["form_radio"]=$form_radio; return $envt; } function maj_entrepot($source_id,$callback_progress="",$recover=false,$recover_env="") { global $charset; global $form_from, $form_until, $form_radio; $this->callback_progress=$callback_progress; $params=$this->unserialize_source_params($source_id); $p=$params["PARAMETERS"]; $this->metadata_prefix=$p["formats"]; $this->source_id=$source_id; $this->n_recu=0; $this->xslt_transform=$p["xsl_transform"]["code"]; //Connexion $oai20=new oai20($p["url"],$charset,$p["TIMEOUT"],$p["clean_base_url"]); if (!$oai20->error) { if ($recover) { $envt=unserialize($recover_env); $sets=$envt["sets"]; $date_start=$envt["date_start"]; $date_end=$envt["date_end"]; $this->del_old=false; } else { //Affectation de la date de départ if ($form_radio == "last_sync") { //Recherche de la dernière date... $requete="select unix_timestamp(max(date_import)) from entrepot_source_".$source_id." where 1;"; $resultat=mysql_query($requete); if (mysql_num_rows($resultat)) { $last_date=mysql_result($resultat,0,0); if ($last_date) { //En fonction de la granularité, on ajoute une seconde ou un jour ! if ($oai20->granularity=="YYYY-MM-DD") $last_date+=3600*24; else $last_date+=1; } else { $earliest_date=new iso8601($oai20->granularity); $last_date=$earliest_date->iso8601_to_unixtime($oai20->earliestDatestamp); } } else { $earliest_date=new iso8601($oai20->granularity); $last_date=$earliest_date->iso8601_to_unixtime($oai20->earliestDatestamp); } $date_start=$last_date; } else { if ($form_from) $date_start=strtotime($form_from); if ($form_until) $date_end = strtotime($form_until); else $date_end = ''; } //Recherche des sets sélectionnés $this->sets_names=$oai20->sets; for ($i=0; $isets[$p["sets"][$i]]) { $sets[]=$p["sets"][$i]; } } $this->del_old=true; } //Mise à jour de source_sync pour reprise en cas d'erreur $envt["sets"]=$sets; $envt["date_start"]=$date_start; $envt["date_end"]=$date_end; $requete="update source_sync set env='".addslashes(serialize($envt))."' where source_id=".$source_id; mysql_query($requete); //Lancement de la requête $this->current_set=0; $this->total_sets=count($sets); if (count($sets)) { for ($i=0; $icurrent_set=$i; $oai20->list_records($date_start,$date_end,$sets[$i],$p["formats"],array(&$this,"rec_record"),array(&$this,"progress")); if (($oai20->error)&&($oai20->error_oai_code!="noRecordsMatch")) { $this->error=true; $this->error_message.=$oai20->error_message."
"; } } } else { $this->current_set=0; $this->total_sets=1; $oai20->list_records($date_start,$date_end,"",$p["formats"],array(&$this,"rec_record"),array(&$this,"progress")); if (($oai20->error)&&($oai20->error_oai_code!="noRecordsMatch")) { $this->error=true; $this->error_message.=$oai20->error_message."
"; } } } else { $this->error=true; $this->error_message=$oai20->error_message; } return $this->n_recu; } } ?>