Skip to content

Commit

Permalink
Merge pull request #15 from Cerdic/optimise_cache_comarquage
Browse files Browse the repository at this point in the history
fix bug copie_locale() et optimisation du cache
  • Loading branch information
psychoz971 committed Jan 2, 2024
2 parents 62f5ddc + 54d2c71 commit b2dd7a4
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -53,7 +53,7 @@ Les squelettes de _Co-Marquage Service Public_ utilisent les classes et composan
Le flux est récupéré grâce à la balise DATA. Les boucles sont en cache
par défaut pendant **86400 secondes** (soit 24 h).

Les XMLs de co-marquage sont copiés en local `|copie_locale{modif}`. Pour
Les XMLs de co-marquage sont copiés en local `|comarquage_url_cache`. Pour
forcer le re-téléchargement des XMLs vider le répertoire `IMG/distant/xml`.

### Astuces
Expand Down
85 changes: 80 additions & 5 deletions comarquage_fonctions.php
Expand Up @@ -14,6 +14,81 @@
return;
}


function comarquage_url_cache($url) {
include_spip('inc/distant');
if (function_exists('curl_init')) {
// version moderne
$fichier_copie_locale = _DIR_RACINE . nom_fichier_copie_locale($url, 'xml');
$fichier_cache = sous_repertoire(_DIR_CACHE, 'comarquage') . basename($fichier_copie_locale);
// recuperer les anciens cache de IMG/distant/ si dispo
if (file_exists($fichier_copie_locale) and !file_exists($fichier_cache)) {
@rename($fichier_copie_locale, $fichier_cache);
}

// le cache date de moins d'1h, on renvoie sans faire de hit sur le serveur
$fichier_cache_check = dirname($fichier_cache). '/.' . $fichier_cache . '.check';
if (file_exists($fichier_cache)) {
if (filemtime($fichier_cache) > $_SERVER['REQUEST_TIME'] - 3600) {
return $fichier_cache;
}
if (file_exists($fichier_cache_check) and filemtime($fichier_cache_check) > $_SERVER['REQUEST_TIME'] - 3600) {
return $fichier_cache;
}
}

// CURL le fichier car recuperer_url() tourne en boucle infinie dans certaines config depuis le 24/11/2023
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');

spip_log("comarquage_url_cache sur $url via curl", 'distant' . _LOG_DEBUG);

$user_agent = "SPIP/Comarquage";

$headers = [];
$headers[] = 'Connection: Close';
$headers[] = "User-Agent: $user_agent";

if (file_exists($fichier_cache)) {
$headers[] = "If-Modified-Since: " . gmdate('D, d M Y H:i:s \G\M\T', filemtime($fichier_cache));
}

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//getting response from server
$response = curl_exec($ch);
$erreur = curl_errno($ch);
$erreur_msg = curl_error($ch);
if (!$erreur){
//closing the curl
curl_close($ch);
spip_log("comarquage_url_cache sur $url via curl OK : " . strlen($response).'car', 'distant');
if ($response) {
spip_log("comarquage_url_cache sur $url via curl OK : => $fichier_cache", 'distant');
file_put_contents($fichier_cache, $response);
}
}
else {
spip_log("comarquage_url_cache sur $url via curl HS : " . $response, 'distant' . _LOG_ERREUR);
}


// et dans tous les cas on touch le check, pour ne pas verifier de nouveau avant 1h
@touch($fichier_cache_check);

return $fichier_cache;

} else {
// compat old SPIP
return copie_locale($url, 'modif');
}
}

