1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#!/usr/bin/env php
<?php
namespace phinde;
require_once __DIR__ . '/../src/init.php';
$supportedCrawlTypes = array(
'text/html', 'application/xhtml+xml'
);
if ($argc < 2) {
echo "No URL given\n";
exit(1);
}
$es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
$url = $argv[1];
if (!Helper::isUrlAllowed($url)) {
echo "Domain is not allowed; not crawling\n";
exit(2);
}
$req = new \HTTP_Request2($url);
//FIXME: send supported mime types in header
$res = $req->send();
if ($res->getStatus() !== 200) {
echo "Response code is not 200 but " . $res->getStatus() . ", stopping\n";
exit(3);
}
$mimetype = explode(';', $res->getHeader('content-type'))[0];
if (!in_array($mimetype, $supportedCrawlTypes)) {
echo "MIME type not supported for crawling: $mimetype\n";
exit(4);
}
//FIXME: mime type switch for cdata
$doc = new \DOMDocument();
//@ to hide parse warning messages in invalid html
@$doc->loadHTMLFile($url);
//FIXME: extract base url from html
$base = new \Net_URL2($url);
$xpath = new \DOMXPath($doc);
$links = $xpath->evaluate('//a');
//FIXME: link rel, img, video
$alreadySeen = array();
foreach ($links as $link) {
$linkTitle = $link->textContent;
$href = '';
foreach ($link->attributes as $attribute) {
if ($attribute->name == 'href') {
$href = $attribute->textContent;
}
}
if ($href == '' || $href{0} == '#') {
//link on this page
continue;
}
$linkUrlObj = $base->resolve($href);
$linkUrlObj->setFragment(false);
$linkUrl = (string) $linkUrlObj;
if (isset($alreadySeen[$linkUrl])) {
continue;
}
switch ($linkUrlObj->getScheme()) {
case 'http':
case 'https':
break;
default:
continue 2;
}
if ($es->isKnown($linkUrl)) {
continue;
}
//FIXME: check target type
//FIXME: check nofollow
//var_dump($linkTitle, $linkUrl);
$es->markQueued($linkUrl);
addToIndex($linkUrl, $linkTitle, $url);
if (Helper::isUrlAllowed($linkUrl)) {
addToCrawl($linkUrl);
}
$alreadySeen[$linkUrl] = true;
}
function addToIndex($linkUrl, $linkTitle, $sourceUrl)
{
echo "Queuing for indexing: $linkUrl\n";
$gmclient = new \GearmanClient();
$gmclient->addServer('127.0.0.1');
$gmclient->doBackground(
'phinde_index',
serialize(
array(
'url' => $linkUrl,
'title' => $linkTitle,
'source' => $sourceUrl
)
)
);
if ($gmclient->returnCode() != GEARMAN_SUCCESS) {
echo 'Error queueing URL indexing for '
. $linkUrl . "\n"
. 'Error code: ' . $gmclient->returnCode() . "\n";
exit(2);
}
}
function addToCrawl($linkUrl)
{
echo "Queuing for crawling: $linkUrl\n";
$gmclient = new \GearmanClient();
$gmclient->addServer('127.0.0.1');
$gmclient->doBackground(
'phinde_crawl',
serialize(
array(
'url' => $linkUrl
)
)
);
if ($gmclient->returnCode() != GEARMAN_SUCCESS) {
echo 'Error queueing URL crawling for '
. $linkUrl . "\n"
. 'Error code: ' . $gmclient->returnCode() . "\n";
exit(2);
}
}
?>
|