-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmeta_parser_utils.php
More file actions
376 lines (306 loc) · 13 KB
/
Copy pathmeta_parser_utils.php
File metadata and controls
376 lines (306 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
class TMetaParser {
private string $url;
function __construct(string $url) {
$this->url = $url;
}
function downloadWebpage(string $urlDownload): bool|string {
// download url can be different from website url if an archive is used to retrieve the page content
$headers = [
"Accept-Encoding:gzip,deflate",
'User-Agent:' . $_SERVER['HTTP_USER_AGENT']
];
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $urlDownload);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_ENCODING,"gzip");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION,true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,false); // Don't verify authenticity of peer
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
function downloadUsingHeadlessBrowser($urlDownload): null|string {
$headlessCommand = PHP_OS_FAMILY === 'Windows'? HEADLESS_BROWSER_COMMAND_WINDOWS : HEADLESS_BROWSER_COMMAND;
$command = $headlessCommand . ' ' . $urlDownload;
exec($command, $output, $statusCode);
if ($statusCode === 0) {
return implode("\n", $output);
} else {
throw new RuntimeException("Unable to download webpage from headless browser. Set correct command in config.php.");
}
}
function getPageMediaMetaData(string $html, string $url): array{
$metaOut = [
'json-ld' => [],
'og' => [],
'twitter' => [],
'article' => [],
'itemprop' => [],
'other' => [],
'cloudflare' => false,
];
// Convert to UTF-8 as some websites do not use it. We need it for parsing.
$html = mb_convert_encoding($html, 'UTF-8', mb_detect_encoding($html, 'UTF-8, ISO-8859-1', true));
libxml_use_internal_errors(true); // Suppress libxml warnings internally
$dom = new DOMDocument();
// Dirty way to tell DOM it really is UTF-8
$dom->loadHTML('<?xml encoding="UTF-8">' . $html);
libxml_clear_errors(); // Clear any errors collected
// See Google structured data guidelines https://developers.google.com/search/docs/guides/intro-structured-data#structured-data-guidelines
// json-ld tags. Used by Google.
// Find JSON-LD meta data
$scripts = $dom->getElementsByTagName('script');
foreach ($scripts as $script) {
if ($script->getAttribute('type') === 'application/ld+json') {
$ldJson = trim($script->nodeValue);
$this->parseLdJson($ldJson, $metaOut['json-ld']);
}
}
$metaTags = $dom->getElementsByTagName('meta');
foreach ($metaTags as $tag) {
$property = $tag->getAttribute('property');
$name = $tag->getAttribute('name');
$content = $tag->getAttribute('content');
$itemprop = $tag->getAttribute('itemprop');
// Open Graph tags (og:* in property or name attributes)
if (str_starts_with($property, 'og:') || str_starts_with($name, 'og:')) {
$key = $property ?: $name;
$metaOut['og'][$key] = $content;
}
// Twitter tags (twitter:* in property or name attributes)
if (str_starts_with($property, 'twitter:') || str_starts_with($name, 'twitter:')) {
$key = $property ?: $name;
$metaOut['twitter'][$key] = $content;
}
// Article tags (article:* in property or name attributes)
if (str_starts_with($property, 'article:') || str_starts_with($name, 'article:')) {
$key = $property ?: $name;
$metaOut['article'][$key] = $content;
}
// Itemprop tags (e.g., datePublished) in itemprop attribute
if ($itemprop) {
$metaOut['itemprop'][$itemprop] = $content;
}
// Description meta tag (description in property or name attributes)
if (($property === 'description' || $name === 'description') && !empty($content)) {
$metaOut['other']['description'] = $content;
}
}
// h1 tag
$h1Tags = $dom->getElementsByTagName('h1');
// Check if at least one <h1> tag exists
if ($h1Tags->length > 0) {
// Get the text content of the first <h1> tag
$metaOut['other']['h1'] = trim($h1Tags->item(0)->textContent);
}
// Time tag
$timeTags = $dom->getElementsByTagName('time');
// Check if at least one <time> tag exists
if ($timeTags->length > 0) {
// Get the datetime attribute of the first <time> tag
$dateText = $timeTags->item(0)->getAttribute('datetime');
if (!empty($dateText)) {
try {
$date = new DateTime($dateText);
$current_date = new DateTime();
// Check if the date is in the past
if ($date < $current_date) {
$metaOut['other']['time'] = $date->format('Y-m-d');
}
} catch (Exception $e) {
// Do nothing for ill formed time tags
}
}
}
$metaOut['other']['domain'] = $this->extractDomain($url);
$metaOut['cloudflare'] = str_contains(strtolower($html), 'cloudflare') !== false;
return $metaOut;
}
function getTagStats(array $tags): array{
$stats = [
'json_ld' => count($tags['json-ld']),
'og' => count($tags['og']),
'twitter' => count($tags['twitter']),
'article' => count($tags['article']),
'itemprop' => count($tags['itemprop']),
'other' => count($tags['other']),
];
$stats['total'] = $stats['json_ld'] + $stats['og'] + $stats['twitter'] + $stats['article'] + $stats['itemprop'];
return $stats;
}
function extractDomain(string $url): string {
// Ensure the URL includes a protocol
$url = parse_url($url, PHP_URL_SCHEME) ? $url : 'https://' . $url;
// Parse the host
$host = parse_url($url, PHP_URL_HOST);
// Split the host into parts
$hostParts = explode('.', $host);
// Return just the domain (second to last and last parts)
if (count($hostParts) >= 2) {
return $hostParts[count($hostParts) - 2] . '.' . $hostParts[count($hostParts) - 1];
}
// If the host is invalid or does not have at least two parts
return $host;
}
private const JSON_LD_TYPE = 'application/ld+json';
function parseLdJson(string $json, array &$result): void {
$trimmedJson = trim($json);
if (empty($trimmedJson)) return;
$result ??= []; // Initialize $result if not already set
$decodedJson = json_decode($trimmedJson);
if (empty($decodedJson)) return;
$this->processJsonData($decodedJson, $result);
}
private function processJsonData(mixed $data, array &$result): void {
if (is_array($data)) {
foreach ($data as $entry) {
$this->parseLdObject($result, $entry);
}
} else {
$this->parseLdObject($result, $data);
}
}
function parseLdObject(array &$result, object $data): void {
// Handle `@graph` property, which contains multiple objects
if (!empty($data->{'@graph'})) {
$this->processGraph($result, $data->{'@graph'});
return;
}
// Handle standard fields
if (!empty($data->headline)) {
$result['headline'] = $data->headline;
}
if (!empty($data->articleBody)) {
$result['articleBody'] = $data->articleBody;
}
if (!empty($data->description)) {
$result['description'] = $data->description;
}
if (!empty($data->datePublished)) {
$result['datePublished'] = $data->datePublished;
}
if (!empty($data->image)) {
$result['image'] = is_string($data->image)
? $data->image
: ($data->image->url ?? null);
}
if (!empty($data->publisher->name)) {
$result['publisher'] = $data->publisher->name;
}
}
private function processGraph(array &$result, array $graph): void {
$articleFound = false;
foreach ($graph as $item) {
if (is_object($item) && !empty($item->{'@type'})) {
// Check for NewsArticle as the primary type
if ($this->isOfType($item->{'@type'}, 'NewsArticle')) {
$this->parseLdObject($result, $item);
$articleFound = true; // Mark as article found
break; // Stop on the first valid NewsArticle
}
}
}
// Fallback to WebPage if no NewsArticle was found
if (!$articleFound) {
foreach ($graph as $item) {
if (is_object($item) && !empty($item->{'@type'}) && $this->isOfType($item->{'@type'}, 'WebPage')) {
$this->parseLdObject($result, $item);
break; // Stop on the first valid WebPage
}
}
}
}
private function isOfType(mixed $typeField, string $expectedType): bool {
// Check if @type matches a specific value
if (is_string($typeField)) {
return $typeField === $expectedType;
}
if (is_array($typeField)) {
return in_array($expectedType, $typeField, true);
}
return false;
}
private function getLongestAvailableTag(?array $tags): string {
if (! isset($tags)) return '';
$result = '';
foreach ($tags as $tag) {
// Sometimes the tag is an array. In that case we use the first element.
if (is_array($tag)) {
if (count($tag) > 0) $tag = $tag[0];
else $tag = null;
}
if (isset($tag)) {
$tag = trim($tag);
if ((! empty($tag)) && (strlen($tag) > strlen($result))) $result = $tag;
}
}
return $result;
}
function mediaTagsFromMetaData(array $metaData): array {
$ldJsonTags = $metaData['json-ld'];
$ogTags = $metaData['og'];
$twitterTags = $metaData['twitter'];
$articleTags = $metaData['article'];
$itemPropTags = $metaData['itemprop'];
// Get best tag (we assume it is the longest one) and decode HTML entities to normal text
$media = [
'url' => $this->getLongestAvailableTag([$ogTags['og:url']?? null, $this->url]),
'urlimage' => $this->getLongestAvailableTag([$ldJsonTags['image']?? null, $ogTags['og:image']?? null]),
'title' => html_entity_decode(htmlspecialchars_decode(strip_tags($this->getLongestAvailableTag([$ldJsonTags['headline']?? null, $ogTags['og:title']?? null, $twitterTags['twitter:title']?? null]))),ENT_QUOTES),
'description' => html_entity_decode(strip_tags(htmlspecialchars_decode($this->getLongestAvailableTag([$ldJsonTags['description']?? null, $ogTags['og:description']?? null, $twitterTags['twitter:description']?? null]))),ENT_QUOTES),
'article_body' => html_entity_decode(strip_tags(htmlspecialchars_decode($this->getLongestAvailableTag([$ldJsonTags['articleBody']?? null]))),ENT_QUOTES),
'sitename' => html_entity_decode(htmlspecialchars_decode($this->getLongestAvailableTag([$ldJsonTags['publisher']?? null, $ogTags['og:site_name']?? null, $metaData['other']['domain']?? null])),ENT_QUOTES),
'published_time' => $this->getLongestAvailableTag([$ldJsonTags['datePublished']?? null, $ogTags['og:article:published_time']?? null, $articleTags['article:published_time']?? null, $itemPropTags['datePublished']?? null, $articleTags['article:modified_time']?? null]),
];
// Replace http with https on image tags. Some sites still send unsecure links
$media['urlimage'] = str_replace('http://', 'https://', $media['urlimage']);
if (substr($media['urlimage'], 0, 1) === '/') {
$parse = parse_url($media['url']);
$media['urlimage'] = 'https://' . $parse['host'] . $media['urlimage'];
}
// Plan C if no other info available: Use H1 for title. Description for description
if (($media['title'] === '') && (isset($metaData['other']['h1']))) $media['title'] = $metaData['other']['h1'];
if (($media['description'] === '') && (isset($metaData['other']['description']))) $media['description'] = $metaData['other']['description'];
if (($media['published_time'] === '') && (isset($metaData['other']['time']))) $media['published_time'] = $metaData['other']['time'];
// Check if valid published_time string
if (strlen($media['published_time']) >= 1) {
try {
$dateTime = new DateTime($media['published_time']);
$media['published_time'] = $dateTime->format(DateTime::ISO8601);
} catch (Exception $e) {
if (strlen($media['published_time']) > 10) {
$media['published_time'] = substr($media['published_time'], 0, 10);
}
}
}
$media['cloudflare'] = $metaData['cloudflare'];
return $media;
}
}
function parseMetaDataFromUrl(string $url): array {
$parser = new TMetaParser($url);
$urlDownload = $url;
$pageHtml = $parser->downloadWebpage($urlDownload);
if ($pageHtml === false) {
// Use a headless browser if CURL fails. This happens if CURL is blocked
$pageHtml = $parser->downloadUsingHeadlessBrowser($urlDownload);
$metaData = $parser->getPageMediaMetaData($pageHtml, $url);
$tagCount = $parser->getTagStats($metaData);
} else {
$metaData = $parser->getPageMediaMetaData($pageHtml, $url);
$tagCount = $parser->getTagStats($metaData);
// if no tags found, try the headless browser
if ($tagCount['total'] <= 0) {
$pageHtml = $parser->downloadUsingHeadlessBrowser($urlDownload);
$metaData = $parser->getPageMediaMetaData($pageHtml, $url);
$tagCount = $parser->getTagStats($metaData);
}
}
$media = $parser->mediaTagsFromMetaData($metaData);
return [
'media' => $media,
'tagCount' => $tagCount,
];
}