remove anchor from source URLs
[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 $url = $argv[1];
28
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);
34 //FIXME: size limit
35
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";
43         exit(0);
44     }
45
46     $req->setHeader('If-Modified-Since: ' . date('r', $nMoDate));
47 }
48 //FIXME: sourcetitle, sourcelink
49
50 $res = $req->send();
51 //FIXME: try-catch
52
53 if ($res->getStatus() === 304) {
54     //not modified since last time
55     //FIXME: store "last try" time
56     exit(0);
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
61     exit(3);
62 }
63
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
68     exit(4);
69 }
70
71
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);
77
78 $indexDoc = new \stdClass();
79
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);
85
86 $xbase = $dx->evaluate('/html/head/base[@href]')->item(0);
87 if ($xbase) {
88     $base = $base->resolve(
89         $xbase->attributes->getNamedItem('href')->textContent
90     );
91 }
92
93 $meta = $dx->evaluate('/html/head/meta[@name="robots" and @content]')
94     ->item(0);
95 if ($meta) {
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";
100             exit(0);
101         }
102     }
103 }
104
105 //remove script tags
106 removeTags($doc, 'script');
107 removeTags($doc, 'style');
108 removeTags($doc, 'nav');
109
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/
114
115 //use microformats content if it exists
116 $xpElems = $dx->query(
117     "//*[contains(concat(' ', normalize-space(@class), ' '), ' e-content ')]"
118 );
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');
124 }
125
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);
132
133 //$indexDoc->source = 'FIXME';
134 //$indexDoc->sourcetitle = 'FIXME';
135
136 $indexDoc->author = new \stdClass();
137
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
142     );
143 }
144 $arXpElems = $dx->query('/html/head/link[@rel="author" and @href]');
145 if ($arXpElems->length) {
146     $indexDoc->author->url = trim(
147         $base->resolve(
148             $arXpElems->item(0)->attributes->getNamedItem('href')->textContent
149         )
150     );
151 }
152
153
154 $arXpElems = $dx->query('/html/head/title');
155 if ($arXpElems->length) {
156     $indexDoc->title = trim(
157         $arXpElems->item(0)->textContent
158     );
159 }
160
161 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
162     $indexDoc->$headlinetype = array();
163     foreach ($xpContext->getElementsByTagName($headlinetype) as $xheadline) {
164         array_push(
165             $indexDoc->$headlinetype,
166             trim($xheadline->textContent)
167         );
168     }
169 }
170
171 //FIXME: split paragraphs
172 //FIXME: insert space after br
173 $indexDoc->text = array();
174 $indexDoc->text[] = trim(
175     str_replace(
176         array("\r\n", "\n", "\r", '  '),
177         ' ',
178         $xpContext->textContent
179     )
180 );
181
182 //tags
183 $tags = array();
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;
188     }
189 }
190 $indexDoc->tags = array_keys($tags);
191
192 //dates
193 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.created" and @content]');
194 if ($arXpdates->length) {
195     $indexDoc->crdate = date(
196         'c',
197         strtotime(
198             $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
199         )
200     );
201 }
202 //FIXME: keep creation date from database, or use modified date if we
203 // do not have it there
204
205 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.modified" and @content]');
206 if ($arXpdates->length) {
207     $indexDoc->modate = date(
208         'c',
209         strtotime(
210             $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
211         )
212     );
213 } else {
214     $lm = $res->getHeader('last-modified');
215     if ($lm !== null) {
216         $indexDoc->modate = date('c', strtotime($lm));
217     } else {
218         //use current time since we don't have any other data
219         $indexDoc->modate = date('c');
220     }
221 }
222
223 //language
224 //there may be "en-US" and "de-DE"
225 $xlang = $doc->documentElement->attributes->getNamedItem('lang');
226 if ($xlang) {
227     $indexDoc->language = strtolower(substr($xlang->textContent, 0, 2));
228 }
229 //FIXME: fallback, autodetection
230 //FIXME: check noindex
231
232 //var_dump($indexDoc);die();
233
234 $indexDoc->status = 'indexed';
235
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
240 );
241 $r->setBody(json_encode($indexDoc));
242 $r->send();
243
244
245 ?>