feat : sparklines 14j stats + filtre IPs LAN (v1.6.27)

- Admin stats : sparklines SVG par page (120×28 px, courbe + dégradé),
  carte « Pages les plus visitées » en pleine largeur
- AccessLogParser : données par jour (pages_by_day) sur 14 jours
- AccessLogParser : IPs privées/LAN exclues de la répartition réseau
- ArticleManager : suppression opérateur nullsafe superflu (PHPStan)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-19 18:20:19 +02:00
parent 007895d24a
commit e8b361e720
6 changed files with 154 additions and 75 deletions
+38 -15
View File
@@ -30,7 +30,7 @@ class AccessLogParser
}
/**
* @return array{pages:array<string,int>,books:array<string,int>,ips:array<string,int>}
* @return array{pages:array<string,int>,books:array<string,int>,ips:array<string,int>,pages_by_day:array<string,list<int>>}
*/
public function stats(): array
{
@@ -44,20 +44,34 @@ class AccessLogParser
}
}
$cutoff = strtotime("-{$this->days} days midnight") ?: (time() - $this->days * 86400);
$pages = [];
$books = [];
$ips = [];
$cutoff = strtotime("-{$this->days} days midnight") ?: (time() - $this->days * 86400);
$pages = [];
$books = [];
$ips = [];
$dayPages = []; // [path => [dayOffset => count]], dayOffset 0=oldest
foreach ($this->logFiles() as $file) {
$this->parseFile($file, $cutoff, $pages, $books, $ips);
$this->parseFile($file, $cutoff, $pages, $books, $ips, $dayPages);
}
arsort($pages);
arsort($books);
arsort($ips);
$result = compact('pages', 'books', 'ips');
// Normalise dayPages : pour chaque page, tableau de $this->days entiers (index 0 = le plus ancien)
$pagesByDay = [];
$today = (int) strtotime('today midnight');
foreach ($dayPages as $path => $byOffset) {
$arr = array_fill(0, $this->days, 0);
foreach ($byOffset as $offset => $count) {
if ($offset >= 0 && $offset < $this->days) {
$arr[$offset] = $count;
}
}
$pagesByDay[$path] = $arr;
}
$result = ['pages' => $pages, 'books' => $books, 'ips' => $ips, 'pages_by_day' => $pagesByDay];
@mkdir(dirname($this->cacheFile), 0755, true);
@file_put_contents($this->cacheFile, json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
return self::$memo = $result;
@@ -113,7 +127,7 @@ class AccessLogParser
return (int) strtotime("{$m[1]} {$m[2]} {$m[3]} {$m[4]} {$m[5]}");
}
private function parseLine(string $line, int $cutoff, array &$pages, array &$books, array &$ips): void
private function parseLine(string $line, int $cutoff, array &$pages, array &$books, array &$ips, array &$dayPages): void
{
if (!preg_match(self::RE, $line, $m)) {
return;
@@ -123,20 +137,29 @@ class AccessLogParser
if ($status !== '200') {
return;
}
if (self::parseTimestamp($ts) < $cutoff) {
$tsVal = self::parseTimestamp($ts);
if ($tsVal < $cutoff) {
return;
}
$publicIp = filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false;
if (str_starts_with($path, '/post/') && strlen($path) > 6) {
$pages[$path] = ($pages[$path] ?? 0) + 1;
$ips[$ip] = ($ips[$ip] ?? 0) + 1;
if ($publicIp) {
$ips[$ip] = ($ips[$ip] ?? 0) + 1;
}
$dayOffset = (int) floor(($tsVal - $cutoff) / 86400);
$dayPages[$path][$dayOffset] = ($dayPages[$path][$dayOffset] ?? 0) + 1;
} elseif (str_starts_with($path, '/book/') && strlen($path) > 6) {
$books[$path] = ($books[$path] ?? 0) + 1;
$ips[$ip] = ($ips[$ip] ?? 0) + 1;
if ($publicIp) {
$ips[$ip] = ($ips[$ip] ?? 0) + 1;
}
}
}
private function parseFile(array $file, int $cutoff, array &$pages, array &$books, array &$ips): void
private function parseFile(array $file, int $cutoff, array &$pages, array &$books, array &$ips, array &$dayPages): void
{
if ($file['type'] === 'tgz') {
try {
@@ -147,7 +170,7 @@ class AccessLogParser
continue;
}
foreach (explode("\n", $content) as $line) {
$this->parseLine($line, $cutoff, $pages, $books, $ips);
$this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages);
}
}
} catch (\Exception $e) {
@@ -160,7 +183,7 @@ class AccessLogParser
while (!gzeof($h)) {
$line = gzgets($h, 8192);
if ($line !== false) {
$this->parseLine($line, $cutoff, $pages, $books, $ips);
$this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages);
}
}
gzclose($h);
@@ -170,7 +193,7 @@ class AccessLogParser
return;
}
while (($line = fgets($h)) !== false) {
$this->parseLine($line, $cutoff, $pages, $books, $ips);
$this->parseLine($line, $cutoff, $pages, $books, $ips, $dayPages);
}
fclose($h);
}
+7 -7
View File
@@ -293,7 +293,7 @@ class ArticleManager
}
$meta['updated_at'] = date('Y-m-d H:i:s');
$this->writeMeta($dir, $meta);
$this->git?->commit("meta: " . ($meta['title'] ?? $uuid));
$this->git?->commit('meta: ' . ($meta['title'] ?? $uuid));
}
public function saveDraftOverlay(string $uuid, array $metaFields, ?string $content = null): void
@@ -379,7 +379,7 @@ class ArticleManager
@unlink($dir . '/draft_overlay.json');
@unlink($dir . '/draft_overlay.md');
if ($title !== null) {
$this->git?->commit("discard-draft: $title");
$this->git->commit("discard-draft: $title");
}
}
@@ -488,7 +488,7 @@ class ArticleManager
}
$meta['cover'] = $coverName;
$this->writeMeta($this->dataDir . '/' . $uuid, $meta);
$this->git?->commit("cover: " . ($article['title'] ?? $uuid));
$this->git?->commit('cover: ' . ($article['title'] ?? $uuid));
}
public function addFileFromUrl(string $uuid, string $url, bool $isCover = false, string $author = '', string $sourceUrl = '', string $title = '', array $extraMeta = []): ?string
@@ -775,7 +775,7 @@ class ArticleManager
$this->tagTypesPath(),
json_encode($types, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) . "\n"
);
$this->git?->commit("tag-types");
$this->git?->commit('tag-types');
}
/** Enregistre les tags d'un article directement (utile pour les scripts de migration). */
@@ -795,7 +795,7 @@ class ArticleManager
$meta['tags'] = $this->normalizeTags($tags);
$this->writeMeta($dir, $meta);
$this->rebuildSearchIndex();
$this->git?->commit("tags: " . ($meta['title'] ?? $uuid));
$this->git?->commit('tags: ' . ($meta['title'] ?? $uuid));
}
/** @return list<string> Toutes les valeurs distinctes d'un type de tag, triées. */
@@ -845,7 +845,7 @@ class ArticleManager
$this->writeMeta($dir, $meta);
$this->allCache = null;
@unlink($this->articleCachePath($uuid));
$this->git?->commit("featured: " . ($meta['title'] ?? $uuid) . " (" . ($featured ? 'on' : 'off') . ")");
$this->git?->commit('featured: ' . ($meta['title'] ?? $uuid) . ' (' . ($featured ? 'on' : 'off') . ')');
}
public function delete(string $uuid): bool
@@ -871,7 +871,7 @@ class ArticleManager
}
$this->rebuildSearchIndex();
$this->rebuildBacklinksCache();
$this->git?->commit("delete: " . ($title ?? $uuid));
$this->git?->commit('delete: ' . ($title ?? $uuid));
return true;
}