/**
* Récupération des types de catégorie et leurs URL
*
Expand All @@ -25,22 +100,22 @@ function filtre_type_categorie_dist($categorie) {
$parametres_xml = array();
switch ($categorie) {
case "particuliers":
$parametres_xml['XMLURL'] = "http://lecomarquage.service-public.fr/vdd/3.3/part/xml/";
$parametres_xml['XMLURL'] = "https://lecomarquage.service-public.fr/vdd/3.3/part/xml/";
$parametres_xml['CATEGORIE'] = "part";
break;

case "associations":
$parametres_xml['XMLURL'] = "http://lecomarquage.service-public.fr/vdd/3.3/asso/xml/";
$parametres_xml['XMLURL'] = "https://lecomarquage.service-public.fr/vdd/3.3/asso/xml/";
$parametres_xml['CATEGORIE'] = "asso";
break;

case 'entreprises':
$parametres_xml['XMLURL'] = "http://lecomarquage.service-public.fr/vdd/3.3/pro/xml/";
$parametres_xml['XMLURL'] = "https://lecomarquage.service-public.fr/vdd/3.3/pro/xml/";
$parametres_xml['CATEGORIE'] = "pro";
break;

default:
$parametres_xml['XMLURL'] = "http://lecomarquage.service-public.fr/vdd/3.3/part/xml/";
$parametres_xml['XMLURL'] = "https://lecomarquage.service-public.fr/vdd/3.3/part/xml/";
$parametres_xml['CATEGORIE'] = "part";
break;
}
Expand Down Expand Up @@ -358,4 +433,4 @@ function fluxXmlObjToArr($obj, $utiliser_namespace = false, $parentName = '') {
}

return $tableau;
}
}
2 changes: 1 addition & 1 deletion xml_pages/accueil.html
Expand Up @@ -10,7 +10,7 @@
<div class="page-accueil">
[(#REM) Menu principal]
<INCLURE{fond=xml_templates/menu, env}{base_url=#ENV{url}} />
<BOUCLE_accueil(DATA){source simplexml, #GET{url}|copie_locale{modif}}
<BOUCLE_accueil(DATA){source simplexml, (#GET{url}|comarquage_url_cache)}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_accueil>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/centresdecontact.html
Expand Up @@ -8,7 +8,7 @@

<B_centresdecontact>
<div class="page-centresdecontact">
<BOUCLE_centresdecontact(DATA){source simplexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_centresdecontact(DATA){source simplexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_centresdecontact>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/commentfairesi.html
Expand Up @@ -8,7 +8,7 @@

<B_commentfairesi>
<div class="page-commentfairesi">
<BOUCLE_commentfairesi(DATA){source simplexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_commentfairesi(DATA){source simplexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_commentfairesi>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/fiche.html
Expand Up @@ -8,7 +8,7 @@

<B_fiche>
<div class="page-fiche">
<BOUCLE_fiche(DATA){source comarquagexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_fiche(DATA){source comarquagexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_fiche>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/noeud.html
Expand Up @@ -8,7 +8,7 @@

<B_noeud>
<div class="page-noeud">
<BOUCLE_noeud(DATA){source comarquagexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_noeud(DATA){source comarquagexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_noeud>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/questionsreponses.html
Expand Up @@ -8,7 +8,7 @@

<B_questionsreponses>
<div class="page-questionsreponses">
<BOUCLE_questionsreponses(DATA){source simplexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_questionsreponses(DATA){source simplexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_questionsreponses>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/ressource.html
Expand Up @@ -8,7 +8,7 @@

<B_ressource>
<div class="page-ressource">
<BOUCLE_ressource(DATA){source comarquagexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_ressource(DATA){source comarquagexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_ressource>
Expand Down
2 changes: 1 addition & 1 deletion xml_pages/servicesenligne.html
Expand Up @@ -8,7 +8,7 @@

<B_servicesenligne>
<div class="page-servicesenligne">
<BOUCLE_servicesenligne(DATA){source simplexml, #GET{url}|copie_locale{modif}, namespace}
<BOUCLE_servicesenligne(DATA){source simplexml, (#GET{url}|comarquage_url_cache), namespace}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/balises/#CLE|replace{:,/}, env}{data=#VALEUR} />
</BOUCLE_servicesenligne>
Expand Down
2 changes: 1 addition & 1 deletion xml_templates/menu.html
Expand Up @@ -9,7 +9,7 @@

<B_menu>
<div class="xml-menu">
<BOUCLE_menu(DATA){source simplexml,#GET{url}|copie_locale{modif}}
<BOUCLE_menu(DATA){source simplexml,(#GET{url}|comarquage_url_cache)}
{datapath root/children}{datacache #GET{datacache}}>
<INCLURE{fond=xml_templates/menu/#CLE, env}{data=#VALEUR} />
</BOUCLE_menu>
Expand Down

0 comments on commit b2dd7a4

Please sign in to comment.