3 class sml_data_sources_cache extends mw_data{
6 * retourne le contenu XML dont l'URL d'origine est $url
8 * si le contenu n'est pas dans le cache, cette fonction
9 * ira le lire et l'ajoutera dans le cache
11 * si le contenu est dans cache mais que sa date de validite
12 * est depassee, le contenu sera mis a jour
14 * $url : l'URL d'origine du fichier XML
16 * $IGNORE_UPDATE : si VRAI et qu'un contenu correspondant est
17 * dans le cache, c'est ce contenu qui sera fourni, meme si
18 * sa date de validite est depassee
21 function get_source_xml_from_cache($url, $IGNORE_UPDATE = false){
23 $sgbd = $this->sgbd();
24 if(($cache_infos = $this->source_cache_infos_db($url)) !== false){
26 if(!($this->buffer = @file_get_contents($env->path("content")."cache/sources/".$cache_infos["id_source"].".xml"))){
29 if($env->config("cache_maj_auto")){
30 if(($need_update = $this->cache_need_update($cache_infos["last_update"], $env->config("cache_time") * 60 * 60)) !== false){
31 if(!$IGNORE_UPDATE && $need_update){
32 if($this->buffer = @file_get_contents($url)){
33 if($this->parse_source_xml($this->buffer)){
34 if($this->del_source_cache($cache_infos["id"], $cache_infos["id_source"])){
35 if($this->add_source_cache($url, $this->buffer)) return $this->buffer;
44 else return $this->buffer;
46 else return $this->buffer;
48 else return $this->buffer;
51 if($this->buffer = @file_get_contents($url)){
52 if($this->parse_source_xml($this->buffer)){
53 if(!$this->add_source_cache($url, $this->buffer)) return false;
63 * retourne 1 si $last_update + $cache_time est
64 * superieur ou egal a la date courante, 0 sinon
66 * retourne FAUX si le formta de $last_update ou
67 * de $cache_time est incorrect
69 * $last_update : date au format Y-m-d H:i:s
71 * $cache_time : duree, en secondes
74 function cache_need_update($last_update, $cache_time)
75 { $v_last_update = explode(" ", $last_update);
76 if(count($v_last_update) == 2)
77 { $v_date = explode("-", $v_last_update[0]);
78 if(count($v_date) == 3)
79 { $v_time = explode(":", $v_last_update[1]);
80 if(count($v_time) == 3)
82 ( preg_match("/^[0-9]+$/", $v_date[0])
83 && preg_match("/^[0-9]+$/", $v_date[1])
84 && preg_match("/^[0-9]+$/", $v_date[2])
85 && preg_match("/^[0-9]+$/", $v_time[0])
86 && preg_match("/^[0-9]+$/", $v_time[1])
87 && preg_match("/^[0-9]+$/", $v_time[2])
88 && preg_match("/^[0-9]+$/", $cache_time)
90 { return (time() - mktime($v_time[0], $v_time[1], $v_time[2], $v_date[1], $v_date[2], $v_date[0])) < $cache_time ? 0 : 1;
99 * ajoute un contenu xml dans le cache
101 * $url : l'URL du fichier XML d'origine
103 * $xml_content : le contenu du fichier XML
106 function add_source_cache($url, $xml_content){
108 $sgbd = $this->sgbd();
109 if(($cache_index = $this->inc_sources_cache_index()) !== false){
110 if($fh = @fopen($env->path("content")."cache/sources/".$cache_index.".xml", "w+")){
112 if(@fwrite($fh, $this->buffer)){
114 $res = $this->add_source_cache_db($url, $cache_index);
124 * met a jour le cache pour le fichier XML dont l'URL d'origine est $url
128 * VRAI si tout se passe bien
129 * -1 si l'URL est introuvable
130 * -2 si le contenu a cette URL n'est pas un fichier XML de source
131 * FAUX si le cache n'a pas pu etre mis a jour avec les nouvelles infos
134 function maj_source_cache($url)
135 { if($this->buffer = @file_get_contents($url))
136 { if($this->parse_source_xml($this->buffer))
137 { if(($cache_infos = $this->source_cache_infos_db($url)) !== false)
139 { if($this->del_source_cache($cache_infos["id"], $cache_infos["id_source"]))
140 { if($this->add_source_cache($url, $this->buffer)) return true;
144 { if($this->add_source_cache($url, $this->buffer)) return true;
158 function empty_source_cache()
160 if(($cache = $this->source_cache_db()) !== false)
161 { foreach($cache as $id_cache => $cache_infos)
162 { if(!$this->del_source_cache($id_cache, $cache_infos["id_source"]))
173 * efface un contenu XML du cache
175 * $id_cache_data : l'index des informations de cache
176 * dans le dossier data
178 * $id_xml_cache : l'index du contenu XML dans le cache
181 function del_source_cache($id_cache_data, $id_xml_cache)
182 { $env = $this->env();
183 if($this->del_source_cache_db($id_cache_data))
184 { if(@unlink($env->path("content")."cache/sources/".$id_xml_cache.".xml")) return true;
190 * initialise le cache
191 * cette fonction verifie que les dossiers du cache existent
195 function init_sources_cache()
196 { $env = $this->env();
197 if(!is_dir($env->path("content")."cache")) @mkdir($env->path("content")."cache");
198 if(is_dir($env->path("content")."cache"))
199 { if(!is_dir($env->path("content")."cache/sources")) @mkdir($env->path("content")."cache/sources");
200 if(is_dir($env->path("content")."cache/sources"))
204 return "impossible d'initialiser le cache";
208 * incremente l'index du cache et retourne le nouvel index
211 function inc_sources_cache_index(){
214 $cache_index = false;
215 $cache_dir = $env->path("content")."cache";
216 if(!is_dir($cache_dir)) @mkdir($cache_dir);
217 if(!is_dir($cache_dir)) return false;
218 $cache_dir .= "/sources/";
219 if(!is_dir($cache_dir)) @mkdir($cache_dir);
220 if(!is_dir($cache_dir)) return false;
221 if(is_dir($cache_dir) && is_writable($cache_dir)){
222 if(!file_exists($cache_dir.".index")){
223 if($fh = @fopen($cache_dir.".index", "w+")){
224 if(@fwrite($fh, "0")) $cache_index = 0;
229 if(($cache_index = @file_get_contents($cache_dir.".index")) !== false){
230 if(preg_match("/^[0-9]+$/", $cache_index)){
231 $cache_index = (int)$cache_index;
233 else $cache_index = false;
237 if($cache_index !== false){
239 if($fh = @fopen($cache_dir.".index", "w+")){
240 if(!@fwrite($fh, (string)$cache_index)) $cache_index = false;
243 else $cache_index = false;