5 require_once __DIR__ . '/../src/init.php';
7 $supportedIndexTypes = array(
8 'application/xhtml+xml',
13 echo "No URL given\n";
17 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
20 $existingDoc = $es->get($url);
21 if ($existingDoc && $existingDoc->status == 'indexed') {
22 echo "URL already indexed: $url\n";
25 //FIXME: sourcetitle, sourcelink
27 //FIXME: enable redirects
29 $req = new \HTTP_Request2($url);
30 $req->setConfig('connect_timeout', 5);
31 $req->setConfig('timeout', 10);
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
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
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);
55 $indexDoc = new \stdClass();
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);
63 $indexDoc->url = $url;
64 $indexDoc->type = 'html';
65 $indexDoc->subtype = '';
66 $indexDoc->mimetype = $mimetype;
67 $indexDoc->domain = parse_url($url, PHP_URL_HOST);
69 //$indexDoc->source = 'FIXME';
70 //$indexDoc->sourcetitle = 'FIXME';
72 $indexDoc->author = new \stdClass();
74 $arSxElems = $sx->xpath('/html/head/meta[@name="author"]');
75 if (count($arSxElems)) {
76 $indexDoc->author->name = trim($arSxElems[0]['content']);
78 $arSxElems = $sx->xpath('/html/head/link[@rel="author"]');
79 if (count($arSxElems)) {
80 $indexDoc->author->url = (string) $base->resolve($arSxElems[0]['href']);
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) {
89 $indexDoc->$headlinetype,
90 trim(dom_import_simplexml($xheadline)->textContent)
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(
102 array("\r\n", "\n", "\r", ' '),
111 foreach ($sx->xpath('/html/head/meta[@name="keywords"]') as $xkeywords) {
112 foreach (explode(',', $xkeywords['content']) as $keyword) {
113 $tags[trim($keyword)] = true;
116 $indexDoc->tags = array_keys($tags);
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']));
123 //FIXME: keep creation date from database, or use modified date if we
124 // do not have it there
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']));
130 $lm = $res->getHeader('last-modified');
132 $indexDoc->modate = date('c', strtotime($lm));
134 //use current time since we don't have any other data
135 $indexDoc->modate = date('c');
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
146 //var_dump($indexDoc);
148 $indexDoc->status = 'indexed';
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
155 $r->setBody(json_encode($indexDoc));