6a13afd546c3293837654dd9507821ed59239b7b
[phinde.git] / bin / index.php
1 #!/usr/bin/env php
2 <?php
3 namespace phinde;
4 // index a given URL
5 set_include_path(__DIR__ . '/../src/' . PATH_SEPARATOR . get_include_path());
6 require_once __DIR__ . '/../data/config.php';
7 require_once 'HTTP/Request2.php';
8 require_once 'Elasticsearch.php';
9 require_once 'Elasticsearch/Request.php';
10 require_once 'functions.php';
11
12 $supportedIndexTypes = array(
13     'application/xhtml+xml',
14     'text/html',
15 );
16
17 if ($argc < 2) {
18     echo "No URL given\n";
19     exit(1);
20 }
21
22 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
23
24 $url = $argv[1];
25 $existingDoc = $es->get($url);
26 if ($existingDoc && $existingDoc->status == 'indexed') {
27     echo "URL already indexed: $url\n";
28     exit(0);
29 }
30 //FIXME: sourcetitle, sourcelink
31
32 //FIXME: enable redirects
33 //FIXME: enable ssl 
34 $req = new \HTTP_Request2($url);
35 $req->setConfig('connect_timeout', 5);
36 $req->setConfig('timeout', 10);
37 $res = $req->send();
38 //FIXME: try-catch
39
40 //FIXME: delete if 401 gone or 404 when updating
41 if ($res->getStatus() !== 200) {
42     echo "Response code is not 200 but " . $res->getStatus() . ", stopping\n";
43     //FIXME: update status
44     exit(3);
45 }
46
47 $mimetype = explode(';', $res->getHeader('content-type'))[0];
48 if (!in_array($mimetype, $supportedIndexTypes)) {
49     echo "MIME type not supported for indexing: $mimetype\n";
50     //FIXME: update status
51     exit(4);
52 }
53
54
55 //FIXME: update index only if changed since last index time
56 //FIXME: extract base url from html
57 //FIXME: use final URL after redirects
58 $base = new \Net_URL2($url);
59
60 $indexDoc = new \stdClass();
61
62 //FIXME: MIME type switch
63 $doc = new \DOMDocument();
64 //@ to hide parse warning messages in invalid html
65 @$doc->loadHTML($res->getBody());
66 $sx = simplexml_import_dom($doc);
67
68 $indexDoc->url = $url;
69 $indexDoc->type = 'html';
70 $indexDoc->subtype = '';
71 $indexDoc->mimetype = $mimetype;
72 $indexDoc->domain   = parse_url($url, PHP_URL_HOST);
73
74 //$indexDoc->source = 'FIXME';
75 //$indexDoc->sourcetitle = 'FIXME';
76
77 $indexDoc->author = new \stdClass();
78
79 $arSxElems = $sx->xpath('/html/head/meta[@name="author"]');
80 if (count($arSxElems)) {
81     $indexDoc->author->name = trim($arSxElems[0]['content']);
82 }
83 $arSxElems = $sx->xpath('/html/head/link[@rel="author"]');
84 if (count($arSxElems)) {
85     $indexDoc->author->url = (string) $base->resolve($arSxElems[0]['href']);
86 }
87
88 $indexDoc->title = (string) $sx->head->title;
89 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
90     $indexDoc->$headlinetype = array();
91     //FIXME: limit to h-entry children
92     foreach ($sx->xpath('//' . $headlinetype) as $xheadline) {
93         array_push(
94             $indexDoc->$headlinetype,
95             trim(dom_import_simplexml($xheadline)->textContent)
96         );
97     }
98 }
99
100 //FIXME: limit to h-entry e-content
101 //FIXME: insert space after br
102 //FIXME: remove javascript
103 $indexDoc->text = array();
104 foreach ($doc->getElementsByTagName('body') as $body) {
105     $indexDoc->text[] = trim(
106         str_replace(
107             array("\r\n", "\n", "\r", '  '),
108             ' ',
109             $body->textContent
110         )
111     );
112 }
113
114 //tags
115 $tags = array();
116 foreach ($sx->xpath('/html/head/meta[@name="keywords"]') as $xkeywords) {
117     foreach (explode(',', $xkeywords['content']) as $keyword) {
118         $tags[trim($keyword)] = true;
119     }
120 }
121 $indexDoc->tags = array_keys($tags);
122
123 //dates
124 $arSxdates = $sx->xpath('/html/head/meta[@name="DC.date.created"]');
125 if (count($arSxdates)) {
126     $indexDoc->crdate = date('c', strtotime((string) $arSxdates[0]['content']));
127 }
128 //FIXME: keep creation date from database, or use modified date if we
129 // do not have it there
130
131 $arSxdates = $sx->xpath('/html/head/meta[@name="DC.date.modified"]');
132 if (count($arSxdates)) {
133     $indexDoc->modate = date('c', strtotime((string) $arSxdates[0]['content']));
134 } else {
135     $lm = $res->getHeader('last-modified');
136     if ($lm !== null) {
137         $indexDoc->modate = date('c', strtotime($lm));
138     } else {
139         //use current time since we don't have any other data
140         $indexDoc->modate = date('c');
141     }
142 }
143
144 //language
145 //there may be "en-US" and "de-DE"
146 $indexDoc->language = substr((string) $sx['lang'], 0, 2);
147 //FIXME: fallback, autodetection
148 //FIXME: check noindex
149
150
151 //var_dump($indexDoc);
152
153 $indexDoc->status = 'indexed';
154
155 //FIXME: update index if it exists already
156 $r = new Elasticsearch_Request(
157     $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
158     \HTTP_Request2::METHOD_PUT
159 );
160 $r->setBody(json_encode($indexDoc));
161 $r->send();
162
163
164 ?>