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 $dx = new \DOMXPath($doc);
65 foreach ($doc->getElementsbyTagName('script') as $elem) {
68 foreach ($elems as $elem) {
69 $elem->parentNode->removeChild($elem);
72 //default content: <body>
73 $xpContext = $doc->getElementsByTagName('body')->item(0);
75 //use microformats content if it exists
76 $xpElems = $dx->query(
77 "//*[contains(concat(' ', normalize-space(@class), ' '), ' e-content ')]"
79 if ($xpElems->length) {
80 $xpContext = $xpElems->item(0);
81 } else if ($doc->getElementById('content')) {
82 //if there is an element with ID "content", we'll use this
83 $xpContext = $doc->getElementById('content');
86 $indexDoc->url = $url;
87 $indexDoc->schemalessUrl = Helper::noSchema($url);
88 $indexDoc->type = 'html';
89 $indexDoc->subtype = '';
90 $indexDoc->mimetype = $mimetype;
91 $indexDoc->domain = parse_url($url, PHP_URL_HOST);
93 //$indexDoc->source = 'FIXME';
94 //$indexDoc->sourcetitle = 'FIXME';
96 $indexDoc->author = new \stdClass();
98 $arXpElems = $dx->query('/html/head/meta[@name="author"]');
99 if ($arXpElems->length) {
100 $indexDoc->author->name = trim(
101 $arXpElems->item(0)->attributes->getNamedItem('content')->textContent
104 $arXpElems = $dx->query('/html/head/link[@rel="author"]');
105 if ($arXpElems->length) {
106 $indexDoc->author->url = trim(
108 $arXpElems->item(0)->attributes->getNamedItem('href')->textContent
114 $arXpElems = $dx->query('/html/head/title');
115 if ($arXpElems->length) {
116 $indexDoc->title = trim(
117 $arXpElems->item(0)->textContent
121 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
122 $indexDoc->$headlinetype = array();
123 foreach ($xpContext->getElementsByTagName($headlinetype) as $xheadline) {
125 $indexDoc->$headlinetype,
126 trim($xheadline->textContent)
131 //FIXME: limit to h-entry e-content
132 //FIXME: insert space after br
133 $indexDoc->text = array();
134 $indexDoc->text[] = trim(
136 array("\r\n", "\n", "\r", ' '),
138 $xpContext->textContent
144 foreach ($dx->query('/html/head/meta[@name="keywords"]') as $xkeywords) {
145 $keywords = $xkeywords->attributes->getNamedItem('content')->textContent;
146 foreach (explode(',', $keywords) as $keyword) {
147 $tags[trim($keyword)] = true;
150 $indexDoc->tags = array_keys($tags);
153 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.created"]');
154 if ($arXpdates->length) {
155 $indexDoc->crdate = date(
158 $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
162 //FIXME: keep creation date from database, or use modified date if we
163 // do not have it there
165 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.modified"]');
166 if ($arXpdates->length) {
167 $indexDoc->modate = date(
170 $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
174 $lm = $res->getHeader('last-modified');
176 $indexDoc->modate = date('c', strtotime($lm));
178 //use current time since we don't have any other data
179 $indexDoc->modate = date('c');
184 //there may be "en-US" and "de-DE"
185 $indexDoc->language = strtolower(
187 $doc->documentElement->attributes->getNamedItem('lang')->textContent,
191 //FIXME: fallback, autodetection
192 //FIXME: check noindex
194 //var_dump($indexDoc);die();
196 $indexDoc->status = 'indexed';
198 //FIXME: update index if it exists already
199 $r = new Elasticsearch_Request(
200 $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
201 \HTTP_Request2::METHOD_PUT
203 $r->setBody(json_encode($indexDoc));