some styling, noindex for search result pages
[phinde.git] / bin / index.php
1 #!/usr/bin/env php
2 <?php
3 namespace phinde;
4 // index a given URL
5 require_once __DIR__ . '/../src/init.php';
6
7 $supportedIndexTypes = array(
8     'application/xhtml+xml',
9     'text/html',
10 );
11
12 if ($argc < 2) {
13     echo "No URL given\n";
14     exit(1);
15 }
16
17 function removeTags($doc, $tag) {
18     $elems = array();
19     foreach ($doc->getElementsbyTagName($tag) as $elem) {
20         $elems[] = $elem;
21     }
22     foreach ($elems as $elem) {
23         $elem->parentNode->removeChild($elem);
24     }
25 }
26
27 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
28
29 $url = $argv[1];
30 $existingDoc = $es->get($url);
31 if ($existingDoc && $existingDoc->status == 'indexed') {
32     echo "URL already indexed: $url\n";
33     exit(0);
34 }
35 //FIXME: size limit
36 //FIXME: sourcetitle, sourcelink
37
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);
43 $res = $req->send();
44 //FIXME: try-catch
45
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
50     exit(3);
51 }
52
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
57     exit(4);
58 }
59
60
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);
66
67 $indexDoc = new \stdClass();
68
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);
74
75 //remove script tags
76 removeTags($doc, 'script');
77 removeTags($doc, 'style');
78 removeTags($doc, 'nav');
79
80 //default content: <body>
81 $xpContext = $doc->getElementsByTagName('body')->item(0);
82 //FIXME: follow meta refresh, no body
83 // example: https://www.gnu.org/software/coreutils/
84
85 //use microformats content if it exists
86 $xpElems = $dx->query(
87     "//*[contains(concat(' ', normalize-space(@class), ' '), ' e-content ')]"
88 );
89 if ($xpElems->length) {
90     $xpContext = $xpElems->item(0);
91 } else if ($doc->getElementById('content')) {
92     //if there is an element with ID "content", we'll use this
93     $xpContext = $doc->getElementById('content');
94 }
95
96 $indexDoc->url = $url;
97 $indexDoc->schemalessUrl = Helper::noSchema($url);
98 $indexDoc->type = 'html';
99 $indexDoc->subtype = '';
100 $indexDoc->mimetype = $mimetype;
101 $indexDoc->domain   = parse_url($url, PHP_URL_HOST);
102
103 //$indexDoc->source = 'FIXME';
104 //$indexDoc->sourcetitle = 'FIXME';
105
106 $indexDoc->author = new \stdClass();
107
108 $arXpElems = $dx->query('/html/head/meta[@name="author" and @content]');
109 if ($arXpElems->length) {
110     $indexDoc->author->name = trim(
111         $arXpElems->item(0)->attributes->getNamedItem('content')->textContent
112     );
113 }
114 $arXpElems = $dx->query('/html/head/link[@rel="author" and @href]');
115 if ($arXpElems->length) {
116     $indexDoc->author->url = trim(
117         $base->resolve(
118             $arXpElems->item(0)->attributes->getNamedItem('href')->textContent
119         )
120     );
121 }
122
123
124 $arXpElems = $dx->query('/html/head/title');
125 if ($arXpElems->length) {
126     $indexDoc->title = trim(
127         $arXpElems->item(0)->textContent
128     );
129 }
130
131 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
132     $indexDoc->$headlinetype = array();
133     foreach ($xpContext->getElementsByTagName($headlinetype) as $xheadline) {
134         array_push(
135             $indexDoc->$headlinetype,
136             trim($xheadline->textContent)
137         );
138     }
139 }
140
141 //FIXME: split paragraphs
142 //FIXME: insert space after br
143 $indexDoc->text = array();
144 $indexDoc->text[] = trim(
145     str_replace(
146         array("\r\n", "\n", "\r", '  '),
147         ' ',
148         $xpContext->textContent
149     )
150 );
151
152 //tags
153 $tags = array();
154 foreach ($dx->query('/html/head/meta[@name="keywords" and @content]') as $xkeywords) {
155     $keywords = $xkeywords->attributes->getNamedItem('content')->textContent;
156     foreach (explode(',', $keywords) as $keyword) {
157         $tags[trim($keyword)] = true;
158     }
159 }
160 $indexDoc->tags = array_keys($tags);
161
162 //dates
163 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.created" and @content]');
164 if ($arXpdates->length) {
165     $indexDoc->crdate = date(
166         'c',
167         strtotime(
168             $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
169         )
170     );
171 }
172 //FIXME: keep creation date from database, or use modified date if we
173 // do not have it there
174
175 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.modified" and @content]');
176 if ($arXpdates->length) {
177     $indexDoc->modate = date(
178         'c',
179         strtotime(
180             $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
181         )
182     );
183 } else {
184     $lm = $res->getHeader('last-modified');
185     if ($lm !== null) {
186         $indexDoc->modate = date('c', strtotime($lm));
187     } else {
188         //use current time since we don't have any other data
189         $indexDoc->modate = date('c');
190     }
191 }
192
193 //language
194 //there may be "en-US" and "de-DE"
195 $xlang = $doc->documentElement->attributes->getNamedItem('lang');
196 if ($xlang) {
197     $indexDoc->language = strtolower(substr($xlang->textContent, 0, 2));
198 }
199 //FIXME: fallback, autodetection
200 //FIXME: check noindex
201
202 //var_dump($indexDoc);die();
203
204 $indexDoc->status = 'indexed';
205
206 //FIXME: update index if it exists already
207 $r = new Elasticsearch_Request(
208     $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
209     \HTTP_Request2::METHOD_PUT
210 );
211 $r->setBody(json_encode($indexDoc));
212 $r->send();
213
214
215 ?>