diff --git a/app/MetaGer.php b/app/MetaGer.php index b860bf7b62872eb9a080f7240b38eeb2c4501cb3..477c3993a4873044bdc9a239dd5c0d52b17421f6 100644 --- a/app/MetaGer.php +++ b/app/MetaGer.php @@ -570,8 +570,7 @@ class MetaGer foreach ($engines as $engine) { $engine->startSearch($this); } - // Derzeit deaktiviert, da es die eigene Suche gibt - // $this->adjustFocus($sumas, $enabledSearchengines); + $quicktips = new \App\Models\Quicktips($this->q, $this->lang, $this->getTime(), $this->getHashCode()); /* Wir warten auf die Antwort der Suchmaschinen * Die Verbindung steht zu diesem Zeitpunkt und auch unsere Requests wurden schon gesendet. diff --git a/app/Models/Quicktip.php b/app/Models/Quicktip.php new file mode 100644 index 0000000000000000000000000000000000000000..95d4a1d853ec36d3c9ba5f30b82565dcd0d3a2a7 --- /dev/null +++ b/app/Models/Quicktip.php @@ -0,0 +1,36 @@ +<?php + +namespace App\Models; + +class Quicktip +{ + private $type; + private $title; + private $link; + private $descr; + private $details; + + # Erstellt ein neues Ergebnis + public function __construct($type, $title, $link, $descr, $details) + { + $this->type = $type; + $this->title = $title; + $this->link = $link; + $this->descr = $descr; + $this->details = $details; + } +} + +class Quicktip_Detail +{ + private $title; + private $link; + private $descr; + + public function __construct($title, $link, $descr) + { + $this->title = $title; + $this->link = $link; + $this->descr = $descr; + } +} diff --git a/app/Models/Quicktips.php b/app/Models/Quicktips.php new file mode 100644 index 0000000000000000000000000000000000000000..0caa8ecf4df0770b42d1786b68eb87ef17fd65e0 --- /dev/null +++ b/app/Models/Quicktips.php @@ -0,0 +1,117 @@ +<?php + +namespace App\Models; + +class Quicktips +{ + use DispatchesJobs; + + const QUICKTIP_URL = "https://quicktips.metager3.de/quicktips.xml"; + const QUICKTIP_NAME = "quicktips"; + + public function __construct($search, $locale, $max_time, $resultHash) + { + $this->startSearch($search, $locale, $max_time, $resultHash); + } + + public function startSearch($search, $locale, $max_time, $resultHash) + { + $full_url = QUICKTIP_URL . "?search=" . $search . "&locale=" . $locale; + + $hash = md5($full_url); + + if (Cache::has($hash)) { + $cached = true; + $this->retrieveResults(); + } else { + // ??? + Redis::hset("search." . $resultHash, QUICKTIP_NAME, "waiting"); + + // Queue this search + $mission = $resultHash . ";" . $url . ";" . $max_time; + Redis::rpush(QUICKTIP_NAME . ".queue", $mission); + + // Check the current status of Searchers for QUICKTIP_NAME + $needSearcher = false; + $searcherData = Redis::hgetall(QUICKTIP_NAME . ".stats"); + + // Create additional Searchers for QUICKTIP_NAME if necessary + if (sizeof($searcherData) === 0) { + $needSearcher = true; + } else { + $median = 0; + foreach ($searcherData as $pid => $data) { + $data = explode(";", $data); + $median += floatval($data[1]); + } + $median /= sizeof($searcherData); + if ($median < .1) { + $needSearcher = true; + } + } + if ($needSearcher && Redis::get(QUICKTIP_NAME) !== "locked") { + Redis::set(QUICKTIP_NAME, "locked"); + $this->dispatch(new Searcher(QUICKTIP_NAME)); + } + } + } + + public function retrieveResults($hash, $resultHash) + { + $body = ""; + if (Cache::has($hash)) { + $body = Cache::get($hash); + } elseif (Redis::hexists('search.' . $resultHash, QUICKTIP_NAME)) { + $body = Redis::hget('search.' . $resultHash, QUICKTIP_NAME); + Redis::hdel('search.' . $resultHash, QUICKTIP_NAME); + Cache::put($hash, $body, $cacheDuration); + } + if ($body !== "") { + return $this->loadResults($body); + } else { + return false; + } + } + + public function loadResults($quicktips_raw) { + $quicktips_raw = preg_replace("/\r\n/si", "", $quicktips_raw); + try { + $content = simplexml_load_string($quicktips_raw); + if (!$content) { + return; + } + + $quicktips = []; + + $quicktips_xpath = $content->xpath('feed/entry'); + foreach ($quicktips_xpath as $quicktip_xml) { + $type = $quicktip_xml->{"mg:type"}->__toString(); + $title = $quicktip_xml->{"title"}->__toString(); + $link = $quicktip_xml->{"link"}->__toString(); + $descr = $quicktip_xml->{"content"}->__toString(); + $details = []; + foreach ($quicktip_xml->{"mg:details"}->{"entry"} as $detail_xml) { + $details_title = $detail_xml->{"title"}->__toString(); + $details_link = $detail_xml->{"url"}->__toString(); + $details_descr = $detail_xml->{"text"}->__toString(); + $details[] = new \App\Models\Detail( + $details_title, + $details_link, + $details_descr + ); + } + $quicktips[] = new \App\Models\Quicktip( + $type, + $title, + $link, + $descr, + $details + ); + } + return $quicktips; + } catch (\Exception $e) { + Log::error("A problem occurred parsing quicktips"); + return []; + } + } +} diff --git a/resources/views/layouts/quicktip.blade.php b/resources/views/layouts/quicktip.blade.php new file mode 100644 index 0000000000000000000000000000000000000000..74593b536989f6a371083339a64084b90a305e52 --- /dev/null +++ b/resources/views/layouts/quicktip.blade.php @@ -0,0 +1,38 @@ +@if (sizeof($quicktip->details) > 0) + <details> + <summary class="quicktip-summary"> + <h1> + @if (isset($quicktip->link)) + <a href="{{ $quicktip->link }}">{{ $quicktip->title }}</a> + @else + {{ $quicktip->title }} + @endif + </h1> + <p>{{ $quicktip->descr }}</p> + @if + </summary> + @foreach ($quicktip->details as $detail) + <div class="quicktip-detail"> + <h2> + @if (isset($detail->link)) + <a href="{{ $detail->link }}">{{ $detail->title }}</a> + @else + {{ $detail->title }} + @endif + </h2> + <p>{{ $detail->descr }}</p> + </div> + @endforeach + </details> +@else + <summary class="quicktip-summary"> + <h1> + @if (isset($quicktip->link)) + <a href="{{ $quicktip->link }}">{{ $quicktip->title }}</a> + @else + {{ $quicktip->title }} + @endif + </h1> + @if + </summary> +@endif \ No newline at end of file diff --git a/resources/views/quicktips.blade.php b/resources/views/quicktips.blade.php new file mode 100644 index 0000000000000000000000000000000000000000..4999e2dcc4af967f5da6edf04928491e46c0df12 --- /dev/null +++ b/resources/views/quicktips.blade.php @@ -0,0 +1,5 @@ +@foreach($quicktips as $quicktip) + <div class="quicktip" type="{{ $quicktip->type }}"> + @include('layouts.quicktip', ['quicktip' => $quicktip]) + </div> +@endforeach \ No newline at end of file