sanitize title better
[phinde.git] / src / phinde / Crawler.php
1 <?php
2 namespace phinde;
3
4 class Crawler
5 {
6     protected $es;
7     protected $queue;
8
9     /**
10      * If the links only should be shown, not queued
11      */
12     protected $showLinksOnly = false;
13
14     static $supportedIndexTypes = array(
15         'application/atom+xml'  => '\\phinde\\LinkExtractor\\Atom',
16         'application/xhtml+xml' => '\\phinde\\LinkExtractor\\Html',
17         'text/html'             => '\\phinde\\LinkExtractor\\Html',
18     );
19
20     public function __construct()
21     {
22         $this->es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
23         $this->queue = new Queue();
24     }
25
26     public function crawl($url)
27     {
28         $res       = $this->fetch($url);
29         $linkInfos = $this->extractLinks($res);
30         if ($this->showLinksOnly) {
31             $this->showLinks($linkInfos);
32         } else {
33             $this->enqueue($linkInfos);
34         }
35     }
36
37     protected function fetch($url)
38     {
39         $req = new HttpRequest($url);
40         $req->setHeader(
41             'accept',
42             implode(',', array_keys(static::$supportedIndexTypes))
43         );
44         $res = $req->send();
45         if ($res->getStatus() !== 200) {
46             throw new \Exception(
47                 "Response code is not 200 but "
48                 . $res->getStatus() . ", stopping"
49             );
50         }
51         return $res;
52     }
53
54     protected function extractLinks(\HTTP_Request2_Response $res)
55     {
56         $mimetype = explode(';', $res->getHeader('content-type'))[0];
57         if (!isset(static::$supportedIndexTypes[$mimetype])) {
58             echo "MIME type not supported for indexing: $mimetype\n";
59             return array();
60         }
61
62         $class = static::$supportedIndexTypes[$mimetype];
63         $extractor = new $class();
64         return $extractor->extract($res);
65     }
66
67     protected function enqueue($linkInfos)
68     {
69         foreach ($linkInfos as $linkInfo) {
70             if ($this->es->isKnown($linkInfo->url)) {
71                 continue;
72             }
73             $this->es->markQueued($linkInfo->url);
74             $this->queue->addToIndex(
75                 $linkInfo->url, $linkInfo->title, $linkInfo->source
76             );
77             if (Helper::isUrlAllowed($linkInfo->url)) {
78                 $this->queue->addToCrawl($linkInfo->url);
79             }
80         }
81     }
82
83     protected function showLinks($linkInfos)
84     {
85         foreach ($linkInfos as $linkInfo) {
86             echo $linkInfo->url . "\n";
87             if ($linkInfo->title) {
88                 echo '  title: ' . $linkInfo->title . "\n";
89                 echo '  source: ' . $linkInfo->source . "\n";
90             }
91         }
92     }
93
94     public function setShowLinksOnly($showLinksOnly)
95     {
96         $this->showLinksOnly = $showLinksOnly;
97     }
98 }
99 ?>