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