agnes-dot-love-pureblog/content/autosaves/post-creating-a-stats-page-in-pureblog.json

1 line
4.7 KiB
JSON
Raw Normal View History

2026-11-18 03:01:10 +00:00
{"timestamp":1791058466,"title":"Creating A Stats Page In PureBlog","content":"I recently started using PureBlog for my main IndieWeb home and I really like it. It has every feature I need and it's simple and lightweight. I have been making some additions for it, such as an automatically updating sitemap, but here's a simple statistics page too. Basically copied from admin\/dashboard.php with some alterations.\r\n\r\n1) Go to routes and enter this:\r\n\r\n``` \/stats | \/content\/includes\/stats.php```\r\n\r\n2) Make stats.php in content\/includes on your server and the contents should be this:\r\n\r\n```<?php\r\n\r\nrequire PUREBLOG_BASE_PATH . '\/includes\/header.php';\r\nrender_masthead_layout($config, ['page' => null]);\r\n\r\n?>\r\n\r\n<main>\r\n <h1> Stats <\/h1>\r\n<?php\r\n\r\n$publishedPosts = array_values(array_filter(get_all_posts(true), static fn(array $post): bool => ($post['status'] ?? 'draft') === 'published'));\r\n$publishedCount = count($publishedPosts);\r\n$tz = site_timezone_object($config);\r\n$now = new DateTimeImmutable('now', $tz);\r\n$currentYear = (int) $now->format('Y');\r\n\r\n$publishedThisYear = 0;\r\n$totalWordsThisYear = 0;\r\n$tagCounts = [];\r\n$tagCountsThisYear = [];\r\n$tagDisplayNames = [];\r\n$totalWords = 0;\r\n$allTimeMonthCounts = array_fill(1, 12, 0);\r\n$allTimeDayCounts = array_fill(1, 7, 0); \/\/ 1=Mon … 7=Sun (ISO 8601)\r\n$yearCounts = [];\r\n\r\n\r\n$chartMonths = [];\r\n$firstOfMonth = new DateTimeImmutable($now->format('Y-m-01'), $tz);\r\nfor ($i = 11; $i >= 0; $i--) {\r\n$dt = $firstOfMonth->modify(\"-{$i} months\");\r\n$month = (int) $dt->format('n');\r\n$chartMonths[] = [\r\n'year' => (int) $dt->format('Y'),\r\n'month' => $month,\r\n'label' => t('date.months_short.' . ($month - 1)),\r\n'count' => 0,\r\n];\r\n}\r\n$chartIndex = [];\r\nforeach ($chartMonths as $idx => $cm) {\r\n$chartIndex[$cm['year'] . '-' . $cm['month']] = $idx;\r\n}\r\n$recentCutoff = $firstOfMonth->modify('-11 months')->getTimestamp();\r\n\r\nforeach ($publishedPosts as $post) {\r\n$content = (string) ($post['content'] ?? '');\r\n$totalWords += str_word_count($content);\r\n\r\n$timestamp = (int) ($post['timestamp'] ?? 0);\r\nif ($timestamp > 0) {\r\n$dt = (new DateTimeImmutable('@' . $timestamp))->setTimezone($tz);\r\n$postYear = (int) $dt->format('Y');\r\n$postMon = (int) $dt->format('n');\r\n$postDay = (int) $dt->format('N'); \/\/ 1=Mon … 7=Sun\r\n\r\n$chartKey = $postYear . '-' . $postMon;\r\nif (isset($chartIndex[$chartKey])) {\r\n$chartMonths[$chartIndex[$chartKey]]['count']++;\r\n}\r\n\r\nif ($timestamp >= $recentCutoff) {\r\n$publishedThisYear++;\r\n$totalWordsThisYear += str_word_count($content);\r\n}\r\n\r\n$allTimeMonthCounts[$postMon]++;\r\n$allTimeDayCounts[$postDay]++;\r\n$yearCounts[$postYear] = ($yearCounts[$postYear] ?? 0) + 1;\r\n}\r\n\r\n$tags = $post['tags'] ?? [];\r\nif (!is_array($tags)) {\r\ncontinue;\r\n}\r\nforeach ($tags as $tag) {\r\n$name = trim((string) $tag);\r\nif ($name !== '') {\r\n$slug = normalize_tag($name);\r\n$tagDisplayNames[$slug] ??= $name;\r\n$tagCounts[$slug] = ($tagCounts[$slug] ?? 0) + 1;\r\nif ($timestamp >= $recentCutoff) {\r\n$tagCountsThisYear[$slug] = ($tagCountsThisYear[$slug] ?? 0) + 1;\r\n}\r\n}\r\n}\r\n}\r\n\r\nksort($yearCounts);\r\n$maxYearCount = !empty($yearCounts) ? max($yearCounts) : 1;\r\n\r\n$avgWordsAllTime = $publishedCount > 0 ? (int) round($totalWords \/ $publishedCount) : 0;\r\n$avgWordsThisYear = $publishedThisYear > 0 ? (int) round($totalWordsThisYear \/ $publishedThisYear) : 0;\r\n$booksEquivalent = $totalWords > 0 ? round($totalWords \/ 80000, 1) : 0;\r\n$booksThisYear = $totalWordsThisYear > 0 ? round($totalWordsThisYear \/ 80000, 1) : 0;\r\n\r\n\r\n$lastTimestamp = 0;\r\nforeach ($publishedPosts as $post) {\r\n$ts = (int) ($post['timestamp'] ?? 0);\r\nif ($ts > $lastTimestamp) {\r\n$lastTimestamp = $ts;\r\n}\r\n}\r\n$lastPublishedAgo = $lastTimestamp > 0 ? relative_time($lastTimestamp) : t('admin.dashboard.no_posts_yet');\r\n\r\n?>\r\n<div class=\"stats\">\r\n <center>\r\n <h3> Published posts: <\/h3> <?= e(number_format