fix indexing, boost config
[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 $dx = new \DOMXPath($doc);
62
63 //remove script tags
64 $elems = array();
65 foreach ($doc->getElementsbyTagName('script') as $elem) {
66     $elems[] = $elem;
67 }
68 foreach ($elems as $elem) {
69     $elem->parentNode->removeChild($elem);
70 }
71
72 //default content: <body>
73 $xpContext = $doc->getElementsByTagName('body')->item(0);
74
75 //use microformats content if it exists
76 $xpElems = $dx->query(
77     "//*[contains(concat(' ', normalize-space(@class), ' '), ' e-content ')]"
78 );
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');
84 }
85
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);
92
93 //$indexDoc->source = 'FIXME';
94 //$indexDoc->sourcetitle = 'FIXME';
95
96 $indexDoc->author = new \stdClass();
97
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
102     );
103 }
104 $arXpElems = $dx->query('/html/head/link[@rel="author"]');
105 if ($arXpElems->length) {
106     $indexDoc->author->url = trim(
107         $base->resolve(
108             $arXpElems->item(0)->attributes->getNamedItem('href')->textContent
109         )
110     );
111 }
112
113
114 $arXpElems = $dx->query('/html/head/title');
115 if ($arXpElems->length) {
116     $indexDoc->title = trim(
117         $arXpElems->item(0)->textContent
118     );
119 }
120
121 foreach (array('h1', 'h2', 'h3', 'h4', 'h5', 'h6') as $headlinetype) {
122     $indexDoc->$headlinetype = array();
123     foreach ($xpContext->getElementsByTagName($headlinetype) as $xheadline) {
124         array_push(
125             $indexDoc->$headlinetype,
126             trim($xheadline->textContent)
127         );
128     }
129 }
130
131 //FIXME: limit to h-entry e-content
132 //FIXME: insert space after br
133 $indexDoc->text = array();
134 $indexDoc->text[] = trim(
135     str_replace(
136         array("\r\n", "\n", "\r", '  '),
137         ' ',
138         $xpContext->textContent
139     )
140 );
141
142 //tags
143 $tags = array();
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;
148     }
149 }
150 $indexDoc->tags = array_keys($tags);
151
152 //dates
153 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.created"]');
154 if ($arXpdates->length) {
155     $indexDoc->crdate = date(
156         'c',
157         strtotime(
158             $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
159         )
160     );
161 }
162 //FIXME: keep creation date from database, or use modified date if we
163 // do not have it there
164
165 $arXpdates = $dx->query('/html/head/meta[@name="DC.date.modified"]');
166 if ($arXpdates->length) {
167     $indexDoc->modate = date(
168         'c',
169         strtotime(
170             $arXpdates->item(0)->attributes->getNamedItem('content')->textContent
171         )
172     );
173 } else {
174     $lm = $res->getHeader('last-modified');
175     if ($lm !== null) {
176         $indexDoc->modate = date('c', strtotime($lm));
177     } else {
178         //use current time since we don't have any other data
179         $indexDoc->modate = date('c');
180     }
181 }
182
183 //language
184 //there may be "en-US" and "de-DE"
185 $xlang = $doc->documentElement->attributes->getNamedItem('lang');
186 if ($xlang) {
187     $indexDoc->language = strtolower(substr($xlang->textContent, 0, 2));
188 }
189 //FIXME: fallback, autodetection
190 //FIXME: check noindex
191
192 //var_dump($indexDoc);die();
193
194 $indexDoc->status = 'indexed';
195
196 //FIXME: update index if it exists already
197 $r = new Elasticsearch_Request(
198     $GLOBALS['phinde']['elasticsearch'] . 'document/' . rawurlencode($url),
199     \HTTP_Request2::METHOD_PUT
200 );
201 $r->setBody(json_encode($indexDoc));
202 $r->send();
203
204
205 ?>