NEW Use a cache file for external RSS in calendar

This commit is contained in:
Laurent Destailleur 2023-01-13 17:17:23 +01:00
parent 5fc9e093eb
commit 97061a9a92
4 changed files with 58 additions and 10 deletions

View File

@ -54,8 +54,8 @@ class ICal
/** /**
* Read text file, icalender text file * Read text file, icalender text file
* *
* @param string $file File * @param string $file File
* @return string * @return string|null Content of remote file read or null if error
*/ */
public function read_file($file) public function read_file($file)
{ {
@ -65,7 +65,7 @@ class ICal
$tmpresult = getURLContent($file, 'GET'); $tmpresult = getURLContent($file, 'GET');
if ($tmpresult['http_code'] != 200) { if ($tmpresult['http_code'] != 200) {
$file_text = ''; $file_text = null;
$this->error = 'Error: '.$tmpresult['http_code'].' '.$tmpresult['content']; $this->error = 'Error: '.$tmpresult['http_code'].' '.$tmpresult['content'];
} else { } else {
$file_text = preg_replace("/[\r\n]{1,} /", "", $tmpresult['content']); $file_text = preg_replace("/[\r\n]{1,} /", "", $tmpresult['content']);
@ -102,17 +102,40 @@ class ICal
/** /**
* Translate Calendar * Translate Calendar
* *
* @param string $uri Url * @param string $uri Url
* @param string $usecachefile Full path of a cache file to use a cache file
* @param string $delaycache Delay in seconds for cache (by default 3600 secondes)
* @return array|string * @return array|string
*/ */
public function parse($uri) public function parse($uri, $usecachefile = '', $delaycache = 3600)
{ {
$this->cal = array(); // new empty array $this->cal = array(); // new empty array
$this->event_count = -1; $this->event_count = -1;
$this->file_text = null;
// Save file into a cache
if ($usecachefile) {
include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
$datefile = dol_filemtime($usecachefile);
$now = dol_now('gmt');
//print $datefile.' '.$now.' ...';
if ($datefile && $datefile > ($now - $delaycache)) {
// We reuse the cache file
$this->file_text = file_get_contents($usecachefile);
}
}
// read FILE text // read FILE text
$this->file_text = $this->read_file($uri); if (is_null($this->file_text)) {
$this->file_text = $this->read_file($uri);
if ($usecachefile && !is_null($this->file_text)) {
// Save the file content into cache file
file_put_contents($usecachefile, $this->file_text, LOCK_EX);
dolChmod($usecachefile);
}
}
$this->file_text = preg_split("[\n]", $this->file_text); $this->file_text = preg_split("[\n]", $this->file_text);

View File

@ -49,6 +49,7 @@ if (empty($conf->global->AGENDA_EXT_NB)) {
$conf->global->AGENDA_EXT_NB = 5; $conf->global->AGENDA_EXT_NB = 5;
} }
$MAXAGENDA = $conf->global->AGENDA_EXT_NB; $MAXAGENDA = $conf->global->AGENDA_EXT_NB;
$DELAYFORCACHE = 300; // 300 seconds
$disabledefaultvalues = GETPOST('disabledefaultvalues', 'int'); $disabledefaultvalues = GETPOST('disabledefaultvalues', 'int');
@ -615,7 +616,7 @@ if (!empty($conf->use_javascript_ajax)) { // If javascript on
$default = ''; $default = '';
} }
$s .= '<div class="nowrap inline-block minheight30"><input type="checkbox" id="check_ext'.$htmlname.'" name="check_ext'.$htmlname.'" value="1" '.$default.'> <label for="check_ext'.$htmlname.'">'.dol_escape_htmltag($val['name']).'</label> &nbsp; </div>'; $s .= '<div class="nowrap inline-block minheight30"><input type="checkbox" id="check_ext'.$htmlname.'" name="check_ext'.$htmlname.'" value="1" '.$default.'> <label for="check_ext'.$htmlname.'" title="'.dol_escape_htmltag($langs->trans("Cache").' '.round($DELAYFORCACHE / 60).'mn').'">'.dol_escape_htmltag($val['name']).'</label> &nbsp; </div>';
} }
} }
@ -1010,9 +1011,13 @@ if ($mode == 'show_day') {
// Request only leaves for the current selected day // Request only leaves for the current selected day
$sql .= " AND '".$db->escape($year)."-".$db->escape($month)."-".$db->escape($day)."' BETWEEN x.date_debut AND x.date_fin"; // date_debut and date_fin are date without time $sql .= " AND '".$db->escape($year)."-".$db->escape($month)."-".$db->escape($day)."' BETWEEN x.date_debut AND x.date_fin"; // date_debut and date_fin are date without time
} elseif ($mode == 'show_week') { } elseif ($mode == 'show_week') {
// TODO: Add filter to reduce database request // Restrict on current month (we get more, but we will filter later)
$sql .= " AND date_debut < '".dol_get_last_day($year, $month)."'";
$sql .= " AND date_fin >= '".dol_get_first_day($year, $month)."'";
} elseif ($mode == 'show_month') { } elseif ($mode == 'show_month') {
// TODO: Add filter to reduce database request // Restrict on current month
$sql .= " AND date_debut <= '".dol_get_last_day($year, $month)."'";
$sql .= " AND date_fin >= '".dol_get_first_day($year, $month)."'";
} }
$resql = $db->query($sql); $resql = $db->query($sql);
@ -1083,8 +1088,11 @@ if (count($listofextcals)) {
$colorcal = $extcal['color']; $colorcal = $extcal['color'];
$buggedfile = $extcal['buggedfile']; $buggedfile = $extcal['buggedfile'];
$pathforcachefile = dol_sanitizePathName($conf->user->dir_temp).'/'.dol_sanitizeFileName('extcal_'.$namecal.'_user'.$user->id).'.cache';
//var_dump($pathforcachefile);exit;
$ical = new ICal(); $ical = new ICal();
$ical->parse($url); $ical->parse($url, $pathforcachefile, $DELAYFORCACHE);
// After this $ical->cal['VEVENT'] contains array of events, $ical->cal['DAYLIGHT'] contains daylight info, $ical->cal['STANDARD'] contains non daylight info, ... // After this $ical->cal['VEVENT'] contains array of events, $ical->cal['DAYLIGHT'] contains daylight info, $ical->cal['STANDARD'] contains non daylight info, ...
//var_dump($ical->cal); exit; //var_dump($ical->cal); exit;

View File

@ -117,6 +117,7 @@ function getServerTimeZoneInt($refgmtdate = 'now')
* @param int $duration_unit Unit of added delay (d, m, y, w, h, i) * @param int $duration_unit Unit of added delay (d, m, y, w, h, i)
* @param int $ruleforendofmonth Change the behavior of PHP over data-interval, 0 or 1 * @param int $ruleforendofmonth Change the behavior of PHP over data-interval, 0 or 1
* @return int New timestamp * @return int New timestamp
* @see convertSecondToTime(), convertTimeToSeconds()
*/ */
function dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth = 0) function dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth = 0)
{ {

View File

@ -6844,6 +6844,22 @@ function dol_mkdir($dir, $dataroot = '', $newmask = '')
} }
/**
* Change mod of a file
*
* @param string $filepath Full file path
* @return void
*/
function dolChmod($filepath)
{
global $conf;
if (!empty($conf->global->MAIN_UMASK)) {
@chmod($filepath, octdec($conf->global->MAIN_UMASK));
}
}
/** /**
* Return picto saying a field is required * Return picto saying a field is required
* *