MetaGerSearch.php 4.67 KB
Newer Older
Dominik Hebeler's avatar
Dominik Hebeler committed
1
2
3
4
5
<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
use App;
8
9
use App\MetaGer;

Dominik Hebeler's avatar
Dominik Hebeler committed
10
11
12

class MetaGerSearch extends Controller
{
13
    public function search(Request $request, MetaGer $metager)
14
    {
15
        #die($request->header('User-Agent'));
16
        $time = microtime();
17
18
        # Mit gelieferte Formulardaten parsen und abspeichern:
        $metager->parseFormData($request);
19
20
        #if($metager->getFokus() !== "bilder" )
        #{
21
22
            # Nach Spezialsuchen überprüfen:
            $metager->checkSpecialSearches($request);
23
        #}
24
25
        # Alle Suchmaschinen erstellen
        $metager->createSearchEngines($request);
26

27
28
29
        # Alle Ergebnisse vor der Zusammenführung ranken:
        $metager->rankAll();

30
31
        # Ergebnisse der Suchmaschinen kombinieren:
        $metager->combineResults();
32
        
33
34
        # Die Ausgabe erstellen:
        return $metager->createView();
Dominik Hebeler's avatar
Dominik Hebeler committed
35
36
    }

37
    public function quicktips(Request $request)
38
    {
39
40
41
42
        $q = $request->input('q', '');

        # Zunächst den Spruch
        $spruecheFile = storage_path() . "/app/public/sprueche.txt";
43
        if( file_exists($spruecheFile) && $request->has('sprueche') )
44
45
46
        {
            $sprueche = file($spruecheFile);
            $spruch = $sprueche[array_rand($sprueche)];
47
48
49
        }else
        {
            $spruch = "";
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
        # Die manuellen Quicktips:
        $file = storage_path() . "/app/public/qtdata.csv";
        
        $mquicktips = [];
        if( file_exists($file) && $q !== '')
        {
            $file = fopen($file, 'r');
            while (($line = fgetcsv($file)) !== FALSE) {
                $words = array_slice($line,3);
                $isIn = FALSE;
                foreach($words as $word){
                        $word = strtolower($word);
                        if(strpos($q, $word) !== FALSE){
                                $isIn = TRUE;
                                break;
                        }
                }
                if($isIn === TRUE){
                        $quicktip = array('QT_Type' => "MQT");
                        $quicktip["URL"] = $line[0];
                        $quicktip["title"] = $line[1];
                        $quicktip["descr"] = $line[2];
                        $mquicktips[] = $quicktip;
                }
        }
        fclose($file);
        }

        # Wikipedia Quicktip
        $quicktips = [];
        $url = "http://de.wikipedia.org/w/api.php?action=query&titles=".urlencode(implode("_",array_diff(explode(" ",$q),array("wikipedia"))))."&prop=info|extracts|categories&inprop=url|displaytitle&exintro&exsentences=3&format=json";
        $decodedResponse = json_decode($this->get($url), true);
84
        if( isset($decodedResponse["query"]["pages"]) )
85
        {
86
            foreach($decodedResponse["query"]["pages"] as $result)
87
            {
88
89
90
91
92
93
94
                if( isset($result['displaytitle']) && isset($result['fullurl']) && isset($result['extract']) )
                {
                    $quicktip = [];
                    $quicktip["title"] = $result['displaytitle'];
                    $quicktip["URL"] = $result['fullurl'];
                    $quicktip["descr"] = strip_tags($result['extract']);
                    $quicktip['gefVon'] = "aus <a href=\"https://de.wikipedia.org\" target=\"_blank\">Wikipedia, der freien Enzyklopädie</a>";
95

96
97
                    $quicktips[] = $quicktip;
                }
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
            }
        }
        $mquicktips = array_merge($mquicktips, $quicktips);

        # Uns Natürlich das wussten Sie schon:
        $file = storage_path() . "/app/public/tips.txt";
        if( file_exists($file) )
        {
            $tips = file($file);
            $tip = $tips[array_rand($tips)];

            $mquicktips[] = ['title' => 'Wussten Sie schon?', 'descr' => $tip, 'URL' => '/tips'];   
        }   

        # Uns die Werbelinks:
        $file = storage_path() . "/app/public/ads.txt";
        if( file_exists($file) )
        {
            $ads = json_decode(file_get_contents($file), true);
            $ad = $ads[array_rand($ads)];

            $mquicktips[] = ['title' => $ad['title'], 'descr' => $ad['descr'], 'URL' => $ad['URL']];   
        }   
        return view('quicktip')
            ->with('spruch', $spruch)
            ->with('mqs', $mquicktips);

            
126
127
    }

128
129
130
131
132
133
134
135
136
137
138
139
140
141
    public function tips()
    {
        $file = storage_path() . "/app/public/tips.txt";
        $tips = [];
        if( file_exists($file) )
        {
            $tips = file($file);
        }
        return view('tips')
            ->with('title', 'MetaGer - Tipps & Tricks')
            ->with('tips', $tips);
    }

    function get($url) {
142
        return file_get_contents($url);
143
144
    } 

Dominik Hebeler's avatar
Dominik Hebeler committed
145
}