From 97fc8d13f097610016bedb61aebeb43a5ccb492a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Tempez?= Date: Sat, 18 Jan 2025 22:53:43 +0100 Subject: [PATCH] =?UTF-8?q?13.6.00=20nettoyage=20de=20constantes=20inutile?= =?UTF-8?q?s=20li=C3=A9es=20=C3=A0=20jsondb=20Un=20quart=20de=20secondes?= =?UTF-8?q?=20entre=20deux=20tentatives?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/class/jsondb/JsonDb.class.php | 10 +- core/core.php | 303 ++++++++++++++++------------- 2 files changed, 175 insertions(+), 138 deletions(-) diff --git a/core/class/jsondb/JsonDb.class.php b/core/class/jsondb/JsonDb.class.php index d220255d..20fe2d40 100644 --- a/core/class/jsondb/JsonDb.class.php +++ b/core/class/jsondb/JsonDb.class.php @@ -18,8 +18,6 @@ class JsonDb extends \Prowebcraft\Dot protected $db = ''; protected $data = null; protected $config = []; - // Tentative d'encodage après échec - const MAX_JSON_ENCODE_ATTEMPTS = 5; // Tentative d'écriture après échec const MAX_FILE_WRITE_ATTEMPTS = 5; // Délais entre deux tentaives @@ -136,8 +134,8 @@ class JsonDb extends \Prowebcraft\Dot } $this->data = json_decode(file_get_contents($this->db), true); if (!$this->data === null) { - throw new \InvalidArgumentException('Database file ' . $this->db - . ' contains invalid json object. Please validate or remove file'); + throw new \InvalidArgumentException('Le fichier ' . $this->db + . ' contient des données invalides.'); } } return $this->data; @@ -175,8 +173,8 @@ class JsonDb extends \Prowebcraft\Dot // Incrémente le compteur de tentatives $attempt++; - // Attente - sleep(1); + // Attente 1/4 de seconde + usleep(0.25); } // Vérifie si l'écriture a échoué même après plusieurs tentatives diff --git a/core/core.php b/core/core.php index 12375470..4e333853 100644 --- a/core/core.php +++ b/core/core.php @@ -67,18 +67,9 @@ class common // Valeurs possibles multiple de 10, 10 autorise 9 profils, 100 autorise 99 profils const MAX_PROFILS = 10; - const MAX_FILE_WRITE_ATTEMPTS = 5; - - /** - * Nombre maximal de tentatives d'encodage JSON - */ - const MAX_JSON_ENCODE_ATTEMPTS = 3; - - /** - * Temps d'attente entre les tentatives en secondes - */ - const RETRY_DELAY_SECONDS = 1; - + // Taille et rotation des journaux + const LOG_MAXSIZE = 4 * 1024 * 1024; + const LOG_MAXARCHIVE = 5; public static $actions = []; public static $coreModuleIds = [ @@ -1135,122 +1126,121 @@ class common * @param string Valeurs possibles */ - public function updateSitemap() - { - // Le drapeau prend true quand au moins une page est trouvée - $flag = false; - - // Rafraîchit la liste des pages après une modification de pageId notamment - $this->buildHierarchy(); - - // Actualise la liste des pages pour TinyMCE - $this->tinyMcePages(); - - //require_once 'core/vendor/sitemap/SitemapGenerator.php'; - - $timezone = $this->getData(['config', 'timezone']); - $outputDir = getcwd(); - $sitemap = new \Icamys\SitemapGenerator\SitemapGenerator(helper::baseurl(false), $outputDir); - - // will create also compressed (gzipped) sitemap : option buguée - // $sitemap->enableCompression(); - - // determine how many urls should be put into one file - // according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html) - $sitemap->setMaxUrlsPerSitemap(50000); - - // sitemap file name - $sitemap->setSitemapFileName('sitemap.xml'); - - - // Set the sitemap index file name - $sitemap->setSitemapIndexFileName('sitemap-index.xml'); - - $datetime = new DateTime(date('c')); - $datetime->format(DateTime::ATOM); // Updated ISO8601 - - foreach ($this->getHierarchy() as $parentPageId => $childrenPageIds) { - // Exclure les barres et les pages non publiques et les pages masquées - if ( - $this->getData(['page', $parentPageId, 'group']) !== 0 || - $this->getData(['page', $parentPageId, 'block']) === 'bar' - ) { - continue; - } - // Page désactivée, traiter les sous-pages sans prendre en compte la page parente. - if ($this->getData(['page', $parentPageId, 'disable']) !== true) { - // Cas de la page d'accueil ne pas dupliquer l'URL - $pageId = ($parentPageId !== $this->getData(['locale', 'homePageId'])) ? $parentPageId : ''; - $sitemap->addUrl('/' . $pageId, $datetime); - $flag = true; - } - // Articles du blog - if ( - $this->getData(['page', $parentPageId, 'moduleId']) === 'blog' - && !empty($this->getData(['module', $parentPageId])) - && $this->getData(['module', $parentPageId, 'posts']) - ) { - foreach ($this->getData(['module', $parentPageId, 'posts']) as $articleId => $article) { - if ($this->getData(['module', $parentPageId, 'posts', $articleId, 'state']) === true) { - $date = $this->getData(['module', $parentPageId, 'posts', $articleId, 'publishedOn']); - $sitemap->addUrl('/' . $parentPageId . '/' . $articleId, DateTime::createFromFormat('U', $date)); - } - } - } - // Sous-pages - foreach ($childrenPageIds as $childKey) { - if ($this->getData(['page', $childKey, 'group']) !== 0 || $this->getData(['page', $childKey, 'disable']) === true) { - continue; - } - // Cas de la page d'accueil ne pas dupliquer l'URL - $pageId = ($childKey !== $this->getData(['locale', 'homePageId'])) ? $childKey : ''; - $sitemap->addUrl('/' . $childKey, $datetime); - $flag = true; - - // La sous-page est un blog - if ( - $this->getData(['page', $childKey, 'moduleId']) === 'blog' && - !empty($this->getData(['module', $childKey])) - ) { - foreach ($this->getData(['module', $childKey, 'posts']) as $articleId => $article) { - if ($this->getData(['module', $childKey, 'posts', $articleId, 'state']) === true) { - $date = $this->getData(['module', $childKey, 'posts', $articleId, 'publishedOn']); - $sitemap->addUrl('/' . $childKey . '/' . $articleId, new DateTime("@{$date}", new DateTimeZone($timezone))); - } - } - } - } - } - - if ($flag === false) { - return false; - } - - // Flush all stored urls from memory to the disk and close all necessary tags. - $sitemap->flush(); - - // Move flushed files to their final location. Compress if the option is enabled. - $sitemap->finalize(); - - // Update robots.txt file in output directory - - if ($this->getData(['config', 'seo', 'robots']) === true) { - if (file_exists('robots.txt')) { - unlink('robots.txt'); - } - $sitemap->updateRobots(); - } else { - $this->secure_file_put_contents('robots.txt', 'User-agent: *' . PHP_EOL . 'Disallow: /'); - } - - // Submit your sitemaps to Google, Yahoo, Bing and Ask.com - if (empty($this->getData(['config', 'proxyType']) . $this->getData(['config', 'proxyUrl']) . ':' . $this->getData(['config', 'proxyPort']))) { - $sitemap->submitSitemap(); - } - - return (file_exists('sitemap.xml') && file_exists('robots.txt')); - - } + public function updateSitemap() + { + // Le drapeau prend true quand au moins une page est trouvée + $flag = false; + + // Rafraîchit la liste des pages après une modification de pageId notamment + $this->buildHierarchy(); + + // Actualise la liste des pages pour TinyMCE + $this->tinyMcePages(); + + //require_once 'core/vendor/sitemap/SitemapGenerator.php'; + + $timezone = $this->getData(['config', 'timezone']); + $outputDir = getcwd(); + $sitemap = new \Icamys\SitemapGenerator\SitemapGenerator(helper::baseurl(false), $outputDir); + + // will create also compressed (gzipped) sitemap : option buguée + // $sitemap->enableCompression(); + + // determine how many urls should be put into one file + // according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html) + $sitemap->setMaxUrlsPerSitemap(50000); + + // sitemap file name + $sitemap->setSitemapFileName('sitemap.xml'); + + + // Set the sitemap index file name + $sitemap->setSitemapIndexFileName('sitemap-index.xml'); + + $datetime = new DateTime(date('c')); + $datetime->format(DateTime::ATOM); // Updated ISO8601 + + foreach ($this->getHierarchy() as $parentPageId => $childrenPageIds) { + // Exclure les barres et les pages non publiques et les pages masquées + if ( + $this->getData(['page', $parentPageId, 'group']) !== 0 || + $this->getData(['page', $parentPageId, 'block']) === 'bar' + ) { + continue; + } + // Page désactivée, traiter les sous-pages sans prendre en compte la page parente. + if ($this->getData(['page', $parentPageId, 'disable']) !== true) { + // Cas de la page d'accueil ne pas dupliquer l'URL + $pageId = ($parentPageId !== $this->getData(['locale', 'homePageId'])) ? $parentPageId : ''; + $sitemap->addUrl('/' . $pageId, $datetime); + $flag = true; + } + // Articles du blog + if ( + $this->getData(['page', $parentPageId, 'moduleId']) === 'blog' + && !empty($this->getData(['module', $parentPageId])) + && $this->getData(['module', $parentPageId, 'posts']) + ) { + foreach ($this->getData(['module', $parentPageId, 'posts']) as $articleId => $article) { + if ($this->getData(['module', $parentPageId, 'posts', $articleId, 'state']) === true) { + $date = $this->getData(['module', $parentPageId, 'posts', $articleId, 'publishedOn']); + $sitemap->addUrl('/' . $parentPageId . '/' . $articleId, DateTime::createFromFormat('U', $date)); + } + } + } + // Sous-pages + foreach ($childrenPageIds as $childKey) { + if ($this->getData(['page', $childKey, 'group']) !== 0 || $this->getData(['page', $childKey, 'disable']) === true) { + continue; + } + // Cas de la page d'accueil ne pas dupliquer l'URL + $pageId = ($childKey !== $this->getData(['locale', 'homePageId'])) ? $childKey : ''; + $sitemap->addUrl('/' . $childKey, $datetime); + $flag = true; + + // La sous-page est un blog + if ( + $this->getData(['page', $childKey, 'moduleId']) === 'blog' && + !empty($this->getData(['module', $childKey])) + ) { + foreach ($this->getData(['module', $childKey, 'posts']) as $articleId => $article) { + if ($this->getData(['module', $childKey, 'posts', $articleId, 'state']) === true) { + $date = $this->getData(['module', $childKey, 'posts', $articleId, 'publishedOn']); + $sitemap->addUrl('/' . $childKey . '/' . $articleId, new DateTime("@{$date}", new DateTimeZone($timezone))); + } + } + } + } + } + + if ($flag === false) { + return false; + } + + // Flush all stored urls from memory to the disk and close all necessary tags. + $sitemap->flush(); + + // Move flushed files to their final location. Compress if the option is enabled. + $sitemap->finalize(); + + // Update robots.txt file in output directory + + if ($this->getData(['config', 'seo', 'robots']) === true) { + if (file_exists('robots.txt')) { + unlink('robots.txt'); + } + $sitemap->updateRobots(); + } else { + $this->secure_file_put_contents('robots.txt', 'User-agent: *' . PHP_EOL . 'Disallow: /'); + } + + // Submit your sitemaps to Google, Yahoo, Bing and Ask.com + if (empty($this->getData(['config', 'proxyType']) . $this->getData(['config', 'proxyUrl']) . ':' . $this->getData(['config', 'proxyPort']))) { + $sitemap->submitSitemap(); + } + + return (file_exists('sitemap.xml') && file_exists('robots.txt')); + } /* @@ -1505,18 +1495,67 @@ class common /** - * Journalisation + * Journalisation avec gestion de la taille maximale et compression */ public function saveLog($message = '') { - // Journalisation - $dataLog = helper::dateUTF8('%Y%m%d', time(), self::$i18nUI) . ';' . helper::dateUTF8('%H:%M', time(), self::$i18nUI) . ';'; + // Chemin du fichier journal + $logFile = self::DATA_DIR . 'journal.log'; + + // Vérifier la taille du fichier + if (file_exists($logFile) && filesize($logFile) > self::LOG_MAXSIZE) { + $this->rotateLogFile(); + } + + // Création de l'entrée de journal + $dataLog = helper::dateUTF8('%Y%m%d', time(), self::$i18nUI) . ';' . + helper::dateUTF8('%H:%M', time(), self::$i18nUI) . ';'; $dataLog .= helper::getIp($this->getData(['config', 'connect', 'anonymousIp'])) . ';'; $dataLog .= empty($this->getUser('id')) ? 'visitor;' : $this->getUser('id') . ';'; $dataLog .= $message ? $this->getUrl() . ';' . $message : $this->getUrl(); $dataLog .= PHP_EOL; + + // Écriture dans le fichier si la journalisation est activée if ($this->getData(['config', 'connect', 'log'])) { - $this->secure_file_put_contents(self::DATA_DIR . 'journal.log', $dataLog, FILE_APPEND); + file_put_contents($logFile, $dataLog, FILE_APPEND); + } + } + + /** + * Gère la rotation et la compression des fichiers journaux + */ + private function rotateLogFile() + { + $logFile = self::DATA_DIR . 'journal.log'; + + // Décaler tous les fichiers d'archive existants + for ($i = self::LOG_MAXARCHIVE - 1; $i > 0; $i--) { + $oldFile = self::DATA_DIR . 'journal-' . $i . '.log.gz'; + $newFile = self::DATA_DIR . 'journal-' . ($i + 1) . '.log.gz'; + + if (file_exists($oldFile)) { + if ($i == self::LOG_MAXARCHIVE - 1) { + unlink($oldFile); // Supprimer le plus ancien + } else { + rename($oldFile, $newFile); + } + } + } + + // Compresser le fichier journal actuel + if (file_exists($logFile)) { + $gz = gzopen(self::DATA_DIR . 'journal-1.log.gz', 'w9'); + $handle = fopen($logFile, 'r'); + + while (!feof($handle)) { + gzwrite($gz, fread($handle, 8192)); + } + + fclose($handle); + gzclose($gz); + + // Créer un nouveau fichier journal vide + file_put_contents($logFile, ''); } }