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 $req = new \HTTP_Request2($url);
28 $req->setConfig('follow_redirects', true);
29 $req->setConfig('connect_timeout', 5);
30 $req->setConfig('timeout', 10);
31 $req->setConfig('ssl_verify_peer', false);
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 $url = $res->getEffectiveUrl();
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->schemalessUrl = Helper::noSchema($url);
65 $indexDoc->type = 'html';
66 $indexDoc->subtype = '';
67 $indexDoc->mimetype = $mimetype;
68 $indexDoc->domain = parse_url($url, PHP_URL_HOST);
70 //$indexDoc->source = 'FIXME';
71 //$indexDoc->sourcetitle = 'FIXME';
73 $indexDoc->author = new \stdClass();
75 $arSxElems = $sx->xpath('/html/head/meta[@name="author"]');
76 if (count($arSxElems)) {
77 $indexDoc->author->name = trim($arSxElems[0]['content']);
79 $arSxElems = $sx->xpath('/html/head/link[@rel="author"]');
80 if (count($arSxElems)) {
81 $indexDoc->author->url = (string) $base->resolve($arSxElems[0]['href']);
84 $indexDoc->title = (string) $sx->head->title;
85 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
86 $indexDoc->$headlinetype = array();
87 //FIXME: limit to h-entry children
88 foreach ($sx->xpath('//' . $headlinetype) as $xheadline) {
90 $indexDoc->$headlinetype,
91 trim(dom_import_simplexml($xheadline)->textContent)
96 //FIXME: limit to h-entry e-content
97 //FIXME: insert space after br
98 //FIXME: remove javascript
99 $indexDoc->text = array();
100 foreach ($doc->getElementsByTagName('body') as $body) {
101 $indexDoc->text[] = trim(
103 array("\r\n", "\n", "\r", ' '),
112 foreach ($sx->xpath('/html/head/meta[@name="keywords"]') as $xkeywords) {
113 foreach (explode(',', $xkeywords['content']) as $keyword) {
114 $tags[trim($keyword)] = true;
117 $indexDoc->tags = array_keys($tags);
120 $arSxdates = $sx->xpath('/html/head/meta[@name="DC.date.created"]');
121 if (count($arSxdates)) {
122 $indexDoc->crdate = date('c', strtotime((string) $arSxdates[0]['content']));
124 //FIXME: keep creation date from database, or use modified date if we
125 // do not have it there
127 $arSxdates = $sx->xpath('/html/head/meta[@name="DC.date.modified"]');
128 if (count($arSxdates)) {
129 $indexDoc->modate = date('c', strtotime((string) $arSxdates[0]['content']));
131 $lm = $res->getHeader('last-modified');
133 $indexDoc->modate = date('c', strtotime($lm));
135 //use current time since we don't have any other data
136 $indexDoc->modate = date('c');
141 //there may be "en-US" and "de-DE"
142 $indexDoc->language = strtolower(substr((string) $sx['lang'], 0, 2));
143 //FIXME: fallback, autodetection
144 //FIXME: check noindex
147 //var_dump($indexDoc);
149 $indexDoc->status = 'indexed';
151 //FIXME: update index if it exists already
152 $r = new Elasticsearch_Request(
153 $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
154 \HTTP_Request2::METHOD_PUT
156 $r->setBody(json_encode($indexDoc));