Support location redirect changes
[phinde.git] / src / phinde / Fetcher.php
index dccb118c0ac52f0bd47fd73029b65cb7b5780a69..c80032d48bf67a5fb5a0bffe8826554c2db35244 100644 (file)
@@ -15,18 +15,22 @@ class Fetcher
      */
     public function fetch($url, $actions, $force = false)
     {
-        $esDoc = $this->es->get($url);
+        $url = Helper::rewriteUrl($url);
+
+        $esDoc  = $this->es->get($url);
+        $locUrl = null;
         if (isset($esDoc->status->location)
             && $esDoc->status->location != ''
         ) {
-            //TODO: what if location redirects change?
-            $url = $esDoc->status->location;
-            $esDoc = $this->es->get($url);
+            //Location redirect: Use modified time of known target
+            $locUrl = $esDoc->status->location;
+            $locUrl = Helper::rewriteUrl($locUrl);
+            $esDoc = $this->es->get($locUrl);
         }
 
         $types = array();
         foreach ($actions as $action) {
-            $types = array_merge($action::$supportedTypes);
+            $types = array_merge($types, array_keys($action::$supportedTypes));
         }
         $types = array_unique($types);
 
@@ -41,8 +45,17 @@ class Fetcher
         }
 
         $res = $req->send();
+        $effUrl = Helper::removeAnchor($res->getEffectiveUrl());
+        $effUrl = Helper::rewriteUrl($effUrl);
+
         if ($res->getStatus() === 304) {
             //not modified since last time, so don't crawl again
+            if ($locUrl !== null && $effUrl != $locUrl) {
+                //location URL changed, and we used the wrong crawl timestampx
+                $this->storeRedirect($url, $effUrl);
+                return $this->fetch($url, $actions, $force);
+            }
+
             Log::info("Not modified since last fetch");
             return false;
         } else if ($res->getStatus() !== 200) {
@@ -52,7 +65,6 @@ class Fetcher
             );
         }
 
-        $effUrl = Helper::removeAnchor($res->getEffectiveUrl());
         if ($effUrl != $url) {
             $this->storeRedirect($url, $effUrl);
             $url = $effUrl;