moteur de recherche : trigram+substring, navbar, page resultats

This commit is contained in:
Cedric Abonnel
2026-05-12 01:29:01 +02:00
parent 3a5ae2631d
commit f236ea24de
9 changed files with 465 additions and 74 deletions
+9
View File
@@ -89,6 +89,15 @@
else: ?>
<ul class="navbar-nav me-auto"></ul>
<?php endif; ?>
<form class="search-form d-flex" action="/" method="GET" role="search">
<input type="hidden" name="action" value="search">
<input class="form-control form-control-sm search-input"
type="search" name="q"
value="<?= htmlspecialchars($_GET['q'] ?? '') ?>"
placeholder="Rechercher…"
aria-label="Rechercher">
</form>
<ul class="navbar-nav">
<?php if (function_exists('isLoggedIn') && isLoggedIn()): ?>
<li class="nav-item"><a class="nav-link" href="/?action=admin">Admin</a></li>
+1 -1
View File
@@ -264,7 +264,7 @@ $mainClass = 'container-fluid';
// Auto-description depuis le contenu si le champ SEO est vide
$seoDescription = $article['seo_description'] ?? '';
if ($seoDescription === '') {
$plain = strip_tags((new Parsedown())->text($article['content']));
$plain = strip_tags($Parsedown->text($article['content']));
$plain = preg_replace('/\s+/', ' ', $plain);
$seoDescription = mb_strimwidth(trim((string)$plain), 0, 155, '…');
}
+65
View File
@@ -0,0 +1,65 @@
<?php ob_start(); ?>
<div class="search-page">
<form class="search-bar mb-5" action="/" method="GET" role="search">
<input type="hidden" name="action" value="search">
<div class="input-group">
<input type="search" class="form-control form-control-lg" name="q"
value="<?= htmlspecialchars($searchQuery) ?>"
placeholder="Rechercher…" autofocus autocomplete="off"
aria-label="Rechercher">
<button class="btn btn-primary" type="submit">Rechercher</button>
</div>
<div class="form-text mt-1">
Plusieurs mots = recherche ET · Fautes de frappe tolérées · Accents ignorés
</div>
</form>
<?php if ($searchQuery !== ''): ?>
<?php if (empty($searchResults)): ?>
<p class="text-muted">Aucun résultat pour <strong><?= htmlspecialchars($searchQuery) ?></strong>.</p>
<?php else: ?>
<p class="text-muted mb-4">
<?= count($searchResults) ?> résultat<?= count($searchResults) > 1 ? 's' : '' ?>
pour <strong><?= htmlspecialchars($searchQuery) ?></strong>
</p>
<div class="search-results">
<?php foreach ($searchResults as $r):
$a = $r['article'];
$postUrl = '/post/' . rawurlencode($a['slug'] ?? '');
$cat = trim($a['category'] ?? '');
$date = $a['published_at'] ? date('d/m/Y', strtotime((string)$a['published_at'])) : '';
?>
<article class="search-result">
<div class="search-result-meta">
<?php if ($cat !== ''): ?>
<a class="search-result-cat" href="/?cat=<?= rawurlencode($cat) ?>"><?= htmlspecialchars($cat) ?></a>
<?php endif; ?>
<?php if ($date !== ''): ?>
<time class="search-result-date"><?= $date ?></time>
<?php endif; ?>
</div>
<h2 class="search-result-title">
<a href="<?= htmlspecialchars($postUrl) ?>"><?= htmlspecialchars($a['title'] ?? '') ?></a>
</h2>
<?php if ($r['snippet'] !== ''): ?>
<p class="search-result-snippet"><?= $r['snippet'] ?></p>
<?php endif; ?>
</article>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endif; ?>
</div>
<?php
$content = ob_get_clean();
$title = $searchQuery !== '' ? 'Recherche : ' . $searchQuery : 'Recherche';
$metaRobots = 'noindex, follow';
$canonical = rtrim(APP_URL, '/') . '/';
include __DIR__ . '/layout.php';