feat: impact-media page type with OG scraping and cache
All checks were successful
Deploy / Deploy to Production (push) Successful in 12s
All checks were successful
Deploy / Deploy to Production (push) Successful in 12s
- Add impact-media blueprint (entries field + linked investigation) - Add minimal impact-media template - Refactor card-open-graph snippet: accepts $url param, Kirby cache (6h TTL), decode HTML entities, empty alt on images - Update impacts.yml to allow impact-media pages - Render impact-media in investigation aside with dynamic count + details/summary - Add OG cache config in config.php - CSS formatting fixes (body, card-block-small, category) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
b8352c81fa
commit
f757906584
11 changed files with 505 additions and 452 deletions
16
site/blueprints/pages/impact-media.yml
Normal file
16
site/blueprints/pages/impact-media.yml
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
title: Impact médiatique
|
||||
|
||||
fields:
|
||||
linkedInvestigation:
|
||||
label: Enquête liée
|
||||
type: pages
|
||||
query: page('enquetes').children.listed
|
||||
multiple: false
|
||||
layout: list
|
||||
text: "{{ page.title }}"
|
||||
info: "{{ page.created }}"
|
||||
entries:
|
||||
label: Articles et reprises
|
||||
type: entries
|
||||
field:
|
||||
type: url
|
||||
|
|
@ -17,8 +17,9 @@ tabs:
|
|||
impactsSection:
|
||||
type: pages
|
||||
label: Liste des impacts
|
||||
template: impact
|
||||
create: impact
|
||||
templates:
|
||||
- impact
|
||||
- impact-media
|
||||
layout: list
|
||||
text: "{{ page.title }}"
|
||||
info: "{{ page.category }} — {{ page.created.toDate('d/m/Y') }}"
|
||||
|
|
|
|||
|
|
@ -76,6 +76,10 @@ return [
|
|||
],
|
||||
'tobimori.seo.canonicalBase' => 'https://www.index.ngo',
|
||||
|
||||
'cache' => [
|
||||
'og' => true,
|
||||
],
|
||||
|
||||
'routes' => [
|
||||
require(__DIR__ . '/routes/newsletter.php'),
|
||||
],
|
||||
|
|
|
|||
|
|
@ -1,111 +1,109 @@
|
|||
<?php
|
||||
// URL à scraper (remplacez par l'URL souhaitée)
|
||||
$url = 'https://www.lemonde.fr/societe/article/2025/07/16/au-c-ur-des-emeutes-de-nouvelle-caledonie-quand-la-mort-de-banane-abattu-par-le-gign-lance-le-siege-de-saint-louis_6621496_3224.html';
|
||||
<?php
|
||||
/**
|
||||
* Card Open Graph snippet
|
||||
* Fetches and displays OG data from an external URL with cache.
|
||||
*
|
||||
* @var string $url The URL to scrape OG data from
|
||||
*/
|
||||
|
||||
// Fonction pour récupérer les données Open Graph
|
||||
function getOpenGraphData($url) {
|
||||
$ogData = [
|
||||
'title' => '',
|
||||
'description' => '',
|
||||
'image' => '',
|
||||
'site_name' => '',
|
||||
'url' => $url
|
||||
];
|
||||
if (empty($url)) return;
|
||||
|
||||
// Configuration du contexte pour éviter les erreurs SSL
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'GET',
|
||||
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'timeout' => 10
|
||||
],
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false
|
||||
]
|
||||
]);
|
||||
$cache = kirby()->cache('og');
|
||||
$cacheKey = md5($url);
|
||||
$ogData = $cache->get($cacheKey);
|
||||
|
||||
// Récupérer le HTML
|
||||
$html = @file_get_contents($url, false, $context);
|
||||
if ($ogData === null) {
|
||||
$ogData = [
|
||||
'title' => '',
|
||||
'description' => '',
|
||||
'image' => '',
|
||||
'site_name' => '',
|
||||
'url' => $url,
|
||||
];
|
||||
|
||||
if ($html === false) {
|
||||
return $ogData;
|
||||
}
|
||||
$context = stream_context_create([
|
||||
'http' => [
|
||||
'method' => 'GET',
|
||||
'header' => 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
|
||||
'timeout' => 10,
|
||||
],
|
||||
'ssl' => [
|
||||
'verify_peer' => false,
|
||||
'verify_peer_name' => false,
|
||||
],
|
||||
]);
|
||||
|
||||
// Parser les meta tags Open Graph
|
||||
preg_match_all('/<meta\s+property=["\']og:([^"\']+)["\']\s+content=["\']([^"\']+)["\']/i', $html, $matches);
|
||||
$html = @file_get_contents($url, false, $context);
|
||||
|
||||
if (!empty($matches[1])) {
|
||||
foreach ($matches[1] as $index => $property) {
|
||||
$content = $matches[2][$index];
|
||||
switch ($property) {
|
||||
case 'title':
|
||||
$ogData['title'] = htmlspecialchars($content);
|
||||
break;
|
||||
case 'description':
|
||||
$ogData['description'] = htmlspecialchars($content);
|
||||
break;
|
||||
case 'image':
|
||||
$ogData['image'] = $content;
|
||||
break;
|
||||
case 'site_name':
|
||||
$ogData['site_name'] = htmlspecialchars($content);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($html !== false) {
|
||||
// Parse OG meta tags (both property...content and content...property orders)
|
||||
preg_match_all('/<meta\s+(?:property=["\']og:([^"\']+)["\']\s+content=["\']([^"\']*)["\']|content=["\']([^"\']*?)["\']\s+property=["\']og:([^"\']+)["\'])/i', $html, $matches);
|
||||
|
||||
// Fallback: si pas de og:title, utiliser <title>
|
||||
if (empty($ogData['title'])) {
|
||||
preg_match('/<title>([^<]+)<\/title>/i', $html, $titleMatch);
|
||||
if (!empty($titleMatch[1])) {
|
||||
$ogData['title'] = htmlspecialchars($titleMatch[1]);
|
||||
}
|
||||
}
|
||||
if (!empty($matches[1])) {
|
||||
foreach ($matches[1] as $index => $property) {
|
||||
$prop = $property ?: $matches[4][$index];
|
||||
$content = $matches[2][$index] ?: $matches[3][$index];
|
||||
$content = html_entity_decode(trim($content), ENT_QUOTES | ENT_HTML5, 'UTF-8');
|
||||
switch ($prop) {
|
||||
case 'title':
|
||||
$ogData['title'] = $content;
|
||||
break;
|
||||
case 'description':
|
||||
$ogData['description'] = $content;
|
||||
break;
|
||||
case 'image':
|
||||
$ogData['image'] = $content;
|
||||
break;
|
||||
case 'site_name':
|
||||
$ogData['site_name'] = $content;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: si pas de site_name, utiliser le domaine
|
||||
if (empty($ogData['site_name'])) {
|
||||
$parsed = parse_url($url);
|
||||
$ogData['site_name'] = $parsed['host'] ?? '';
|
||||
}
|
||||
// Fallback: use <title> if no og:title
|
||||
if (empty($ogData['title'])) {
|
||||
preg_match('/<title>([^<]+)<\/title>/i', $html, $titleMatch);
|
||||
if (!empty($titleMatch[1])) {
|
||||
$ogData['title'] = html_entity_decode($titleMatch[1], ENT_QUOTES, 'UTF-8');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $ogData;
|
||||
}
|
||||
// Always use domain as site_name
|
||||
$parsed = parse_url($url);
|
||||
$ogData['site_name'] = $parsed['host'] ?? '';
|
||||
|
||||
$ogData = getOpenGraphData($url);
|
||||
$cache->set($cacheKey, $ogData, 360); // 6 hours
|
||||
}
|
||||
?>
|
||||
|
||||
// Toujours utiliser le domaine de l'URL pour site_name
|
||||
$parsed = parse_url($url);
|
||||
$ogData['site_name'] = $parsed['host'] ?? '';
|
||||
?>
|
||||
<div class="card--open-graph">
|
||||
<div class="open-graph__inner">
|
||||
|
||||
<div class="card--open-graph">
|
||||
<div class="open-graph__inner">
|
||||
<?php if (!empty($ogData['image'])): ?>
|
||||
<figure>
|
||||
<img src="<?= htmlspecialchars($ogData['image']) ?>" alt="">
|
||||
</figure>
|
||||
<?php endif ?>
|
||||
|
||||
<?php if (!empty($ogData['image'])): ?>
|
||||
<figure>
|
||||
<img src="<?= $ogData['image'] ?>" alt="<?= $ogData['title'] ?>">
|
||||
</figure>
|
||||
<?php endif; ?>
|
||||
<div class="content">
|
||||
<?php if (!empty($ogData['site_name'])): ?>
|
||||
<span class="site-name"><?= htmlspecialchars($ogData['site_name']) ?></span>
|
||||
<?php endif ?>
|
||||
|
||||
<div class="content">
|
||||
<?php if (!empty($ogData['site_name'])): ?>
|
||||
<span class="site-name"><?= $ogData['site_name'] ?></span>
|
||||
<?php endif; ?>
|
||||
<?php if (!empty($ogData['title'])): ?>
|
||||
<h3 class="title">
|
||||
<a href="<?= htmlspecialchars($ogData['url']) ?>" target="_blank">
|
||||
<?= htmlspecialchars($ogData['title']) ?>
|
||||
</a>
|
||||
</h3>
|
||||
<?php endif ?>
|
||||
|
||||
<?php if (!empty($ogData['title'])): ?>
|
||||
|
||||
<h3 class="title">
|
||||
<a href="<?= $ogData['url'] ?>" target="_blank">
|
||||
<?= $ogData['title'] ?>
|
||||
</a>
|
||||
</h3>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($ogData['description'])): ?>
|
||||
<p class="description"><?= $ogData['description'] ?></p>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<a class="link-block" href="<?= $ogData['url'] ?>" target="_blank"></a>
|
||||
</div>
|
||||
</div>
|
||||
<?php if (!empty($ogData['description'])): ?>
|
||||
<p class="description"><?= htmlspecialchars($ogData['description']) ?></p>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
<a class="link-block" href="<?= htmlspecialchars($ogData['url']) ?>" target="_blank"></a>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
47
site/templates/impact-media.php
Normal file
47
site/templates/impact-media.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php snippet('header') ?>
|
||||
|
||||
<main class="main__single">
|
||||
|
||||
<header class="page__header">
|
||||
<p class="page__type">Impact</p>
|
||||
<p class="page__category">Médiatique</p>
|
||||
<h2 class="page__title"><?= $page->title()->esc() ?></h2>
|
||||
</header>
|
||||
|
||||
<?php
|
||||
$entries = $page->entries()->toEntries();
|
||||
$investigation = $page->linkedInvestigation()->toPages()->first();
|
||||
?>
|
||||
|
||||
<?php if ($entries->isNotEmpty()): ?>
|
||||
<div class="page__content">
|
||||
<?php foreach ($entries as $entry): ?>
|
||||
<?php snippet('card-open-graph', ['url' => $entry->value()]) ?>
|
||||
<?php endforeach ?>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
|
||||
<?php if ($investigation): ?>
|
||||
<aside class="page__aside" id="related-investigations">
|
||||
<h3 class="aside__title">Enquête liée</h3>
|
||||
|
||||
<article class="card--article-small">
|
||||
<?php if ($cover = $investigation->cover()->toFile()): ?>
|
||||
<figure>
|
||||
<img src="<?= $cover->url() ?>" alt="<?= $investigation->title()->esc() ?>">
|
||||
</figure>
|
||||
<?php endif ?>
|
||||
<div class="content">
|
||||
<p class="type">Enquête</p>
|
||||
<h4 class="title"><a href="<?= $investigation->url() ?>"><?= $investigation->title()->esc() ?></a></h4>
|
||||
</div>
|
||||
<button class="btn--go-to"><a href="<?= $investigation->url() ?>"><?= svg('assets/icons/arrow-left.svg') ?></a></button>
|
||||
<a class="link-block" href="<?= $investigation->url() ?>" aria-hidden="true"></a>
|
||||
</article>
|
||||
</aside>
|
||||
<?php endif ?>
|
||||
|
||||
</main>
|
||||
|
||||
<?php snippet('bottom-bar') ?>
|
||||
<?php snippet('footer') ?>
|
||||
|
|
@ -303,19 +303,6 @@ $dateLocale = substr(is_array($locale) ? reset($locale) : $locale, 0, 5);
|
|||
</div>
|
||||
|
||||
|
||||
<!-- <div class="card--block-small" data-impact-type="media">
|
||||
|
||||
<p class="category">Médiatique</p>
|
||||
<div class="content">
|
||||
<p>12 articles et reprises</p>
|
||||
<p>1.5M de vues cumulées</p>
|
||||
</div>
|
||||
<details class="open-graph__details">
|
||||
<summary><p class="summary-inner">Détails <span class="arrow-details"><?= svg('assets/icons/arrow-details.svg') ?></span><p></summary>
|
||||
<?php snippet('card-open-graph') ?>
|
||||
</details>
|
||||
</div> -->
|
||||
|
||||
|
||||
<?php
|
||||
$resourcesParent = site()->find('ressources');
|
||||
|
|
@ -361,6 +348,9 @@ $resources = $resourcesParent ? $resourcesParent->children()->listed()->filter(f
|
|||
<?php
|
||||
$impactsParent = site()->find('impacts');
|
||||
$impacts = $impactsParent ? $impactsParent->children()->listed()->filter(function($impact) use ($page) {
|
||||
if ($impact->intendedTemplate()->name() === 'impact-media') {
|
||||
return $impact->linkedInvestigation()->toPages()->has($page);
|
||||
}
|
||||
return $impact->linkedInvestigations()->toPages()->has($page);
|
||||
}) : null;
|
||||
?>
|
||||
|
|
@ -370,6 +360,23 @@ $impacts = $impactsParent ? $impactsParent->children()->listed()->filter(functio
|
|||
<h3 class="aside__title"><?= t('impacts.title') ?></h3>
|
||||
|
||||
<?php foreach ($impacts as $impact): ?>
|
||||
<?php if ($impact->intendedTemplate()->name() === 'impact-media'): ?>
|
||||
<?php $entries = $impact->entries()->toEntries(); ?>
|
||||
<div class="card--block-small" data-impact-type="media">
|
||||
<p class="type">Médiatique</p>
|
||||
<div class="content">
|
||||
<p><?= $entries->count() ?> article<?= $entries->count() > 1 ? 's' : '' ?> et reprise<?= $entries->count() > 1 ? 's' : '' ?></p>
|
||||
</div>
|
||||
<?php if ($entries->isNotEmpty()): ?>
|
||||
<details class="open-graph__details">
|
||||
<summary><p class="summary-inner">Détails <span class="arrow-details"><?= svg('assets/icons/arrow-details.svg') ?></span></p></summary>
|
||||
<?php foreach ($entries as $entry): ?>
|
||||
<?php snippet('card-open-graph', ['url' => $entry->value()]) ?>
|
||||
<?php endforeach ?>
|
||||
</details>
|
||||
<?php endif ?>
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div class="card--block-small has-link">
|
||||
<div class="group-top">
|
||||
<p class="type"><?= t('impact.type') ?></p>
|
||||
|
|
@ -394,6 +401,7 @@ $impacts = $impactsParent ? $impactsParent->children()->listed()->filter(functio
|
|||
|
||||
<a class="link-block" href="<?= $impact->url() ?>" aria-hidden="true"></a>
|
||||
</div>
|
||||
<?php endif ?>
|
||||
<?php endforeach ?>
|
||||
|
||||
</aside>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue