From 46bf17e1b4103a5ea8a5b158d096f4bbac43791b Mon Sep 17 00:00:00 2001
From: Karl <Karl Hasselbring>
Date: Thu, 8 Sep 2016 11:34:16 +0200
Subject: [PATCH] Mehr Kommentare

---
 app/Models/Result.php | 163 +++++++++++++++++++++++++++++-------------
 1 file changed, 115 insertions(+), 48 deletions(-)

diff --git a/app/Models/Result.php b/app/Models/Result.php
index 43f519206..70f575975 100644
--- a/app/Models/Result.php
+++ b/app/Models/Result.php
@@ -2,9 +2,31 @@
 
 namespace App\Models;
 
+/* Die Klasse Result sammelt alle Informationen über ein einzelnes Suchergebnis.
+ *  Die Results werden von den Suchmaschinenspezifischen Parser-Skripten erstellt.
+ */
 class Result
 {
-
+    public $provider; # Die Engine von der das Suchergebnis kommt
+    public $titel; # Der Groß Angezeigte Name für das Suchergebnis
+    public $link; # Der Link auf die Ergebnisseite
+    public $anzeigeLink; # Der tatsächlich angezeigte Link (rein optisch)
+    public $descr; # Die Beschreibung des Suchergebnisses
+    public $gefVon; # Das bei Suchergebnissen angezeigte von ... mitsamt Verlinkung
+    public $sourceRank; # Das Ranking für dieses Suchergebnis von der Seite, die es geliefert hat (implizit durch Ergebnisreihenfolge: 20 - Position in Ergebnisliste)
+    public $partnershop; # Ist das Ergebnis von einem Partnershop? (bool)
+    public $image; # Ein Vorschaubild für das Suchergebnis (als URL)
+
+    public $proxyLink; # Der Link für die Seite über unseren Proxy-Service
+    public $engineBoost = 1; # Der Boost für den Provider des Suchergebnisses
+    public $valid       = true; # Ob das Ergebnis noch gültig ist (bool)
+    public $host; # Der aus dem Link gelesene Host des Suchergebnisses
+    public $strippedHost; # Der Host      in Form "foo.bar.de"
+    public $strippedDomain; # Die Domain    in Form "bar.de"
+    public $strippedLink; # Der Link      in Form "foo.bar.de/test"
+    public $rank; # Das Ranking für das Ergebnis
+
+    # Erstellt ein neues Ergebnis
     public function __construct($provider, $titel, $link, $anzeigeLink, $descr, $gefVon, $sourceRank, $partnershop = false, $image = "", $price = 0)
     {
         $provider          = simplexml_load_string($provider);
@@ -24,14 +46,12 @@ class Result
         if ($this->sourceRank <= 0 || $this->sourceRank > 20) {
             $this->sourceRank = 20;
         }
-
         $this->sourceRank = 20 - $this->sourceRank;
         if (isset($provider["engineBoost"])) {
             $this->engineBoost = floatval($provider["engineBoost"]->__toString());
         } else {
             $this->engineBoost = 1;
         }
-
         $this->valid          = true;
         $this->host           = @parse_url($link, PHP_URL_HOST);
         $this->strippedHost   = $this->getStrippedHost($this->anzeigeLink);
@@ -42,61 +62,90 @@ class Result
         $this->image          = $image;
     }
 
+    /* Ranked das Ergebnis nach folgenden Aspekten:
+     *  Startwert 0
+     *  + 0.02 * Sourcerank (20 - Position in Ergebnisliste des Suchanbieters)
+     *  * Engine-Boost
+     */
     public function rank(\App\MetaGer $metager)
     {
-
         $rank = 0;
+
+        $eingabe = $metager->getQ();
+
+        # Boost für Source Ranking
         $rank += ($this->sourceRank * 0.02);
 
-        #URL-Boost
+        # Boost für passende ??? URL
+        $rank += $this->calcURLBoost($eingabe);
+
+        # Boost für Vorkommen der Suchwörter:
+        $rank += $this->calcSuchwortBoost($eingabe);
+
+        # Boost für Suchmaschine
+        if ($this->engineBoost > 0) {
+            $rank *= floatval($this->engineBoost);
+        }
+
+        $this->rank = $rank;
+    }
+
+    # Berechnet den Ranking-Boost durch ??? URL
+    public function calcURLBoost($tmpEingabe)
+    {
         $link = $this->anzeigeLink;
         if (strpos($link, "http") !== 0) {
             $link = "http://" . $link;
         }
-        $link       = @parse_url($link, PHP_URL_HOST) . @parse_url($link, PHP_URL_PATH);
-        $tmpLi      = $link;
-        $tmpEingabe = $metager->getQ();
-        $count      = 0;
-        $tmpLink    = "";
-
+        $link    = @parse_url($link, PHP_URL_HOST) . @parse_url($link, PHP_URL_PATH);
+        $tmpLi   = $link;
+        $count   = 0;
+        $tmpLink = "";
+        # Löscht verschiedene unerwünschte Teile aus $link und $tmpEingabe
         $regex = [
-            "/\s+/si",
-            "/http:/si",
-            "/https:/si",
-            "/www\./si",
-            "/\//si",
-            "/\./si",
-            "/-/si",
+            "/\s+/si", # Leerzeichen
+            "/http:/si", # "http:"
+            "/https:/si", # "https:"
+            "/www\./si", # "www."
+            "/\//si", # "/"
+            "/\./si", # "."
+            "/-/si", # "-"
         ];
         foreach ($regex as $reg) {
             $link       = preg_replace($regex, "", $link);
             $tmpEingabe = preg_replace($regex, "", $tmpEingabe);
         }
-        #die($tmpLi . "<br>" . $link . "<br>" . $tmpEingabe . "<br><br>");
         foreach (str_split($tmpEingabe) as $char) {
-            if (!$char || !$tmpEingabe || strlen($tmpEingabe) === 0 || strlen($char) === 0) {
+            if (!$char
+                || !$tmpEingabe
+                || strlen($tmpEingabe) === 0
+                || strlen($char) === 0
+            ) {
                 continue;
             }
-
             if (strpos(strtolower($tmpLink), strtolower($char)) >= 0) {
                 $count++;
                 $tmpLink = str_replace(urlencode($char), "", $tmpLink);
             }
-            if (strlen($this->descr) > 80 && strlen($link) > 0) {
-                #$rank += $count /((strlen($link)) * 60);
-            }
         }
+        if (strlen($this->descr) > 80 && strlen($link) > 0) {
+            return $count / ((strlen($link)) * 60); # ???
+        } else {
+            return 0;
+        }
+    }
 
-        # Boost für Vorkommen der Suchwörter:
+    # Berechnet den Ranking-Boost durch das Vorkommen von Suchwörtern
+    private function calcSuchwortBoost($tmpEingabe)
+    {
         $maxRank        = 0.1;
         $tmpTitle       = $this->titel;
         $tmpDescription = $this->descr;
         $isWithin       = false;
         $tmpRank        = 0;
-        $tmpEingabe     = $metager->getQ();
         $tmpEingabe     = preg_replace("/\b\w{1,3}\b/si", "", $tmpEingabe);
         $tmpEingabe     = preg_replace("/\s+/si", " ", $tmpEingabe);
-        #die($tmpEingabe);
+
         foreach (explode(" ", trim($tmpEingabe)) as $el) {
             if (strlen($tmpTitle) === 0 || strlen($el) === 0 || strlen($tmpDescription) === 0) {
                 continue;
@@ -118,35 +167,26 @@ class Result
                 }
             }
         }
-        $tmpRank /= sizeof(explode(" ", trim($tmpEingabe))) * 10;
-        $rank += $tmpRank;
-
-        if ($this->engineBoost > 0) {
-            $rank *= floatval($this->engineBoost);
-        }
-
-        $this->rank = $rank;
-    }
 
-    public function getRank()
-    {
-        return $this->rank;
+        $tmpRank /= sizeof(explode(" ", trim($tmpEingabe))) * 10;
+        return $tmpRank;
     }
 
+    # Überprüft ob das Ergebnis aus irgendwelchen Gründen unerwünscht ist.
     public function isValid(\App\MetaGer $metager)
     {
-        # Zunächst die persönlich ( über URL-Parameter ) definierten Blacklists:
+        # Perönliche URL und Domain Blacklist
         if (in_array($this->strippedHost, $metager->getUserHostBlacklist())
             || in_array($this->strippedDomain, $metager->getUserDomainBlacklist())) {
             return false;
         }
 
-        # Jetzt unsere URL und Domain Blacklist
+        # Allgemeine URL und Domain Blacklist
         if ($this->strippedHost !== "" && (in_array($this->strippedHost, $metager->getDomainBlacklist()) || in_array($this->strippedLink, $metager->getUrlBlacklist()))) {
             return false;
         }
 
-        # Nun der Eventuelle Sprachfilter
+        # Eventueller Sprachfilter
         if ($metager->getLang() !== "all") {
             $text = $this->titel . " " . $this->descr;
             $path = app_path() . "/Models/lang.pl";
@@ -158,7 +198,7 @@ class Result
 
         }
 
-        # Wir wenden die Stoppwortsuche an und schmeißen entsprechende Ergebnisse raus:
+        # Stopworte
         foreach ($metager->getStopWords() as $stopWord) {
             $text = $this->titel . " " . $this->descr;
             if (stripos($text, $stopWord) !== false) {
@@ -166,7 +206,7 @@ class Result
             }
         }
 
-        # Die Strinsuche:
+        # Phrasensuche:
         $text = strtolower($this->titel) . " " . strtolower($this->descr);
         foreach ($metager->getPhrases() as $phrase) {
             if (strpos($text, $phrase) === false) {
@@ -175,9 +215,10 @@ class Result
 
         }
 
-        # Abschließend noch 2 Überprüfungen. Einmal den Host filter, der Sicherstellt, dass von jedem Host maximal 3 Links angezeigt werden
-        # und dann noch den Dublettefilter, der sicher stellt, dass wir nach Möglichkeit keinen Link doppelt in der Ergebnisliste haben
-        # Diese Überprüfung führen wir unter bestimmten Bedingungen nicht durch:
+        /* Der Host-Filter der sicherstellt,
+         *  dass von jedem Host maximal 3 Links angezeigt werden.
+         *  Diese Überprüfung führen wir unter bestimmten Bedingungen nicht durch.
+         */
         if ($metager->getSite() === "" &&
             strpos($this->strippedHost, "ncbi.nlm.nih.gov") === false &&
             strpos($this->strippedHost, "twitter.com") === false &&
@@ -190,7 +231,9 @@ class Result
             }
         }
 
-        # Unabhängig davon unser Dublettenfilter:
+        /* Der Dublettefilter, der sicher stellt,
+         *  dass wir nach Möglichkeit keinen Link doppelt in der Ergebnisliste haben.
+         */
         if ($metager->addLink($this->strippedLink)) {
             $metager->addHostCount($this->strippedHost);
             return true;
@@ -199,6 +242,11 @@ class Result
         }
     }
 
+    /* Liest aus einem Link den Host.
+     *  Dieser wird dabei in die Form:
+     *  "http://www.foo.bar.de/test?ja=1" -> "foo.bar.de"
+     *  gebracht.
+     */
     private function getStrippedHost($link)
     {
         if (strpos($link, "http") !== 0) {
@@ -209,6 +257,12 @@ class Result
         $link = preg_replace("/^www\./si", "", $link);
         return $link;
     }
+
+    /* Entfernt "http://", "www" und Parameter von einem Link
+     *  Dieser wird dabei in die Form:
+     *  "http://www.foo.bar.de/test?ja=1" -> "foo.bar.de/test"
+     *  gebracht.
+     */
     private function getStrippedLink($link)
     {
         if (strpos($link, "http") !== 0) {
@@ -220,6 +274,11 @@ class Result
         return $host . $path;
     }
 
+    /* Liest aus einem Link die Domain.
+     *  Dieser wird dabei in die Form:
+     *  "http://www.foo.bar.de/test?ja=1" -> "bar.de"
+     *  gebracht.
+     */
     private function getStrippedDomain($link)
     {
         if (preg_match("/([^\.]*\.[^\.]*)$/si", $link, $match)) {
@@ -229,6 +288,7 @@ class Result
         }
     }
 
+    # Erstellt aus einem Link einen Proxy-Link für unseren Proxy-Service
     private function generateProxyLink($link)
     {
         if (!$link) {
@@ -241,4 +301,11 @@ class Result
         return "https://proxy.suma-ev.de/cgi-bin/nph-proxy.cgi/en/I0/" . $tmp;
 
     }
+
+    # Getter
+
+    public function getRank()
+    {
+        return $this->rank;
+    }
 }
-- 
GitLab