Commit 33ef3f93 authored by Phil Höfer's avatar Phil Höfer
Browse files

Reintroduce quicktips into resultpage

parent 3f2dd7e1
......@@ -76,10 +76,21 @@ class MetaGerSearch extends Controller
return response($responseContent);
}
$quicktips = $metager->createQuicktips();
if (!empty($timings)) {
$timings["createQuicktips"] = microtime(true) - $time;
}
# Suche für alle zu verwendenden Suchmaschinen als Job erstellen,
# auf Ergebnisse warten und die Ergebnisse laden
$metager->createSearchEngines($request, $timings);
# Versuchen die Ergebnisse der Quicktips zu laden
$quicktipResults = $quicktips->loadResults();
if (!empty($timings)) {
$timings["loadResults"] = microtime(true) - $time;
}
$metager->startSearch($timings);
$metager->waitForMainResults();
......@@ -128,7 +139,7 @@ class MetaGerSearch extends Controller
}
# Die Ausgabe erstellen:
$resultpage = $metager->createView();
$resultpage = $metager->createView($quicktipResults);
if ($spamEntry !== null) {
try {
Cache::put('spam.' . $metager->getFokus() . "." . md5($spamEntry), $resultpage->render(), 604800);
......
......@@ -135,7 +135,7 @@ class MetaGer
}
# Erstellt aus den gesammelten Ergebnissen den View
public function createView()
public function createView($quicktipResults = [])
{
# Hiermit werden die evtl. ausgewählten SuMas extrahiert, damit die Input-Boxen richtig gesetzt werden können
$focusPages = [];
......@@ -175,7 +175,7 @@ class MetaGer
->with('apiAuthorized', $this->apiAuthorized)
->with('metager', $this)
->with('browser', (new Agent())->browser())
->with('quicktips', action('MetaGerSearch@quicktips', ["search" => $this->eingabe]))
->with('quicktips', $quicktipResults)
->with('focus', $this->fokus)
->with('resultcount', count($this->results));
}
......@@ -248,7 +248,7 @@ class MetaGer
->with('apiAuthorized', $this->apiAuthorized)
->with('metager', $this)
->with('browser', (new Agent())->browser())
->with('quicktips', action('MetaGerSearch@quicktips', ["search" => $this->eingabe, "quotes" => $this->sprueche]))
->with('quicktips', $quicktipResults)
->with('resultcount', count($this->results))
->with('focus', $this->fokus);
break;
......@@ -1213,6 +1213,15 @@ class MetaGer
}
}
public function createQuicktips()
{
# Die quicktips werden als job erstellt und zur Abarbeitung freigegeben
$quicktips = new \App\Models\Quicktips\Quicktips($this->q, LaravelLocalization::getCurrentLocale(), $this->getTime());
return $quicktips;
}
private function anonymizeIp($ip)
{
if (str_contains($ip, ":")) {
......
......@@ -3,53 +3,54 @@
namespace App\Models\Quicktips;
use Cache;
use LaravelLocalization;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Redis;
use Log;
class Quicktips
{
use DispatchesJobs;
private $quicktipUrl = "/1.1/quicktips.xml";
private $results = [];
const QUICKTIP_NAME = "quicktips";
const CACHE_DURATION = 60 * 60;
const CACHE_DURATION = 60;
private $hash;
public function __construct($search, $quotes)
public function __construct($search, $locale, $max_time)
{
$locale = LaravelLocalization::getCurrentLocale();
if (env("APP_ENV") === "production") {
$this->quicktipUrl = "https://quicktips.metager.de" . $this->quicktipUrl;
} else {
$this->quicktipUrl = "https://dev.quicktips.metager.de" . $this->quicktipUrl;
}
$this->startSearch($search, $quotes, $locale);
$this->startSearch($search, $locale, $max_time);
}
public function startSearch($search, $quotes, $locale)
public function startSearch($search, $locale, $max_time)
{
$url = $this->quicktipUrl . "?search=" . $this->normalize_search($search) . "&locale=" . $locale . "&quotes=" . $quotes;
$url = $this->quicktipUrl . "?search=" . $this->normalize_search($search) . "&locale=" . $locale;
$this->hash = md5($url);
$results = null;
try {
if (!Cache::has($this->hash)) {
$results = file_get_contents($url);
Cache::put($this->hash, $results, Quicktips::CACHE_DURATION);
} else {
$results = Cache::get($this->hash);
}
} catch (\Exception $e) {
Log::error($e->getMessage());
}
if (!Cache::has($this->hash)) {
if ($results === null) {
$results = file_get_contents($url);
}
// Queue this search
$mission = [
"resulthash" => $this->hash,
"url" => $url,
"useragent" => "",
"username" => null,
"password" => null,
"headers" => [],
"cacheDuration" => self::CACHE_DURATION,
"name" => "Quicktips",
];
$mission = json_encode($mission);
$this->results = $this->loadResults($results);
Redis::rpush(\App\MetaGer::FETCHQUEUE_KEY, $mission);
}
}
/**
......@@ -58,8 +59,9 @@ class Quicktips
* 2. Parse the results
* Returns an empty array if no results are found
*/
public function loadResults($resultsRaw)
public function loadResults()
{
$resultsRaw = $this->retrieveResults($this->hash);
if ($resultsRaw) {
$results = $this->parseResults($resultsRaw);
return $results;
......@@ -71,9 +73,13 @@ class Quicktips
public function retrieveResults($hash)
{
$body = null;
$body = Redis::brpoplpush($this->hash, $this->hash,1);
if ($body === false) {
return false;
}
if (Cache::has($this->hash)) {
$body = Cache::get($this->hash);
if ($body === "no-result") {
return false;
}
if ($body !== null) {
......@@ -176,9 +182,4 @@ class Quicktips
{
return urlencode($search);
}
public function getResults()
{
return $this->results;
}
}
@import "./resultpage/result-page.less";
@import "./resultpage/result.less";
@import "./resultpage/quicktips.less";
@import "./resultpage/product.less";
@import "./resultpage/result-saver.less";
@import "./resultpage/keyboard-nav.less";
\ No newline at end of file
/* Quicktips */
@import "../../variables.less";
@import "../../general/cards.less";
@import "../../general/general.less";
// @import "../../variables.less";
// @import "../../general/cards.less";
// @import "../../general/general.less";
@quicktip-font-large: @result-font-large;
@quicktip-font-medium: @result-font-medium;
@quicktip-font-small: @result-font-small;
......
......@@ -34,11 +34,11 @@
</div>
<div id="additions-container">
@include('layouts.keyboardNavBox')
<div id="quicktips">
@if( $metager->showQuicktips() )
<div id="quicktips">
<iframe src="{{ $quicktips }}" frameborder="0"></iframe>
</div>
@include('quicktips', ['quicktips', $quicktips])
@endif
</div>
</div>
@include('parts.footer', ['type' => 'resultpage', 'id' => 'resultPageFooter'])
</div>
<!DOCTYPE html>
<html lang="{{ LaravelLocalization::getCurrentLocale() }}">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>{{ $search }} - MetaGer Quicktips</title>
<link type="text/css" rel="stylesheet" href="{{ mix('css/quicktips.css') }}" />
<link href="/fonts/liberationsans/stylesheet.css" rel="stylesheet">
<link type="text/css" rel="stylesheet" href="{{ mix('css/fontawesome.css') }}" />
<link type="text/css" rel="stylesheet" href="{{ mix('css/fontawesome-solid.css') }}" />
</head>
<body id="quicktips">
@foreach($quicktips as $quicktip)
<div class="quicktip" type="{{ $quicktip->type }}">
@include('parts.quicktip', ['quicktip' => $quicktip])
</div>
@endforeach
</body>
</html>
@endforeach
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment