Bugfix
This commit is contained in:
@@ -2,30 +2,181 @@
|
|||||||
|
|
||||||
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
|
||||||
{
|
{
|
||||||
if (PHP_SAPI === 'cli') {
|
$debug = (PHP_SAPI === 'cli');
|
||||||
echo "\n=== HARD OVERRIDE TEST ===\n";
|
$url = $set['url'] ?? $data['url'] ?? null;
|
||||||
echo "URL: " . ($set['url'] ?? '-') . "\n";
|
|
||||||
|
if ($debug) {
|
||||||
|
echo "\n=============================\n";
|
||||||
|
echo "INDEXPAGE HOOK START\n";
|
||||||
|
echo "URL: " . ($url ?? '[no url]') . "\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 🔥 HART GESETZT – KEINE LOGIK
|
if (!$url || !str_contains($content, 'MEILISEARCH_JSON')) {
|
||||||
$set['priority'] = 9;
|
if ($debug) {
|
||||||
$set['keywords'] = 'HARDCODED_KEYWORDS_TEST';
|
echo "❌ No URL or no MEILISEARCH_JSON marker\n";
|
||||||
$set['imagepath'] = 'HARDCODED-IMAGE-UUID-123';
|
echo "INDEXPAGE HOOK END\n";
|
||||||
$set['startDate'] = 1234567890;
|
echo "=============================\n";
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (PHP_SAPI === 'cli') {
|
$parsed = $this->extractMeilisearchJson($content);
|
||||||
echo "SET WRITTEN:\n";
|
|
||||||
var_dump([
|
if ($parsed === null) {
|
||||||
'priority' => $set['priority'],
|
if ($debug) {
|
||||||
'keywords' => $set['keywords'],
|
echo "❌ Invalid JSON in MEILISEARCH_JSON\n";
|
||||||
'imagepath' => $set['imagepath'],
|
echo "INDEXPAGE HOOK END\n";
|
||||||
'startDate' => $set['startDate'],
|
echo "=============================\n";
|
||||||
]);
|
}
|
||||||
echo "=== END HARD OVERRIDE ===\n";
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($debug) {
|
||||||
|
echo "✅ MEILISEARCH_JSON parsed\n";
|
||||||
|
var_dump($parsed);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =====================
|
||||||
|
* PRIORITY
|
||||||
|
* ===================== */
|
||||||
|
$priority =
|
||||||
|
$parsed['event']['priority'] ??
|
||||||
|
$parsed['news']['priority'] ??
|
||||||
|
$parsed['page']['priority'] ??
|
||||||
|
null;
|
||||||
|
|
||||||
|
if ($priority !== null && $priority !== '') {
|
||||||
|
$set['priority'] = (int) $priority;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* =====================
|
||||||
|
* KEYWORDS
|
||||||
|
* ===================== */
|
||||||
|
$keywordSources = [
|
||||||
|
$parsed['event']['keywords'] ?? null,
|
||||||
|
$parsed['news']['keywords'] ?? null,
|
||||||
|
$parsed['page']['keywords'] ?? null,
|
||||||
|
];
|
||||||
|
|
||||||
|
$kw = [];
|
||||||
|
foreach ($keywordSources as $s) {
|
||||||
|
if (!is_string($s) || trim($s) === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
foreach (preg_split('/\s+/', trim($s)) as $p) {
|
||||||
|
if ($p !== '') {
|
||||||
|
$kw[] = $p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$keywords = $kw ? implode(' ', array_unique($kw)) : null;
|
||||||
|
|
||||||
|
/* =====================
|
||||||
|
* IMAGEPATH
|
||||||
|
* ===================== */
|
||||||
|
$image =
|
||||||
|
$parsed['event']['searchimage'] ??
|
||||||
|
$parsed['news']['searchimage'] ??
|
||||||
|
$parsed['page']['searchimage'] ??
|
||||||
|
$parsed['custom']['searchimage'] ??
|
||||||
|
null;
|
||||||
|
|
||||||
|
$imagepath = is_string($image) && $image !== '' ? trim($image) : null;
|
||||||
|
|
||||||
|
/* =====================
|
||||||
|
* STARTDATE
|
||||||
|
* ===================== */
|
||||||
|
$date =
|
||||||
|
$parsed['event']['date'] ??
|
||||||
|
$parsed['news']['date'] ??
|
||||||
|
null;
|
||||||
|
|
||||||
|
$startDate = null;
|
||||||
|
if (is_string($date) && $date !== '') {
|
||||||
|
$ts = strtotime($date);
|
||||||
|
if ($ts !== false) {
|
||||||
|
$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
|
||||||
|
{
|
||||||
|
if (!preg_match('/<!--\s*MEILISEARCH_JSON\s*(\{.*?\})\s*-->/s', $content, $m)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$json = preg_replace('/^\xEF\xBB\xBF/', '', trim($m[1]));
|
||||||
|
$data = json_decode($json, true);
|
||||||
|
|
||||||
|
return is_array($data) ? $data : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user