Quicktips.php 5.41 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php

namespace App\Models\Quicktips;

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

class Quicktips
{
    use DispatchesJobs;

    const QUICKTIP_URL = "https://quicktips.metager3.de/quicktips.xml";
    const QUICKTIP_NAME = "quicktips";
    const CACHE_DURATION = 60;

    private $hash;

    public function __construct($search, $locale, $max_time)
    {
        $this->startSearch($search, $locale, $max_time);
    }

    public function startSearch($search, $locale, $max_time)
    {
        $url = self::QUICKTIP_URL . "?search=" . $search . "&locale=" . $locale;

        $hash = md5($url);

        # TODO anders weitergeben
        $this->hash = $hash;

        # TODO cache wieder einbauen (eventuell)
        if ( /*!Cache::has($hash)*/true) {
            Redis::hset("search." . $hash, self::QUICKTIP_NAME, "waiting");

            // Queue this search
            $mission = $hash . ";" . base64_encode($url) . ";" . $max_time;
            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));
            }
        }
    }

    public function loadResults()
    {
        $resultsRaw = $this->retrieveResults($this->hash);
        $results = $this->parseResults($resultsRaw);
        return $results;
    }

    public function retrieveResults($hash)
    {
        $body = "";
        #if (Cache::has($hash)) {
        $body = Cache::get($hash);
        #} elseif (Redis::hexists('search.' . $hash, self::QUICKTIP_NAME)) {
        $body = Redis::hget('search.' . $hash, self::QUICKTIP_NAME);
        Redis::hdel('search.' . $hash, self::QUICKTIP_NAME);
        Cache::put($hash, $body, self::CACHE_DURATION);
        #}
        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');
            $content->registerXPathNamespace('opensearch', 'http://a9.com/-/spec/opensearch/1.1/');
            $content->registerXPathNamespace('relevance', 'http://a9.com/-/opensearch/extensions/relevance/1.0/');
            $content->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');

            $quicktips = [];
            #die(var_dump($content->xpath('//main:entry/mg:type')));

            $quicktips_xpath = $content->xpath('main:entry');
            foreach ($quicktips_xpath as $quicktip_xml) {
                $content->registerXPathNamespace('mg', 'http://metager.de/opensearch/quicktips/');
                $types = $quicktip_xml->xpath('mg:type');
                if (sizeof($types) > 0) {
                    $type = $types[0]->__toString();
                }
                $title = $quicktip_xml->{"title"}->__toString();
                $link_xml = $quicktip_xml->link['href'];
                if ($link_xml->count() > 0) {
                    $link = $link_xml->toString();
                } else {
                    $link = "";
                }

                $link = $quicktip_xml->{"link"}->__toString();
                $descr = $quicktip_xml->{"content"}->__toString();

                $details = [];
                $details_xpath = $quicktip_xml->xpath('mg:details');
                if (sizeof($details_xpath) > 0) {
                    foreach ($details_xpath 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\Quicktips\Quicktip_detail(
                            $details_title,
                            $details_link,
                            $details_descr
                        );
                    }
                }
                $quicktips[] = new \App\Models\Quicktips\Quicktip(
                    $type,
                    $title,
                    $link,
                    $descr,
                    $details
                );
            }
            die(var_dump($quicktips));
            return $quicktips;
        } catch (\Exception $e) {
            die($e);
            Log::error("A problem occurred parsing quicktips");
            return [];
        }
    }
}