feat: section articles proches dans la vue article

This commit is contained in:
Cedric Abonnel
2026-05-12 22:41:48 +02:00
parent b81750616f
commit f273c3fa6a
2 changed files with 66 additions and 0 deletions
+45
View File
@@ -566,6 +566,51 @@ switch ($action) {
$categorySidebar[$aCat][] = $a;
}
}
// Articles proches : un search par mot du titre → OR implicite, cumul des scores
$similarArticles = [];
require_once BASE_PATH . '/src/SearchEngine.php';
$_simEngine = new SearchEngine();
$_relatedUuids = array_column($relatedArticles, 'uuid');
$_stopWords = ['avec', 'dans', 'pour', 'une', 'les', 'des', 'sur', 'par', 'qui', 'que',
'tout', 'mais', 'donc', 'comment', 'quand', 'plus', 'cette', 'cet', 'ces',
'mon', 'ton', 'son', 'notre', 'votre', 'leur', 'tres', 'bien', 'fait',
'aussi', 'comme', 'plus', 'sans', 'sous', 'entre', 'vers', 'chez'];
$_simPool = array_values(array_filter(
$_allPublished,
static function (array $a) use ($article, $privateCats, $_relatedUuids): bool {
if ($a['uuid'] === $article['uuid']) {
return false;
}
if (in_array($a['uuid'], $_relatedUuids, true)) {
return false;
}
if (strtotime((string)($a['published_at'] ?? '')) > time() && !isLoggedIn()) {
return false;
}
$cat = trim($a['category'] ?? '');
return $cat === '' || !in_array($cat, $privateCats, true) || isLoggedIn();
}
));
$_titleWords = array_unique(array_values(array_filter(
preg_split('/\W+/u', mb_strtolower($article['title']), -1, PREG_SPLIT_NO_EMPTY) ?: [],
fn ($w) => mb_strlen($w) >= 4 && !in_array($w, $_stopWords, true)
)));
$_scoreMap = [];
$_articleMap = [];
foreach ($_titleWords as $_word) {
foreach ($_simEngine->search($_word, $_simPool) as $_r) {
$_uuid = $_r['article']['uuid'];
$_scoreMap[$_uuid] = ($_scoreMap[$_uuid] ?? 0.0) + $_r['score'];
$_articleMap[$_uuid] = $_r['article'];
}
}
arsort($_scoreMap);
$similarArticles = array_map(
fn ($uuid) => $_articleMap[$uuid],
array_slice(array_keys($_scoreMap), 0, 5)
);
unset($_simEngine, $_simPool, $_titleWords, $_stopWords, $_scoreMap, $_articleMap, $_relatedUuids);
unset($_allPublished);
include BASE_PATH . '/templates/post_view.php';
+21
View File
@@ -258,6 +258,27 @@ $hasSources = (!empty($externalLinks) || !empty($files))
<p class="text-muted small">Aucun autre article dans cette catégorie.</p>
<?php endif; ?>
<?php if (!empty($similarArticles ?? [])): ?>
<h6 class="related-sidebar-title mt-4">Articles proches</h6>
<?php foreach ($similarArticles as $sim):
$simCover = $sim['cover'] ?? '';
$simCat = trim($sim['category'] ?? '');
$simGradient = coverGradient($simCat !== '' ? $simCat : $sim['uuid'], $allCats ?? []);
$simDate = date('d/m/Y', strtotime((string)($sim['published_at'] ?? $sim['created_at'] ?? '')));
?>
<a href="/post/<?= rawurlencode($sim['slug'] ?? '') ?>" class="related-card">
<div class="related-card-thumb" style="<?= $simCover !== ''
? 'background-image:url(/file?uuid=' . rawurlencode($sim['uuid']) . '&name=' . rawurlencode($simCover) . ');background-size:cover;background-position:center'
: 'background:' . htmlspecialchars($simGradient) ?>">
</div>
<div class="related-card-body">
<div class="related-card-title"><?= htmlspecialchars($sim['title']) ?></div>
<div class="related-card-date"><?= $simDate ?></div>
</div>
</a>
<?php endforeach; ?>
<?php endif; ?>
</aside>
</div>