Quicktips.php 6.41 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
15
    const QUICKTIP_URL = "http://localhost:63825/1.1/quicktips.xml";
    const QUICKTIP_NAME = "quicktips";
16
17
18
19
20
21
22
23
24
25
26
    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)
    {
27
        $url = self::QUICKTIP_URL . "?search=" . $this->normalize_search($search) . "&locale=" . $locale;
28
        # TODO anders weitergeben
29
        $this->hash = md5($url);
30
31
32

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

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

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

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

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

89
90
        $redis->del('search.' . $hash . ".results." . self::QUICKTIP_NAME);
        $redis->del('search.' . $hash . ".ready");
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
        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) {
113
114
115
116
117
118
119
120
121
                // 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 = "";
                }

122
123
124
125
126
127
                // Title
                $title = $quicktip_xml->title->__toString();

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

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

143
144
145
146
147
148
149
150
                $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 = "";
                }

151
152
                // Description
                $descr = $quicktip_xml->content->__toString();
153

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

    public function normalize_search($search)
    {
189
        return urlencode($search);
190
    }
191
}