This commit is contained in:
Jürgen Mummert
2025-12-23 12:23:04 +01:00
parent d77af88480
commit 093ac084bb
+41 -117
View File
@@ -2,62 +2,41 @@
namespace MummertMedia\ContaoMeilisearchBundle\EventListener; namespace MummertMedia\ContaoMeilisearchBundle\EventListener;
use Contao\Database;
class IndexPageListener class IndexPageListener
{ {
public function onIndexPage(string $content, array &$data, array &$set): void public function onIndexPage(string $content, array &$data, array &$set): void
{ {
$debug = (PHP_SAPI === 'cli'); // Marker vorhanden?
$url = $set['url'] ?? $data['url'] ?? null; if (!str_contains($content, 'MEILISEARCH_JSON')) {
if ($debug) {
echo "\n=============================\n";
echo "INDEXPAGE HOOK START\n";
echo "URL: " . ($url ?? '[no url]') . "\n";
}
if (!$url || !str_contains($content, 'MEILISEARCH_JSON')) {
if ($debug) {
echo "❌ No URL or no MEILISEARCH_JSON marker\n";
echo "INDEXPAGE HOOK END\n";
echo "=============================\n";
}
return; return;
} }
// JSON aus Kommentar extrahieren + parsen
$parsed = $this->extractMeilisearchJson($content); $parsed = $this->extractMeilisearchJson($content);
if ($parsed === null) { if ($parsed === null) {
if ($debug) {
echo "❌ Invalid JSON in MEILISEARCH_JSON\n";
echo "INDEXPAGE HOOK END\n";
echo "=============================\n";
}
return; return;
} }
if ($debug) { /*
echo "✅ MEILISEARCH_JSON parsed\n"; * =====================
var_dump($parsed); * PRIORITY (event > news > page)
} * =====================
*/
/* =====================
* PRIORITY
* ===================== */
$priority = $priority =
$parsed['event']['priority'] ?? $parsed['event']['priority'] ?? null ??
$parsed['news']['priority'] ?? $parsed['news']['priority'] ?? null ??
$parsed['page']['priority'] ?? $parsed['page']['priority'] ?? null;
null;
if ($priority !== null && $priority !== '') { if ($priority !== null && $priority !== '') {
$set['priority'] = (int) $priority; $set['priority'] = (int) $priority;
} }
/* ===================== /*
* KEYWORDS * =====================
* ===================== */ * KEYWORDS (merge)
* =====================
*/
$keywordSources = [ $keywordSources = [
$parsed['event']['keywords'] ?? null, $parsed['event']['keywords'] ?? null,
$parsed['news']['keywords'] ?? null, $parsed['news']['keywords'] ?? null,
@@ -69,103 +48,48 @@ class IndexPageListener
if (!is_string($s) || trim($s) === '') { if (!is_string($s) || trim($s) === '') {
continue; continue;
} }
foreach (preg_split('/\s+/', trim($s)) as $p) {
foreach (preg_split('/\s+/', trim($s)) ?: [] as $p) {
if ($p !== '') { if ($p !== '') {
$kw[] = $p; $kw[] = $p;
} }
} }
} }
$keywords = $kw ? implode(' ', array_unique($kw)) : null; if ($kw) {
$set['keywords'] = implode(' ', array_unique($kw));
}
/* ===================== /*
* IMAGEPATH * =====================
* ===================== */ * IMAGEPATH (event > news > page > custom)
* =====================
*/
$image = $image =
$parsed['event']['searchimage'] ?? $parsed['event']['searchimage'] ?? null ??
$parsed['news']['searchimage'] ?? $parsed['news']['searchimage'] ?? null ??
$parsed['page']['searchimage'] ?? $parsed['page']['searchimage'] ?? null ??
$parsed['custom']['searchimage'] ?? $parsed['custom']['searchimage'] ?? null;
null;
$imagepath = is_string($image) && $image !== '' ? trim($image) : null; if (is_string($image) && $image !== '') {
$set['imagepath'] = trim($image);
}
/* ===================== /*
* STARTDATE * =====================
* ===================== */ * STARTDATE (event.date/news.date => timestamp)
* =====================
*/
$date = $date =
$parsed['event']['date'] ?? $parsed['event']['date'] ?? null ??
$parsed['news']['date'] ?? $parsed['news']['date'] ?? null;
null;
$startDate = null;
if (is_string($date) && $date !== '') { if (is_string($date) && $date !== '') {
$ts = strtotime($date); $ts = strtotime($date);
if ($ts !== false) { if ($ts !== false) {
$startDate = $ts; $set['startDate'] = $ts;
} }
} }
/* =====================
* 🔥 WICHTIGER TEIL:
* tl_search UPDATE
* ===================== */
$this->updateSearchRow($url, $keywords, $imagepath, $startDate, $debug);
if ($debug) {
echo "INDEXPAGE HOOK END\n";
echo "=============================\n";
}
}
private function updateSearchRow(
string $url,
?string $keywords,
?string $imagepath,
?int $startDate,
bool $debug
): void {
$db = Database::getInstance();
$fields = [];
$values = [];
if ($keywords !== null) {
$fields[] = 'keywords = ?';
$values[] = $keywords;
}
if ($imagepath !== null) {
$fields[] = 'imagepath = ?';
$values[] = $imagepath;
}
if ($startDate !== null) {
$fields[] = 'startDate = ?';
$values[] = $startDate;
}
if (!$fields) {
if ($debug) {
echo "️ Nothing to update in tl_search\n";
}
return;
}
$values[] = $url;
$sql = 'UPDATE tl_search SET ' . implode(', ', $fields) . ' WHERE url = ?';
$db->prepare($sql)->execute(...$values);
if ($debug) {
echo "✅ tl_search updated:\n";
var_dump([
'keywords' => $keywords,
'imagepath' => $imagepath,
'startDate' => $startDate,
]);
}
} }
private function extractMeilisearchJson(string $content): ?array private function extractMeilisearchJson(string $content): ?array