Quicktips.php 6.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace App\Models\Quicktips;

use App\Jobs\Searcher;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Facades\Redis;
use Log;

class Quicktips
{
    use DispatchesJobs;

14
    private $QUICKTIP_URL = "/1.1/quicktips.xml";
15
    const QUICKTIP_NAME = "quicktips";
16 17 18 19 20 21
    const CACHE_DURATION = 60;

    private $hash;

    public function __construct($search, $locale, $max_time)
    {
22 23 24 25 26
        if (env("APP_ENV") === "production") {
            $this->QUICKTIP_URL = "https://quicktips.metager.de" . $this->QUICKTIP_URL;
        } else {
            $this->QUICKTIP_URL = "https://dev.quicktips.metager.de" . $this->QUICKTIP_URL;
        }
27 28 29 30 31
        $this->startSearch($search, $locale, $max_time);
    }

    public function startSearch($search, $locale, $max_time)
    {
32
        $url = self::QUICKTIP_URL . "?search=" . $this->normalize_search($search) . "&locale=" . $locale;
33
        # TODO anders weitergeben
34
        $this->hash = md5($url);
35 36 37

        # TODO cache wieder einbauen (eventuell)
        if ( /*!Cache::has($hash)*/true) {
38 39 40
            $redis = Redis::connection(env('REDIS_RESULT_CONNECTION'));

            $redis->hset("search." . $this->hash . ".results." . self::QUICKTIP_NAME, "status", "waiting");
41 42

            // Queue this search
43
            $mission = $this->hash . ";" . base64_encode($url) . ";" . $max_time;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            Redis::rpush(self::QUICKTIP_NAME . ".queue", $mission);

            // Check the current status of Searchers for QUICKTIP_NAME
            $needSearcher = false;
            $searcherData = Redis::hgetall(self::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(self::QUICKTIP_NAME) !== "locked") {
                Redis::set(self::QUICKTIP_NAME, "locked");
                $this->dispatch(new Searcher(self::QUICKTIP_NAME));
            }
        }
    }

71 72 73 74 75 76
    /**
     * Load the current Quicktip results
     * 1. Retrieve the raw results
     * 2. Parse the results
     * Returns an empty array if no results are found
     */
77 78 79
    public function loadResults()
    {
        $resultsRaw = $this->retrieveResults($this->hash);
80 81 82 83 84 85
        if ($resultsRaw) {
            $results = $this->parseResults($resultsRaw);
            return $results;
        } else {
            return [];
        }
86 87 88 89 90
    }

    public function retrieveResults($hash)
    {
        $body = "";
91 92
        $redis = Redis::connection(env('REDIS_RESULT_CONNECTION'));
        $body = $redis->hget('search.' . $hash . ".results." . self::QUICKTIP_NAME, "response");
93

94 95
        $redis->del('search.' . $hash . ".results." . self::QUICKTIP_NAME);
        $redis->del('search.' . $hash . ".ready");
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        if ($body !== "") {
            return $body;
        } else {
            return false;
        }
    }

    public function parseResults($quicktips_raw)
    {
        $quicktips_raw = preg_replace("/\r\n/si", "", $quicktips_raw);
        try {
            $content = simplexml_load_string($quicktips_raw);
            if (!$content) {
                return;
            }

            $content->registerXPathNamespace('main', 'http://www.w3.org/2005/Atom');

            $quicktips = [];

            $quicktips_xpath = $content->xpath('main:entry');
            foreach ($quicktips_xpath as $quicktip_xml) {
118 119 120 121 122 123 124 125 126
                // Type
                $quicktip_xml->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
                $types = $quicktip_xml->xpath('mg:type');
                if (sizeof($types) > 0) {
                    $type = $types[0]->__toString();
                } else {
                    $type = "";
                }

127 128 129 130 131 132
                // Title
                $title = $quicktip_xml->title->__toString();

                // Link
                $link = $quicktip_xml->link['href']->__toString();

133
                // gefVon
134
                $quicktip_xml->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
135 136 137
                $gefVonTitles = $quicktip_xml->xpath('mg:gefVonTitle');
                if (sizeof($gefVonTitles) > 0) {
                    $gefVonTitle = $gefVonTitles[0]->__toString();
138
                } else {
139 140 141 142 143 144 145
                    $gefVonTitle = "";
                }
                $gefVonLinks = $quicktip_xml->xpath('mg:gefVonLink');
                if (sizeof($gefVonLinks) > 0) {
                    $gefVonLink = $gefVonLinks[0]->__toString();
                } else {
                    $gefVonLink = "";
146 147
                }

148 149 150 151 152 153 154 155
                $quicktip_xml->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
                $author = $quicktip_xml->xpath('mg:author');
                if (sizeof($author) > 0) {
                    $author = $author[0]->__toString();
                } else {
                    $author = "";
                }

156 157
                // Description
                $descr = $quicktip_xml->content->__toString();
158

159
                // Details
160
                $details = [];
161 162
                $details_xpath = $quicktip_xml->xpath('mg:details');
                if (sizeof($details_xpath) > 0) {
163 164
                    foreach ($details_xpath[0] as $detail_xml) {
                        $details_title = $detail_xml->title->__toString();
165
                        $details_link = $detail_xml->url->__toString();
166
                        $details_descr = $detail_xml->text->__toString();
167
                        $details[] = new \App\Models\Quicktips\Quicktip_detail(
168 169 170 171 172 173 174 175 176 177
                            $details_title,
                            $details_link,
                            $details_descr
                        );
                    }
                }
                $quicktips[] = new \App\Models\Quicktips\Quicktip(
                    $type,
                    $title,
                    $link,
178 179
                    $gefVonTitle,
                    $gefVonLink,
180
                    $author,
181 182 183 184 185 186 187 188 189 190
                    $descr,
                    $details
                );
            }
            return $quicktips;
        } catch (\Exception $e) {
            Log::error("A problem occurred parsing quicktips");
            return [];
        }
    }
191 192 193

    public function normalize_search($search)
    {
194
        return urlencode($search);
195
    }
196
}