dd32dea86148e5007a8aa133a8e2a21bfa579212
[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 $es = new Elasticsearch($GLOBALS['phinde']['elasticsearch']);
18
19 $url = $argv[1];
20 $existingDoc = $es->get($url);
21 if ($existingDoc && $existingDoc->status == 'indexed') {
22     echo "URL already indexed: $url\n";
23     exit(0);
24 }
25 //FIXME: sourcetitle, sourcelink
26
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);
32 $res = $req->send();
33 //FIXME: try-catch
34
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
39     exit(3);
40 }
41
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
46     exit(4);
47 }
48
49
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);
54
55 $indexDoc = new \stdClass();
56
57 //FIXME: MIME type switch
58 $doc = new \DOMDocument();
59 //@ to hide parse warning messages in invalid html
60 @$doc->loadHTML($res->getBody());
61 $sx = simplexml_import_dom($doc);
62
63 $indexDoc->url = $url;
64 $indexDoc->schemalessUrl = Helper::noSchema($url);
65 $indexDoc->type = 'html';
66 $indexDoc->subtype = '';
67 $indexDoc->mimetype = $mimetype;
68 $indexDoc->domain   = parse_url($url, PHP_URL_HOST);
69
70 //$indexDoc->source = 'FIXME';
71 //$indexDoc->sourcetitle = 'FIXME';
72
73 $indexDoc->author = new \stdClass();
74
75 $arSxElems = $sx->xpath('/html/head/meta[@name="author"]');
76 if (count($arSxElems)) {
77     $indexDoc->author->name = trim($arSxElems[0]['content']);
78 }
79 $arSxElems = $sx->xpath('/html/head/link[@rel="author"]');
80 if (count($arSxElems)) {
81     $indexDoc->author->url = (string) $base->resolve($arSxElems[0]['href']);
82 }
83
84 $indexDoc->title = (string) $sx->head->title;
85 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
86     $indexDoc->$headlinetype = array();
87     //FIXME: limit to h-entry children
88     foreach ($sx->xpath('//' . $headlinetype) as $xheadline) {
89         array_push(
90             $indexDoc->$headlinetype,
91             trim(dom_import_simplexml($xheadline)->textContent)
92         );
93     }
94 }
95
96 //FIXME: limit to h-entry e-content
97 //FIXME: insert space after br
98 //FIXME: remove javascript
99 $indexDoc->text = array();
100 foreach ($doc->getElementsByTagName('body') as $body) {
101     $indexDoc->text[] = trim(
102         str_replace(
103             array("\r\n", "\n", "\r", '  '),
104             ' ',
105             $body->textContent
106         )
107     );
108 }
109
110 //tags
111 $tags = array();
112 foreach ($sx->xpath('/html/head/meta[@name="keywords"]') as $xkeywords) {
113     foreach (explode(',', $xkeywords['content']) as $keyword) {
114         $tags[trim($keyword)] = true;
115     }
116 }
117 $indexDoc->tags = array_keys($tags);
118
119 //dates
120 $arSxdates = $sx->xpath('/html/head/meta[@name="DC.date.created"]');
121 if (count($arSxdates)) {
122     $indexDoc->crdate = date('c', strtotime((string) $arSxdates[0]['content']));
123 }
124 //FIXME: keep creation date from database, or use modified date if we
125 // do not have it there
126
127 $arSxdates = $sx->xpath('/html/head/meta[@name="DC.date.modified"]');
128 if (count($arSxdates)) {
129     $indexDoc->modate = date('c', strtotime((string) $arSxdates[0]['content']));
130 } else {
131     $lm = $res->getHeader('last-modified');
132     if ($lm !== null) {
133         $indexDoc->modate = date('c', strtotime($lm));
134     } else {
135         //use current time since we don't have any other data
136         $indexDoc->modate = date('c');
137     }
138 }
139
140 //language
141 //there may be "en-US" and "de-DE"
142 $indexDoc->language = strtolower(substr((string) $sx['lang'], 0, 2));
143 //FIXME: fallback, autodetection
144 //FIXME: check noindex
145
146
147 //var_dump($indexDoc);
148
149 $indexDoc->status = 'indexed';
150
151 //FIXME: update index if it exists already
152 $r = new Elasticsearch_Request(
153     $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
154     \HTTP_Request2::METHOD_PUT
155 );
156 $r->setBody(json_encode($indexDoc));
157 $r->send();
158
159
160 ?>