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';
12 $supportedIndexTypes = array(
13 'application/xhtml+xml',
18 echo "No URL given\n";
22 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
25 $existingDoc = $es->get($url);
26 if ($existingDoc && $existingDoc->status == 'indexed') {
27 echo "URL already indexed: $url\n";
30 //FIXME: sourcetitle, sourcelink
32 //FIXME: enable redirects
34 $req = new \HTTP_Request2($url);
35 $req->setConfig('connect_timeout', 5);
36 $req->setConfig('timeout', 10);
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
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
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);
60 $indexDoc = new \stdClass();
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);
68 $indexDoc->url = $url;
69 $indexDoc->type = 'html';
70 $indexDoc->subtype = '';
71 $indexDoc->mimetype = $mimetype;
72 $indexDoc->domain = parse_url($url, PHP_URL_HOST);
74 //$indexDoc->source = 'FIXME';
75 //$indexDoc->sourcetitle = 'FIXME';
77 $indexDoc->author = new \stdClass();
79 $arSxElems = $sx->xpath('/html/head/meta[@name="author"]');
80 if (count($arSxElems)) {
81 $indexDoc->author->name = trim($arSxElems[0]['content']);
83 $arSxElems = $sx->xpath('/html/head/link[@rel="author"]');
84 if (count($arSxElems)) {
85 $indexDoc->author->url = (string) $base->resolve($arSxElems[0]['href']);
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) {
94 $indexDoc->$headlinetype,
95 trim(dom_import_simplexml($xheadline)->textContent)
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(
107 array("\r\n", "\n", "\r", ' '),
116 foreach ($sx->xpath('/html/head/meta[@name="keywords"]') as $xkeywords) {
117 foreach (explode(',', $xkeywords['content']) as $keyword) {
118 $tags[trim($keyword)] = true;
121 $indexDoc->tags = array_keys($tags);
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']));
128 //FIXME: keep creation date from database, or use modified date if we
129 // do not have it there
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']));
135 $lm = $res->getHeader('last-modified');
137 $indexDoc->modate = date('c', strtotime($lm));
139 //use current time since we don't have any other data
140 $indexDoc->modate = date('c');
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
151 //var_dump($indexDoc);
153 $indexDoc->status = 'indexed';
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
160 $r->setBody(json_encode($indexDoc));