@ -6,7 +6,7 @@ if (!defined('_ECRIRE_INC_VERSION')) {
// Cette balise teste si la connexion à Zotero fonctionne
function balise_TESTER_CONNEXION_ZOTERO_dist($p) {
$p->code = "zotspip_tester_connexion_zotero()" ;
$p->code = 'zotspip_tester_connexion_zotero()' ;
return $p;
}
@ -23,24 +23,24 @@ function balise_REFERENCE_dist($p) {
$csljson = champ_sql('csljson', $p);
$annee = champ_sql('annee', $p);
$_lang = champ_sql('lang', $p);
$style = interprete_argument_balise(1,$p);
$souligne = interprete_argument_balise(2,$p);
$style = interprete_argument_balise(1, $p);
$souligne = interprete_argument_balise(2, $p);
$date = champ_sql('date', $p);
if (!$style) {
$style='""';
$style = '""';
}
if (!$souligne) {
$souligne='array()';
$souligne = 'array()';
}
$p->code = "zotspip_calculer_reference($csljson,$annee,$style,$souligne,$date,htmlentities($_lang ? $_lang : \$GLOBALS['spip_lang']))";
return $p;
}
function zotspip_calculer_reference($csljson,$annee,$style,$souligne,$date,$lang) {
function zotspip_calculer_reference($csljson, $annee, $style, $souligne, $date, $lang) {
include_spip('lib/citeproc-php/CiteProc');
include_spip('inc/config');
static $citeproc = array() ;
static $citeproc = [] ;
if (!$style) {
include_spip('inc/config');
$style = lire_config('zotspip/csl_defaut') ? lire_config('zotspip/csl_defaut') : 'apa';
@ -50,28 +50,36 @@ function zotspip_calculer_reference($csljson,$annee,$style,$souligne,$date,$lang
if (isset($data->issued->raw) & & lire_config('zotspip/corriger_date')) { // Correction de la date de publication (si fournie brute et si option activée)
unset($data->issued->raw);
// Gestion des cas où la date est de la forme yyyy-mm ou yyyy-mm-dd
if (preg_match('#^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$#',trim($date),$matches))
$data->issued->{'date-parts'} = array(array($matches[1],$matches[2],$matches[3])) ;
elseif (preg_match('#^([0-9]{4})-([0-9]{1,2})$#',trim($date),$matches))
$data->issued->{'date-parts'} = array(array($matches[1],$matches[2])) ;
else
$data->issued->{'date-parts' } = array(array($annee));
if (preg_match('#^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$#', trim($date), $matches)) {
$data->issued->{'date-parts'} = [[$matches[1],$matches[2],$matches[3]]] ;
} elseif (preg_match('#^([0-9]{4})-([0-9]{1,2})$#', trim($date), $matches)) {
$data->issued->{'date-parts'} = [[$matches[1],$matches[2]]] ;
} else { $data->issued->{'date-parts'} = [[$annee]];
}
}
if (!is_array($souligne)) $souligne = explode(';',$souligne);
if (!is_array($souligne)) { $souligne = explode(';', $souligne);
}
if (count($souligne)){
if (count($souligne)) {
foreach ($souligne as $aut_souligne) {
if(!strpos($aut_souligne,',')) $aut_souligne .= ', ';
$aut_souligne = explode(',',$aut_souligne);
if (is_array($data->author))
foreach ($data->author as $cle => $author)
if ($author->family == trim($aut_souligne[0]) & & $author->given == trim($aut_souligne[1]))
$data->author[$cle]->family = '§§'.$data->author[$cle]->family.'§§';
if (is_array($data->editor))
foreach ($data->editor as $cle => $editor)
if ($editor->family == trim($aut_souligne[0]) & & $editor->given == trim($aut_souligne[1]))
$data->editor[$cle]->family = '§§'.$data->editor[$cle]->family.'§§';
if (!strpos($aut_souligne, ',')) { $aut_souligne .= ', ';
}
$aut_souligne = explode(',', $aut_souligne);
if (is_array($data->author)) {
foreach ($data->author as $cle => $author) {
if ($author->family == trim($aut_souligne[0]) & & $author->given == trim($aut_souligne[1])) {
$data->author[$cle]->family = '§§' . $data->author[$cle]->family . '§§';
}
}
}
if (is_array($data->editor)) {
foreach ($data->editor as $cle => $editor) {
if ($editor->family == trim($aut_souligne[0]) & & $editor->given == trim($aut_souligne[1])) {
$data->editor[$cle]->family = '§§' . $data->editor[$cle]->family . '§§';
}
}
}
}
}
@ -80,39 +88,40 @@ function zotspip_calculer_reference($csljson,$annee,$style,$souligne,$date,$lang
$csl = spip_file_get_contents(find_in_path("csl/$style.csl"));
// Si le style demande n'est pas disponible, message d'erreur et se rabattre sur apa.csl
if (!$csl) {
erreur_squelette(_T('zotspip:message_erreur_style_csl',array('style'=>$style) ));
$csl = spip_file_get_contents(find_in_path("csl/apa.csl" ));
erreur_squelette(_T('zotspip:message_erreur_style_csl', ['style' => $style] ));
$csl = spip_file_get_contents(find_in_path('csl/apa.csl' ));
}
$citeproc[$style] = new citeproc($csl,$lang);
$citeproc[$style] = new citeproc($csl, $lang);
}
$ret = $citeproc[$style]->render($data, 'bibliography');
if (count($souligne))
$ret = preg_replace(',§§(.+)§§,U','< span style = "text-decoration:underline;" > $1< / span > ',$ret);
if (count($souligne)) {
$ret = preg_replace(',§§(.+)§§,U', '< span style = "text-decoration:underline;" > $1< / span > ', $ret);
}
return $ret;
}
// Lister les styles CSL disponibles
function balise_LISTER_CSL_dist($p) {
$p->code = "zotspip_lister_csl()" ;
$p->code = 'zotspip_lister_csl()' ;
return $p;
}
function zotspip_lister_csl(){
function zotspip_lister_csl() {
static $liste_csl = null;
if (is_null($liste_csl)){
$liste_csl = array() ;
$match = ".+[.]csl$" ;
if (is_null($liste_csl)) {
$liste_csl = [] ;
$match = '.+[.]csl$' ;
$liste = find_all_in_path('csl/', $match);
if (count($liste)){
foreach($liste as $fichier=>$chemin) {
if (count($liste)) {
foreach ($liste as $fichier => $chemin) {
$style = spip_file_get_contents($chemin);
$csl = substr($fichier,0,-4);
if (preg_match('#\< title \ > (.*)\< /title\>#',$style,$matches))
$csl = substr($fichier, 0, -4);
if (preg_match('#\< title \ > (.*)\< /title\>#', $style, $matches)) {
$liste_csl[$csl] = $matches[1];
else
$liste_csl[$csl] = $csl;
} else { $liste_csl[$csl] = $csl;
}
}
}
}
@ -121,35 +130,40 @@ function zotspip_lister_csl(){
// Traduire le type de reference
function zotspip_traduire_type($type) {
return ($type!='') ? _T('zotero:itemtypes_'.strtolower($type)) : '';
return ($type != '') ? _T('zotero:itemtypes_' . strtolower($type)) : '';
}
// Traduire le champ Zotero
function zotspip_traduire_champ($champ) {
return ($champ!='') ? _T('zotero:itemfields_'.strtolower($champ)) : '';
return ($champ != '') ? _T('zotero:itemfields_' . strtolower($champ)) : '';
}
// Traduire le type d'auteur
function zotspip_traduire_createur($type) {
return ($type!='') ? _T('zotero:creatortypes_'.strtolower($type)) : '';
return ($type != '') ? _T('zotero:creatortypes_' . strtolower($type)) : '';
}
// Fonction utilisée dans pre_typo pour traiter les raccourcis [ref=xxxx]
function zotspip_traiter_raccourci_ref_dist($texte) {
$texte = preg_replace('#\[ref=(.*)\]#U','[[  < ref | id = $1 > ]]',$texte);
$texte = preg_replace('#\[ref=(.*)\]#U', '[[  < ref | id = $1 > ]]', $texte);
return $texte;
}
// Afficher l'icône du document
// On peut optionnellement ajouter mimetype et fichier pour distinguer les icônes dans le cadre des pièces jointes
function zotspip_icone_type($type, $mimetype=NULL, $fichier=NULL ) {
function zotspip_icone_type($type, $mimetype = null, $fichier = null ) {
$alt = zotspip_traduire_type($type);
if ($type=='attachment' & & !is_null($fichier) & & !$fichier) $type = 'attachment-web-link'; // Si fichier n'est pas renseigné, c'est un lien sur le web
if ($type=='attachment' & & $fichier & & $mimetype=='text/html') $type = 'attachment-snapshot';
if ($type=='attachment' & & $fichier & & $mimetype=='application/pdf') $type = 'attachment-pdf';
if ($type == 'attachment' & & !is_null($fichier) & & !$fichier) { $type = 'attachment-web-link'; // Si fichier n'est pas renseigné, c'est un lien sur le web
}
if ($type == 'attachment' & & $fichier & & $mimetype == 'text/html') { $type = 'attachment-snapshot';
}
if ($type == 'attachment' & & $fichier & & $mimetype == 'application/pdf') { $type = 'attachment-pdf';
}
$chemin = find_in_path("images/zotero/$type.svg");
if (!$chemin) $chemin = find_in_path("images/zotero/$type.png");
if (!$chemin) $chemin = find_in_path("images/zotero/item.png");
if (!$chemin) { $chemin = find_in_path("images/zotero/$type.png");
}
if (!$chemin) { $chemin = find_in_path('images/zotero/item.png');
}
return "< img src = \"$chemin\" height = \"16\" width = \"16\" alt = \"$alt\"/ > ";
}
@ -162,71 +176,76 @@ function balise_ZITEM_DETAILS_dist($p) {
function zotspip_calculer_zitem_details($json) {
$ret = '< table class = "zitem_details spip" > ';
$data = json_decode($json,true);
if (!is_array($data)) return '';
$data = json_decode($json, true);
if (!is_array($data)) { return '';
}
foreach ($data as $champ => $valeur) {
if ($champ=='itemType')
$ret .= "< tr > < td class = \"champ\" > < strong > ".zotspip_traduire_champ($champ)."< / strong > < / td > < td class = \"valeur\" > ".zotspip_traduire_type($valeur)."< / td > < / tr > ";
elseif ($champ=='creators')
foreach ($valeur as $creator)
$ret .= "< tr > < td class = \"champ\" > < strong > ".zotspip_traduire_createur($creator['creatorType'])."< / strong > < / td > < td class = \"valeur\" > ".(isset($creator['name'])?$creator['name']:($creator['lastName'].(isset($creator['firstName'])?', '.$creator['firstName']:'')))."< / td > < / tr > ";
elseif ($champ=='tags' & & count($valeur)>0) {
$tags = array();
foreach ($valeur as $tag)
if ($champ == 'itemType') {
$ret .= '< tr > < td class = "champ" > < strong > ' . zotspip_traduire_champ($champ) . '< / strong > < / td > < td class = "valeur" > ' . zotspip_traduire_type($valeur) . '< / td > < / tr > ';
} elseif ($champ == 'creators') {
foreach ($valeur as $creator) {
$ret .= '< tr > < td class = "champ" > < strong > ' . zotspip_traduire_createur($creator['creatorType']) . '< / strong > < / td > < td class = "valeur" > ' . (isset($creator['name']) ? $creator['name'] : ($creator['lastName'] . (isset($creator['firstName']) ? ', ' . $creator['firstName'] : ''))) . '< / td > < / tr > ';
}
} elseif ($champ == 'tags' & & count($valeur) > 0) {
$tags = [];
foreach ($valeur as $tag) {
$tags[] = $tag['tag'];
$tags = implode(' · ',$tags);
$ret .= "< tr > < td class = \"champ\" > < strong > "._T('zotero:itemfields_tags')."< / strong > < / td > < td class = \"valeur\" > $tags< / td > < / tr > ";
}
$tags = implode(' · ', $tags);
$ret .= '< tr > < td class = "champ" > < strong > ' . _T('zotero:itemfields_tags') . "< / strong > < / td > < td class = \"valeur\" > $tags< / td > < / tr > ";
}
elseif ($champ=='mimeType')
elseif ($champ == 'mimeType') {
$ret .= "< tr > < td class = \"champ\" > < strong > MIME< / strong > < / td > < td class = \"valeur\" > $valeur< / td > < / tr > ";
elseif ($champ=='url')
$ret .= "< tr > < td class = \"champ\" > < strong > ".zotspip_traduire_champ($champ)."< / strong > < / td > < td class = \"valeur\" > < a href = \"$valeur\" > $valeur< / a > < / td > < / tr > ";
elseif ($valeur)
$ret .= "< tr > < td class = \"champ\" > < strong > ".zotspip_traduire_champ($champ)."< / strong > < / td > < td class = \"valeur\" > $valeur< / td > < / tr > ";
} elseif ($champ == 'url') {
$ret .= '< tr > < td class = "champ" > < strong > ' . zotspip_traduire_champ($champ) . "< / strong > < / td > < td class = \"valeur\" > < a href = \"$valeur\" > $valeur< / a > < / td > < / tr > ";
} elseif ($valeur) {
$ret .= '< tr > < td class = "champ" > < strong > ' . zotspip_traduire_champ($champ) . "< / strong > < / td > < td class = \"valeur\" > $valeur< / td > < / tr > ";
}
}
$ret .= '< / table > ';
return $ret;
}
// Exporte les items dans le format demandé
function zotspip_export ($id, $format) {
if (is_array($id)) $id = implode(',',$id);
function zotspip_export($id, $format) {
if (is_array($id)) { $id = implode(',', $id);
}
include_spip('inc/zotspip');
return zotero_get("items/?itemKey=$id&format=$format");
}
// Renvoie le content-type correspondant à un format d'export
function zotspip_content_type ($format) {
function zotspip_content_type($format) {
switch ($format) {
case 'bibtex':
$ctype = "application/x-bibtex" ;
$ctype = 'application/x-bibtex' ;
break;
case 'mods':
$ctype = "application/mods+xml" ;
$ctype = 'application/mods+xml' ;
break;
case 'refer':
$ctype = "application/x-research-info-systems" ;
$ctype = 'application/x-research-info-systems' ;
break;
case 'rdf_bibliontology':
case 'rdf_dc':
case 'rdf_zotero':
$ctype = "application/rdf+xml" ;
$ctype = 'application/rdf+xml' ;
break;
case 'ris':
$ctype = "application/x-research-info-systems" ;
$ctype = 'application/x-research-info-systems' ;
break;
case 'wikipedia':
$ctype = "text/x-wiki" ;
$ctype = 'text/x-wiki' ;
break;
default: