profile=_parser_text_no_function_($xml,"AWSCONFIG");
}
public function get_id() {
return "amazon";
}
//Est-ce un entrepot ?
public function is_repository() {
return 2;
}
public 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;
}
}
$sites=$this->profile["SITES"][0]["SITE"];
if (!$url) $url=$sites[0]["COUNTRY"];
if (!$max_return) $max_return=100;
$form="
";
for ($i=0; $i
";
for ($j=0; $j".htmlentities($this->get_libelle($searchindexes[$j]["COMMENT"]),ENT_QUOTES,$charset)."\n";
}
$form.="
";
}
$form.="
";
return $form;
}
public function make_serialized_source_properties($source_id) {
global $url,$response_group,$search_index,$max_return,$review_height,$review_width;
$t["url"]=stripslashes($url);
$t["response_group"]=$response_group;
$t["search_index"]=$search_index;
$t["max_return"]=$max_return;
$t["review_height"]=$review_height ? $review_height : "350px";
$t["review_width"]=$review_width ? $review_width : "450px";
$this->sources[$source_id]["PARAMETERS"]=serialize($t);
}
//Formulaire des propriétés générales
public function get_property_form() {
global $charset;
$this->fetch_global_properties();
//Affichage du formulaire en fonction de $this->parameters
if ($this->parameters) {
$keys = unserialize($this->parameters);
$accesskey= $keys['accesskey'];
$secretkey=$keys['secretkey'];
$associatetag=$keys['associatetag'];
} else {
$accesskey="";
$secretkey="";
$associatetag="";
}
$r="
";
return $r;
}
public function make_serialized_properties() {
global $accesskey, $secretkey, $associatetag;
//Mise en forme des paramètres à partir de variables globales (mettre le résultat dans $this->parameters)
$keys = array();
$keys['accesskey']=$accesskey;
$keys['secretkey']=$secretkey;
$keys['associatetag'] = $associatetag;
$this->parameters = serialize($keys);
}
public function rec_record($record,$source_id,$search_id) {
global $base_path,$url,$search_index;
$date_import=date("Y-m-d H:i:s",time());
//Recherche du 001
$ref=$record["001"][0];
//Mise à jour
if ($ref) {
//Si conservation des anciennes notices, on regarde si elle existe
if (!$this->del_old) {
$ref_exists = $this->has_ref($source_id, $ref, $search_id);
}
//Si pas de conservation des anciennes notices, on supprime
if ($this->del_old) {
$this->delete_from_entrepot($source_id, $ref, $search_id);
$this->delete_from_external_count($source_id, $ref);
}
//Si pas de conservation ou reférence inexistante
if (($this->del_old)||((!$this->del_old)&&(!$ref_exists))) {
//Insertion de l'entête
$n_header=array();
$n_header["rs"]="*";
$n_header["ru"]="*";
$n_header["el"]="1";
$n_header["bl"]="m";
$n_header["hl"]="0";
$n_header["dt"]=$this->types[$search_index[$url][0]];
if (!$n_header["dt"]) $n_header["dt"]="a";
//Récupération d'un ID
$recid = $this->insert_into_external_count($source_id, $ref);
foreach($n_header as $hc=>$code) {
$this->insert_header_into_entrepot($source_id, $ref, $date_import, $hc, $code, $recid, $search_id);
}
$field_order=0;
foreach ($record as $field=>$val) {
for ($i=0; $i$vals) {
for ($j=0; $jinsert_content_into_entrepot($source_id, $ref, $date_import, $field, $sfield, $field_order, $j, $vals[$j], $recid, $search_id);
}
}
} else {
//if ($charset!="utf-8") $vals[$i]=utf8_decode($vals[$i]);
$this->insert_content_into_entrepot($source_id, $ref, $date_import, $field, '', $field_order, 0, $val[$i], $recid, $search_id);
}
$field_order++;
}
}
$this->rec_isbd_record($source_id, $ref, $recid);
}
}
}
public function parse_profile() {
global $url,$search_index;
$matches=$this->profile["MATCHES"][0]["MATCH"];
for ($j=0; $jmatch[$matches[$j]["TYPE"]]=$matches[$j]["CRITERIA"];
}
//Récupération des sites
$this->current_site=false;
for ($i=0; $iprofile["SITES"][0]["SITE"]); $i++) {
if ($this->profile["SITES"][0]["SITE"][$i]["COUNTRY"]==$url) {
$this->current_site=$i;
break;
}
}
$this->searchindexes=$this->profile["SITES"][0]["SITE"][$this->current_site]["SEARCHINDEXES"][0]["SEARCHINDEX"];
//Recherche des critères
for ($i=0; $isearchindexes); $i++) {
if ($this->searchindexes[$i]["TYPE"]==$search_index[$url][0]) {
$this->current_searchindex=$i;
break;
}
}
//Est-ce une recherche POWER ?
if ($this->searchindexes[$this->current_searchindex]["POWER"]=="yes")
$this->match_index="power";
else
$this->match_index="simple";
//Parse des types de document
$types=$this->profile["TYPES"][0]["TYPE"];
for ($i=0; $itypes[$types[$i]["NAME"]]=$types[$i]["DT"];
}
}
public function parse_query($query,$first_call=false) {
global $url,$search_index;
$searchattr = array();
//Si c'est le premier appel du parser, on indique le type de recherche
if ($first_call) $searchattr["TYPE"]=$this->match_index;
//Affectation de la grillle des critères autorisés
$criterias=$this->searchindexes[$this->current_searchindex]["CRITERIA"];
$auth_criterias=array();
for ($i=0; $isub) {
//Enlevons tous les caractères rigolos des ISBNs
if ($query[$i]->ufield == '010$a') {
for($k=0, $acount=count($query[$i]->values); $k<$acount;$k++) {
$query[$i]->values[$k] = preg_replace('/-|\.| /', '', $query[$i]->values[$k]);;
}
}
$af=explode(":",$query[$i]->ufield);
$isid=false;
if (count($af)>1) {
if ($af[0]=="id") $isid=true;
$amf=$af[1];
} else $amf=$af[0];
$amazon_field=array();
$op=$query[$i]->op;
for ($j=0; $jmatch[$this->match_index]); $j++) {
//Est-ce que l'opérateur est exclu ??
$can_translate=false;
if (($this->match[$this->match_index][$j]["EXCLUDEOPERATORS"])&&(in_array($op,explode(",",$this->match[$this->match_index][$j]["EXCLUDEOPERATORS"]))===false)) {
$can_translate=true;
} else if ($this->match[$this->match_index][$j]["OPERATORS"]) {
if (in_array($op,explode(",",$this->match[$this->match_index][$j]["OPERATORS"]))!==false) $can_translate=true;
} else {
$can_translate=true;
}
//Si l'opérateur est autorisé alors on regarde si le critère correspond
if ($can_translate) {
if (in_array($amf,explode(",",$this->match[$this->match_index][$j]["UNIMARCFIELD"]))!==false) {
$azf=$this->match[$this->match_index][$j]["NAME"];
//Le champ est-il autorisé dans cette configuration ?
if ((($this->searchindexes[$this->current_searchindex]["POWER"]!="yes")&&($auth_criterias[$azf]))||($this->searchindexes[$this->current_searchindex]["POWER"]=="yes")) {
$amazon_field[]=$azf;
}
}
if ($this->match[$this->match_index][$j]["UNIMARCFIELD"]=="ALLOTHERS") {
$allothers=$this->match[$this->match_index][$j]["NAME"];
}
}
}
if (!count($amazon_field)) $amazon_field[]=$allothers;
$t=array();
$t["FIELDS"]=$amazon_field;
if ($isid) {
$t["VALUE"][0]=$this->get_values_from_id($query[$i]->values[0],$amf);
} else $t["VALUE"]=$query[$i]->values;
if (($query[$i]->inter)&&($i)) $t["INTER"]=$query[$i]->inter;
$searchattr[]=$t;
} else {
$t["FIELDS"]="";
$t["VALUE"]="";
$t["SUB"]=$this->parse_query($query[$i]->sub);
if (($query[$i]->inter)&&($i)) $t["INTER"]=$query[$i]->inter;
$searchattr[]=$t;
}
}
return $searchattr;
//return $r;
}
public function make_power_query($query) {
$r="";
for ($i=0; $i0)) {
if ($query[$i]["INTER"]=="ex") $inter="and not"; else $inter=$query[$i]["INTER"];
$r.=" ".$inter." ";
}
if ($query[$i]["SUB"])
$r.="(".$this->make_power_query($query[$i]["SUB"]).")";
else {
$expr=array();
for ($j=0; $jarray("PARAM"=>array("Power"=>$r)));
}
public function make_simple_search($query) {
$r=array();
for ($i=0; $i0)) {
$q["INTER"]=$query[$i]["INTER"];
}
if ($query[$i]["SUB"])
$q["SUB"]=$this->make_simple_search($query[$i]["SUB"]);
else {
$expr=array();
for ($j=0; $j1) {
$sub=array();
for ($j=0; $j0) $q1["INTER"]="or";
$q1["PARAM"]=$expr[$j];
$sub[]=$q1;
}
$q["SUB"]=$sub;
} else {
$q["PARAM"]=$expr[0];
}
}
$r[]=$q;
}
return $r;
}
public function store_asins($result,$inter,$first=false,&$n) {
//Lecture et stockage des Items
$asins=array();
$items=$this->soap2array($result["Items"],"Item");
for ($j=0; $jfetch_global_properties();
$parameters = unserialize($this->parameters);
$client->__setSoapHeaders($this->make_soap_headers('ItemSearch'));
$paws["Request"]=array(
"SearchIndex"=>$search_index[$url][0],
"ResponseGroup"=>"ItemIds"
);
$n=0;
for ($i=0; $i$value)
$pawsp["Request"][$rparam]=$value;
$pawsp['AssociateTag'] = $parameters['associatetag'];
try{
$this->sleep_needed();
$result=$client->ItemSearch($pawsp);
if(is_object($result)){
$result=amazon::objectToArrayAndCharset($result);
}
}catch(Exception $e){
$this->error=true;
$this->error_message=$e->getMessage();
}
if (!$this->error) {
$this->store_asins($result,$q[$i]["INTER"],($i==0),$n);
if ($result["Items"]["TotalPages"]>1) {
$npages=$result["Items"]["TotalPages"];
for ($j=2; $j<=$npages; $j++) {
$pawsp["Request"]["ItemPage"]=$j;
try{
$this->sleep_needed();
$result=$client->ItemSearch($pawsp);
if(is_object($result)){
$result=amazon::objectToArrayAndCharset($result);
}
}catch(Exception $e){
$this->error=true;
$this->error_message=$e->getMessage();
break;
}
if(!$this->error){
$this->store_asins($result,$q[$i]["INTER"],($i==0),$n);
}
if ((count($q)==1) and ($n>=$max_return)) break;
}
}
}
} else {
$this->make_search($source_id,$q[$i]["SUB"],$client);
}
}
}
public function soap2array($t,$element) {
$n=0;
$r=array();
if (is_array($t[$element])) {
foreach ($t[$element] as $elt=>$val) {
if ((string)$elt!=(string)$n) {
$r=array();
$r[0]=$t[$element];
break;
} else {
$r[$n]=$val;
$n++;
}
}
} else if ($t[$element]) $r[0]=$t[$element];
return $r;
}
public function amazon_2_uni($item) {
global $charset;
$nt=$item["ItemAttributes"];
$unimarc=array();
$unimarc["001"][0]=$item["ASIN"];
if ($nt["EAN"]) $unimarc["010"][0]["a"][0]=$nt["EAN"];
if ($nt["UPC"]) $unimarc["010"][0]["a"][0]=$nt["UPC"];
if ($nt["ISBN"]) $unimarc["010"][0]["a"][0]=$nt["ISBN"];
//Langue
$langue=$this->soap2array($nt,"Language");
if (count($langue)) {
for ($i=0; $isoap2array($nt,"Edition");
if (count($edition)) $unimarc["205"][0]["a"][0]=$edition[0];
//Editeurs
$publisher=$this->soap2array($nt,"Publisher");
$pubdate =$this->soap2array($nt,"PublicationDate");
$releasedate=$this->soap2array($nt,"ReleaseDate");
$unimarc["210"][0]["c"][0]=$publisher[0];
if (count($pubdate)) $unimarc["210"][0]["d"][]=substr($pubdate[0],0,4);
if (count($releasedate)) $unimarc["210"][0]["d"][]=substr($releasedate[0],0,4);
//Collation
$numberofpages=$this->soap2array($nt,"NumberOfPages");
$numberoftracks=$this->soap2array($nt,"NumberOfTracks");
if (count($numberofpages)) $unimarc["215"][0]["a"][]=$numberofpages[0]." p.";
if (count($numberoftracks)) $unimarc["215"][0]["a"][]=$numberoftracks[0]." p.";
$aspectratio=$this->soap2array($nt,"AscpectRatio");
$audioformat=$this->soap2array($nt,"AudioFormat");
$c_215=array();
if ($aspectratio[0]) $c_215[]=$aspectratio[0];
if ($audioformat[0]) $c_215[]="Audio : ".$audioformat[0];
$c_215=implode(" - ",$c_215);
if ($c_215) $unimarc["215"][0]["c"][0]=$c_215;
$numberofitems=$this->soap2array($nt,"NumberOfItems");
$numberofdiscs=$this->soap2array($nt,"NumberOfDiscs");
$e_215=array();
if ($numberofitems[0]) $e_215[]=$this->msg["numberofitems"]." : ".$numberofitems[0];
if ($numberofdiscs[0]) $e_215[]=$this->msg["numberofdiscs"]." : ".$numberofdiscs[0];
$e_215=implode(" - ",$e_215);
if ($e_215) $unimarc["215"][0]["e"][0]=$e_215;
$binding=$this->soap2array($nt,"Binding");
$runningtime=$this->soap2array($nt,"RunningTime");
$d_215=array();
if ($binding[0]) $d_215[]=$binding[0];
if ($runningtime[0]) $d_215[]=$this->msg["runningtime"]." : ".$runningtime[0]["!"]." ".$runningtime[0]["!Units"];
$d_215=implode(" - ",$d_215);
if ($d_215) $unimarc["215"][0]["d"][0]=$d_215;
//Notes
$theatricaldate=$this->soap2array($nt,"TheatricalReleaseDate");
$format=$this->soap2array($nt,"Format");
$a_300=array();
if ($theatricaldate[0]) $a_300[]=$this->msg["theatricaldate"]." : ".$theatricaldate[0];
if ($format[0]) $a_300[]=$this->msg["format"]." : ".$format[0];
$a_300=implode("\n",$a_300);
if ($a_300) $unimarc["300"][0]["a"][0]=$a_300;
//Dewey
$dewey=$this->soap2array($nt,"DeweyDecimalNumber");
if ($dewey[0]) $unimarc["676"][0]["a"][0]=$dewey[0];
//Auteurs
$auttotal=array();
$authors=$this->soap2array($nt,"Author");
if (count($authors)) {
if (count($authors)>1) $autf="701"; else $autf="700";
for ($i=0; $isoap2array($nt,"Artist");
if (count($authors)) {
if (($naut+count($authors))>1) $autf="701"; else $autf="700";
for ($i=0; $isoap2array($nt,"Actor");
if (count($authors)) {
if (($naut+count($authors))>1) $autf="701"; else $autf="700";
for ($i=0; $isoap2array($nt,"Director");
if (count($authors)) {
if (($naut+count($authors))>1) $autf="701"; else $autf="700";
for ($i=0; $isoap2array($nt,"Creator");
$authors=array();
if (count($creator)) {
for ($i=0; $i1) $autf="701"; else $autf="700";
for ($i=0; $isoap2array($item["EditorialReviews"],"EditorialReview");
if (count($reviews)) {
for ($i=0; $isoap2array($item["Tracks"],"Disc");
if (count($discs)) {
$tr="";
for ($i=0; $imsg["disc"]." ".($i+1)."\n";
$tracks=$this->soap2array($discs[$i],"Track");
for ($j=0; $jerror=false;
$this->error_message="";
$params=$this->get_source_params($source_id);
$this->fetch_global_properties();
$parameters = unserialize($this->parameters);
if ($params["PARAMETERS"]) {
//Affichage du formulaire avec $params["PARAMETERS"]
$vars=unserialize($params["PARAMETERS"]);
foreach ($vars as $key=>$val) {
global ${$key};
${$key}=$val;
}
}
if (!$max_return) $max_return=100;
//Tranformation de la recherche en requete amazon
$this->parse_profile();
$searchattr=array();
$searchattr=$this->parse_query($query,true);
if ($searchattr["TYPE"]=="power")
$q=$this->make_power_query($searchattr);
else {
//Recherche simple
$q=$this->make_simple_search($searchattr);
}
$arraySoapOptions = array();
$arraySoapOptions["exceptions"]=true;
$arraySoapOptions["connection_timeout"]=$params["TIMEOUT"];
if($opac_curl_proxy!=''){
$param_proxy = explode(',',$opac_curl_proxy);
$arraySoapOptions["proxy_host"] = $param_proxy[0];
$arraySoapOptions["proxy_port"] = $param_proxy[1];
$arraySoapOptions["proxy_login"] = $param_proxy[2];
$arraySoapOptions["proxy_password"] = $param_proxy[3];
}
try {
$client = new SoapClient("http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl", $arraySoapOptions);
$client->__setLocation($this->profile["SITES"][0]["SITE"][$this->current_site]["ADDRESS"][0]["URL"]);
} catch (Exception $e) {
$this->error=true;
$this->error_message=$e->getMessage();
}
if (!$this->error) {
$this->make_search($search_id,$q,$client);
$requete="select asin from amazon_items limit $max_return";
$resultat=pmb_mysql_query($requete);
if (@pmb_mysql_num_rows($resultat)) {
//Récupération des résultats et import
$n=0;
while ($r=pmb_mysql_fetch_object($resultat)) {
$asins[]=$r->asin;
$n++;
if ($n==10) {
$tasins[]=$asins;
$asins=array();
$n=0;
}
}
if (count($asins)) $tasins[]=$asins;
pmb_mysql_query("drop table amazon_items");
//Nouvelle requete amazon
$this->sleep_needed();
$client->__setSoapHeaders(NULL);
$client->__setSoapHeaders($this->make_soap_headers('ItemLookup'));
for ($k=0; $k"Large,Tracks",
"ItemId"=>implode(",",$tasins[$k])
);
$paws['AssociateTag'] = $parameters['associatetag'];
try{
$this->sleep_needed();
$result=$client->ItemLookup($paws);
}catch(Exception $e){
$this->error=true;
$this->error_message=$e->getMessage();
}
if(!$this->error){
if(is_object($result)){
$result=amazon::objectToArrayAndCharset($result);
}
if ($result["Items"]["Item"]) {
$items=$this->soap2array($result["Items"],"Item");
for ($i=0; $irec_record($this->amazon_2_uni($items[$i]),$source_id,$search_id);
}
}
}
}
}
}
}
public function make_soap_headers($action){
$keys = unserialize($this->parameters);
$time = gmdate("Y-m-d\TH:i:s\Z");
$signature = base64_encode(hash_hmac("sha256",$action.$time,$keys['secretkey'],true));
$header_arr = array();
$header_arr[] = new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/', 'AWSAccessKeyId', $keys['accesskey']);
$header_arr[] = new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/', 'Timestamp', $time );
$header_arr[] = new SoapHeader('http://security.amazonaws.com/doc/2007-01-01/', 'Signature', $signature );
return $header_arr;
}
public function enrichment_is_allow(){
return true;
}
public function getEnrichmentHeader($source_id){
$header= array();
//$header[]= "";
return $header;
}
public function getTypeOfEnrichment($notice_id,$source_id){
$type=array();
$type['type'] = array(
"resume",
"similarities",
array(
"code" => "review",
"label" => $this->msg['amazon_enrichment_customers_reviews']
),
"extract_audio"
);
$type['source_id'] = $source_id;
return $type;
}
public function getEnrichment($notice_id,$source_id,$type="",$enrich_params=array(),$page=1){
$enrichment= array();
//on renvoi ce qui est demandé... si on demande rien, on renvoi tout..
$info = $this->getNoticeInfos($notice_id,$source_id);
switch ($type){
case "extract_audio" :
if($info) $enrichment['extract_audio']['content'] = $info['extract_audio'];
else $enrichment['extract_audio']['content'] = $this->msg['amazon_enrichment_no_similarities'];
break;
case "review" :
if($info) $enrichment['review']['content'] = $info['review'];
else $enrichment['review']['content'] = $this->msg['amazon_enrichment_no_similarities'];
break;
case "resume" :
if($info) $enrichment['resume']['content'] = $info['resume'];
else $enrichment['resume']['content'] = $this->msg['amazon_enrichment_no_similarities'];
break;
case "similarities" :
default :
if($info) $enrichment['similarities']['content'] = $info['similarities'];
else $enrichment['similarities']['content'] = $this->msg['amazon_enrichment_no_similarities'];
break;
}
$enrichment['source_label']=$this->msg['amazon_enrichment_source'];
return $enrichment;
}
public function getNoticeInfos($notice_id,$source_id){
global $search_index,$url;
$error=false;
$infos = array();
$paws = array();
$asin = 0;
$params=$this->get_source_params($source_id);
$this->fetch_global_properties();
$parameters = unserialize($this->parameters);
if ($params["PARAMETERS"]) {
//Affichage du formulaire avec $params["PARAMETERS"]
$vars=unserialize($params["PARAMETERS"]);
foreach ($vars as $key=>$val) {
global ${$key};
${$key}=$val;
}
}
$client = $this->initAWS($source_id);
if(!is_object($client) && $client === false) {
return false;
}
$client->__setSoapHeaders($this->make_soap_headers('ItemSearch'));
$rqt = "select code from notices where notice_id = '".$notice_id."'";
$res = pmb_mysql_query($rqt);
if(pmb_mysql_num_rows($res)){
$code = pmb_mysql_result($res,0,0);
if($code != ""){
$code = preg_replace('/-|\.| /', '', $code);
$paws["Request"]=array(
"SearchIndex" => $search_index[$url][0],
"ResponseGroup"=>"ItemIds",
"Keywords" => "$code"
);
$paws["AssociateTag"] = $parameters['associatetag'];
try{
$this->sleep_needed();
$result=$client->ItemSearch($paws);
}catch(Exception $e){
$error=true;
}
if(!$error){
if(is_object($result)){
$result=amazon::objectToArrayAndCharset($result);
}
$items=$this->soap2array($result["Items"],"Item");
$asin = $items[0]['ASIN'];
if($asin){
$client->__setSoapHeaders(NULL);
$client->__setSoapHeaders($this->make_soap_headers('ItemLookup'));
$paws["Request"]=array(
"ResponseGroup"=>"Large",
"IdType" => "ASIN",
"ItemId" => $asin
);
try{
$this->sleep_needed();
$result=$client->ItemLookup($paws);
}catch(Exception $e){
$error=true;
}
if(!$error){
if(is_object($result)){
$result=amazon::objectToArrayAndCharset($result);
}
$items=$this->soap2array($result["Items"],"Item");
//récupération des résumés...
$resumes=$this->soap2array($items[0]["EditorialReviews"],"EditorialReview");
if (count($resumes)) {
for ($i=0; $isoap2array($items[0]["ItemLinks"],"ItemLink");
if (count($links)) {
for ($i=0; $i";
}
}
}
//récupération des extrait sonore si dispo ...
$discs=$this->soap2array($items[0]["Tracks"],"Disc");
if (count($discs)) {
$infos['extract_audio'] = "