5 require_once __DIR__ . '/../src/init.php';
7 $supportedIndexTypes = array(
8 'application/xhtml+xml',
13 echo "No URL given\n";
17 function removeTags($doc, $tag) {
19 foreach ($doc->getElementsbyTagName($tag) as $elem) {
22 foreach ($elems as $elem) {
23 $elem->parentNode->removeChild($elem);
27 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
30 $existingDoc = $es->get($url);
31 if ($existingDoc && $existingDoc->status == 'indexed') {
32 echo "URL already indexed: $url\n";
36 //FIXME: sourcetitle, sourcelink
38 $req = new \HTTP_Request2($url);
39 $req->setConfig('follow_redirects', true);
40 $req->setConfig('connect_timeout', 5);
41 $req->setConfig('timeout', 10);
42 $req->setConfig('ssl_verify_peer', false);
46 //FIXME: delete if 401 gone or 404 when updating
47 if ($res->getStatus() !== 200) {
48 echo "Response code is not 200 but " . $res->getStatus() . ", stopping\n";
49 //FIXME: update status
53 $mimetype = explode(';', $res->getHeader('content-type'))[0];
54 if (!in_array($mimetype, $supportedIndexTypes)) {
55 echo "MIME type not supported for indexing: $mimetype\n";
56 //FIXME: update status
61 //FIXME: update index only if changed since last index time
62 //FIXME: extract base url from html
63 //FIXME: check if effective url needs updating
64 $url = $res->getEffectiveUrl();
65 $base = new \Net_URL2($url);
67 $indexDoc = new \stdClass();
69 //FIXME: MIME type switch
70 $doc = new \DOMDocument();
71 //@ to hide parse warning messages in invalid html
72 @$doc->loadHTML($res->getBody());
73 $dx = new \DOMXPath($doc);
75 $xbase = $dx->evaluate('/html/head/base[@href]')->item(0);
77 $base = $base->resolve(
78 $xbase->attributes->getNamedItem('href')->textContent
84 removeTags($doc, 'script');
85 removeTags($doc, 'style');
86 removeTags($doc, 'nav');
88 //default content: <body>
89 $xpContext = $doc->getElementsByTagName('body')->item(0);
90 //FIXME: follow meta refresh, no body
91 // example: https://www.gnu.org/software/coreutils/
93 //use microformats content if it exists
94 $xpElems = $dx->query(
95 "//*[contains(concat(' ', normalize-space(@class), ' '), ' e-content ')]"
97 if ($xpElems->length) {
98 $xpContext = $xpElems->item(0);
99 } else if ($doc->getElementById('content')) {
100 //if there is an element with ID "content", we'll use this
101 $xpContext = $doc->getElementById('content');
104 $indexDoc->url = $url;
105 $indexDoc->schemalessUrl = Helper::noSchema($url);
106 $indexDoc->type = 'html';
107 $indexDoc->subtype = '';
108 $indexDoc->mimetype = $mimetype;
109 $indexDoc->domain = parse_url($url, PHP_URL_HOST);
111 //$indexDoc->source = 'FIXME';
112 //$indexDoc->sourcetitle = 'FIXME';
114 $indexDoc->author = new \stdClass();
116 $arXpElems = $dx->query('/html/head/meta[@name="author" and @content]');
117 if ($arXpElems->length) {
118 $indexDoc->author->name = trim(
119 $arXpElems->item(0)->attributes->getNamedItem('content')->textContent
122 $arXpElems = $dx->query('/html/head/link[@rel="author" and @href]');
123 if ($arXpElems->length) {
124 $indexDoc->author->url = trim(
126 $arXpElems->item(0)->attributes->getNamedItem('href')->textContent
132 $arXpElems = $dx->query('/html/head/title');
133 if ($arXpElems->length) {
134 $indexDoc->title = trim(
135 $arXpElems->item(0)->textContent
139 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
140 $indexDoc->$headlinetype = array();
141 foreach ($xpContext->getElementsByTagName($headlinetype) as $xheadline) {
143 $indexDoc->$headlinetype,
144 trim($xheadline->textContent)
149 //FIXME: split paragraphs
150 //FIXME: insert space after br
151 $indexDoc->text = array();
152 $indexDoc->text[] = trim(
154 array("\r\n", "\n", "\r", ' '),
156 $xpContext->textContent
162 foreach ($dx->query('/html/head/meta[@name="keywords" and @content]') as $xkeywords) {
163 $keywords = $xkeywords->attributes->getNamedItem('content')->textContent;
164 foreach (explode(',', $keywords) as $keyword) {
165 $tags[trim($keyword)] = true;
168 $indexDoc->tags = array_keys($tags);
171 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.created" and @content]');
172 if ($arXpdates->length) {
173 $indexDoc->crdate = date(
176 $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
180 //FIXME: keep creation date from database, or use modified date if we
181 // do not have it there
183 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.modified" and @content]');
184 if ($arXpdates->length) {
185 $indexDoc->modate = date(
188 $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
192 $lm = $res->getHeader('last-modified');
194 $indexDoc->modate = date('c', strtotime($lm));
196 //use current time since we don't have any other data
197 $indexDoc->modate = date('c');
202 //there may be "en-US" and "de-DE"
203 $xlang = $doc->documentElement->attributes->getNamedItem('lang');
205 $indexDoc->language = strtolower(substr($xlang->textContent, 0, 2));
207 //FIXME: fallback, autodetection
208 //FIXME: check noindex
210 //var_dump($indexDoc);die();
212 $indexDoc->status = 'indexed';
214 //FIXME: update index if it exists already
215 $r = new Elasticsearch_Request(
216 $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
217 \HTTP_Request2::METHOD_PUT
219 $r->setBody(json_encode($indexDoc));