Add crawlBlacklist configuration option
authorChristian Weiske <cweiske@cweiske.de>
Tue, 30 Aug 2016 11:35:05 +0000 (13:35 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 30 Aug 2016 11:35:05 +0000 (13:35 +0200)
Resolves: #7

data/config.php.dist
src/phinde/Crawler.php

index 3c1ccea35f33e1910c01c6413ceaa5fb90bf20be..20bf208356fcac5c017d61609efa15766c5d114b 100644 (file)
@@ -10,6 +10,9 @@ $GLOBALS['phinde'] = array(
     'blacklist' => array(
         'http://bad.example.org/'
     ),
+    //list of regexes for URLs that should not be crawled
+    'crawlBlacklist' => array(
+    ),
     //list of URLs that should be subscribed to with PubSubHubbub
     'subscriptions' => array(
         'http://www.example.org/feed',
index a63815de69f93e008e5ceed04b4b0958f8f0e83c..72726a5a950fb808ac7c0019f2306c07cc42c855 100644 (file)
@@ -31,6 +31,7 @@ class Crawler
         }
 
         $linkInfos = $this->extractLinks($res);
+        $linkInfos = $this->filterLinks($linkInfos);
         if ($this->showLinksOnly) {
             $this->showLinks($linkInfos);
         } else {
@@ -78,25 +79,45 @@ class Crawler
         return $extractor->extract($res);
     }
 
-    protected function enqueue($linkInfos)
+    protected function filterLinks($linkInfos)
     {
+        $filteredLinkInfos = array();
         foreach ($linkInfos as $linkInfo) {
-            if ($this->es->isKnown($linkInfo->url)) {
-                continue;
-            }
             $allowed = Helper::isUrlAllowed($linkInfo->url);
             $crawl   = $allowed;
             $index   = $GLOBALS['phinde']['indexNonAllowed'] || $allowed;
 
-            if ($crawl || $index) {
+            if ($crawl && count($GLOBALS['phinde']['crawlBlacklist'])) {
+                foreach ($GLOBALS['phinde']['crawlBlacklist'] as $bl) {
+                    if (preg_match('#' . $bl . '#', $linkInfo->url)) {
+                        $crawl = false;
+                    }
+                }
+            }
+
+            $linkInfo->known = $this->es->isKnown($linkInfo->url);
+            $linkInfo->crawl = $crawl;
+            $linkInfo->index = $index;
+            $filteredLinkInfos[] = $linkInfo;
+        }
+        return $filteredLinkInfos;
+    }
+
+    protected function enqueue($linkInfos)
+    {
+        foreach ($linkInfos as $linkInfo) {
+            if ($linkInfo->known) {
+                continue;
+            }
+            if ($linkInfo->crawl || $linkInfo->index) {
                 $this->es->markQueued($linkInfo->url);
             }
-            if ($index) {
+            if ($linkInfo->index) {
                 $this->queue->addToIndex(
                     $linkInfo->url, $linkInfo->title, $linkInfo->source
                 );
             }
-            if ($allowed) {
+            if ($linkInfo->crawl) {
                 $this->queue->addToCrawl($linkInfo->url);
             }
         }
@@ -107,8 +128,11 @@ class Crawler
         foreach ($linkInfos as $linkInfo) {
             echo $linkInfo->url . "\n";
             if ($linkInfo->title) {
-                echo '  title: ' . $linkInfo->title . "\n";
+                echo '   title: ' . $linkInfo->title . "\n";
                 echo '  source: ' . $linkInfo->source . "\n";
+                echo '   known: ' . intval($linkInfo->known)
+                    . ', crawl: ' . intval($linkInfo->crawl)
+                    . ', index: ' . intval($linkInfo->index) . "\n";
             }
         }
     }