Files
KantSites/src/Controller/StatisticsController.php
T
kantarciandClaude Opus 5 aaea385422 Die Verwaltung kann umziehen: /admin ist frei waehlbar
php bin/console cms:admin:path <neu> legt die Verwaltung auf eine
andere Adresse. Geprueft wird, dass der Name frei ist und keine Seite
verdeckt; der Wert steht in .env.local und fuellt einen Parameter, der
als Praefix in jeder Verwaltungsroute steht, in der Firewall, in den
Schutzkopfzeilen und in der Statistik.

Die robots.txt nennt einen geaenderten Namen nicht - sie ist oeffentlich
und waere sonst ein Wegweiser. Beim Standardnamen bleibt die Zeile,
weil diese Adresse ohnehin jeder kennt.

Die Auffangroute fuer Seiten steht jetzt zuletzt: sie kann einen frei
gewaehlten Namen nicht per Regex ausschliessen, und die Reihenfolge ist
ohnehin die richtige. Der Befehl raeumt den Zwischenspeicher auf, aber
nicht den, in dem er selbst laeuft.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 20:51:55 +02:00

64 lines
2.5 KiB
PHP

<?php
namespace App\Controller;
use App\Content\Statistics;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\Routing\Attribute\Route;
final class StatisticsController extends AbstractController
{
private const RANGES = [7 => '7 Tage', 30 => '30 Tage', 90 => '90 Tage', 365 => '1 Jahr'];
public function __construct(private Statistics $statistics)
{
}
#[Route('/%kant.admin_path%/statistik', name: 'admin_statistics', methods: ['GET'])]
public function index(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_EDITOR');
$range = isset(self::RANGES[$request->query->getInt('tage')]) ? $request->query->getInt('tage') : 30;
return $this->render('admin/statistics.html.twig', [
'stats' => $this->statistics->overview($range),
'ranges' => self::RANGES,
'range' => $range,
'compactAfter' => Statistics::COMPACT_AFTER_DAYS,
'keepDays' => Statistics::KEEP_DAYS,
]);
}
#[Route('/%kant.admin_path%/statistik.csv', name: 'admin_statistics_csv', methods: ['GET'])]
public function csv(Request $request): Response
{
$this->denyAccessUnlessGranted('ROLE_EDITOR');
$range = isset(self::RANGES[$request->query->getInt('tage')]) ? $request->query->getInt('tage') : 30;
// Streamed, because a year of counters is a lot of rows and none of them need to be held
// in memory at once.
$response = new StreamedResponse(function () use ($range): void {
$out = fopen('php://output', 'w');
fputcsv($out, ['tag', 'stunde', 'pfad', 'status', 'verweis-domain', 'anzahl'], ';', '"', '');
foreach ($this->statistics->export($range) as $row) {
fputcsv($out, [
$row['day'],
-1 === (int) $row['hour'] ? 'ganzer Tag' : $row['hour'],
$row['path'],
$row['status'],
$row['referrer_host'],
$row['count'],
], ';', '"', '');
}
fclose($out);
});
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
$response->headers->set('Content-Disposition', sprintf('attachment; filename="kantsites-statistik-%d-tage.csv"', $range));
return $response;
}
}