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);
29 $req = new \HTTP_Request2($url);
30 $req->setConfig('follow_redirects', true);
31 $req->setConfig('connect_timeout', 5);
32 $req->setConfig('timeout', 10);
33 $req->setConfig('ssl_verify_peer', false);
36 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
37 $existingDoc = $es->get($url);
38 if ($existingDoc && $existingDoc->status == 'indexed') {
39 $nMoDate = strtotime($existingDoc->modate);
40 $refreshtime = $GLOBALS['phinde']['refreshtime'];
41 if (time() - $nMoDate < $refreshtime) {
42 echo "URL already indexed less than $refreshtime seconds ago: $url\n";
46 $req->setHeader('If-Modified-Since: ' . date('r', $nMoDate));
48 //FIXME: sourcetitle, sourcelink
53 if ($res->getStatus() === 304) {
54 //not modified since last time
55 //FIXME: store "last try" time
57 } else if ($res->getStatus() !== 200) {
58 //FIXME: delete if 401 gone or 404 when updating
59 echo "Response code is not 200 but " . $res->getStatus() . ", stopping\n";
60 //FIXME: update status
64 $mimetype = explode(';', $res->getHeader('content-type'))[0];
65 if (!in_array($mimetype, $supportedIndexTypes)) {
66 echo "MIME type not supported for indexing: $mimetype\n";
67 //FIXME: update status
72 //FIXME: update index only if changed since last index time
73 //FIXME: extract base url from html
74 //FIXME: check if effective url needs updating
75 $url = $res->getEffectiveUrl();
76 $base = new \Net_URL2($url);
78 $indexDoc = new \stdClass();
80 //FIXME: MIME type switch
81 $doc = new \DOMDocument();
82 //@ to hide parse warning messages in invalid html
83 @$doc->loadHTML($res->getBody());
84 $dx = new \DOMXPath($doc);
86 $xbase = $dx->evaluate('/html/head/base[@href]')->item(0);
88 $base = $base->resolve(
89 $xbase->attributes->getNamedItem('href')->textContent
93 $meta = $dx->evaluate('/html/head/meta[@name="robots" and @content]')
96 $robots = $meta->attributes->getNamedItem('content')->textContent;
97 foreach (explode(',', $robots) as $value) {
98 if (trim($value) == 'noindex') {
99 echo "URL does not want to be indexed: $url\n";
106 removeTags($doc, 'script');
107 removeTags($doc, 'style');
108 removeTags($doc, 'nav');
110 //default content: <body>
111 $xpContext = $doc->getElementsByTagName('body')->item(0);
112 //FIXME: follow meta refresh, no body
113 // example: https://www.gnu.org/software/coreutils/
115 //use microformats content if it exists
116 $xpElems = $dx->query(
117 "//*[contains(concat(' ', normalize-space(@class), ' '), ' e-content ')]"
119 if ($xpElems->length) {
120 $xpContext = $xpElems->item(0);
121 } else if ($doc->getElementById('content')) {
122 //if there is an element with ID "content", we'll use this
123 $xpContext = $doc->getElementById('content');
126 $indexDoc->url = $url;
127 $indexDoc->schemalessUrl = Helper::noSchema($url);
128 $indexDoc->type = 'html';
129 $indexDoc->subtype = '';
130 $indexDoc->mimetype = $mimetype;
131 $indexDoc->domain = parse_url($url, PHP_URL_HOST);
133 //$indexDoc->source = 'FIXME';
134 //$indexDoc->sourcetitle = 'FIXME';
136 $indexDoc->author = new \stdClass();
138 $arXpElems = $dx->query('/html/head/meta[@name="author" and @content]');
139 if ($arXpElems->length) {
140 $indexDoc->author->name = trim(
141 $arXpElems->item(0)->attributes->getNamedItem('content')->textContent
144 $arXpElems = $dx->query('/html/head/link[@rel="author" and @href]');
145 if ($arXpElems->length) {
146 $indexDoc->author->url = trim(
148 $arXpElems->item(0)->attributes->getNamedItem('href')->textContent
154 $arXpElems = $dx->query('/html/head/title');
155 if ($arXpElems->length) {
156 $indexDoc->title = trim(
157 $arXpElems->item(0)->textContent
161 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
162 $indexDoc->$headlinetype = array();
163 foreach ($xpContext->getElementsByTagName($headlinetype) as $xheadline) {
165 $indexDoc->$headlinetype,
166 trim($xheadline->textContent)
171 //FIXME: split paragraphs
172 //FIXME: insert space after br
173 $indexDoc->text = array();
174 $indexDoc->text[] = trim(
176 array("\r\n", "\n", "\r", ' '),
178 $xpContext->textContent
184 foreach ($dx->query('/html/head/meta[@name="keywords" and @content]') as $xkeywords) {
185 $keywords = $xkeywords->attributes->getNamedItem('content')->textContent;
186 foreach (explode(',', $keywords) as $keyword) {
187 $tags[trim($keyword)] = true;
190 $indexDoc->tags = array_keys($tags);
193 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.created" and @content]');
194 if ($arXpdates->length) {
195 $indexDoc->crdate = date(
198 $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
202 //FIXME: keep creation date from database, or use modified date if we
203 // do not have it there
205 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.modified" and @content]');
206 if ($arXpdates->length) {
207 $indexDoc->modate = date(
210 $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
214 $lm = $res->getHeader('last-modified');
216 $indexDoc->modate = date('c', strtotime($lm));
218 //use current time since we don't have any other data
219 $indexDoc->modate = date('c');
224 //there may be "en-US" and "de-DE"
225 $xlang = $doc->documentElement->attributes->getNamedItem('lang');
227 $indexDoc->language = strtolower(substr($xlang->textContent, 0, 2));
229 //FIXME: fallback, autodetection
230 //FIXME: check noindex
232 //var_dump($indexDoc);die();
234 $indexDoc->status = 'indexed';
236 //FIXME: update index if it exists already
237 $r = new Elasticsearch_Request(
238 $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
239 \HTTP_Request2::METHOD_PUT
241 $r->setBody(json_encode($indexDoc));