This repository was archived by the owner on Jan 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathWikipedia.php
More file actions
77 lines (65 loc) · 2.26 KB
/
Wikipedia.php
File metadata and controls
77 lines (65 loc) · 2.26 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
<?php
class Wikipedia
{
const URL = 'https://en.wikipedia.org/w/api.php?action=query&titles=Microsoft_Edge&prop=revisions&rvprop=content&rvsection=4&format=xml';
private static $errors = array(
'fetch_error' => 'Unable to fetch content',
'parse_error' => 'Unable to parse content',
);
public static function fetch()
{
$content = file_get_contents(self::URL);
if (!$content) {
throw new Exception(self::$errors['fetch_error']);
}
$content = explode('===Release history===', $content);
if (!isset($content[1])) {
throw new Exception(self::$errors['parse_error']);
}
$table = explode('|-', $content[1]);
if (!isset($table[1])) {
throw new Exception(self::$errors['parse_error']);
}
$table = array_slice($table, 1);
$versions = array_map(array('Wikipedia', 'extractVersion'), $table);
self::writeEdgeVersions($versions);
}
private static function extractVersion($content)
{
$lines = array_slice(array_filter(
explode(PHP_EOL, $content),
function ($val) {
return trim($val) && strpos($val, '|') === 0;
}
), 0, 2);
preg_match("/{[^}{]*Version[^}{]*\| ?([\d\.]+)}/", $lines[0], $edgeVersion);
preg_match("/\| *(\d*\.\d*)/", $lines[1], $edgeHtmlVersion);
if (!isset($edgeVersion[1])) {
throw new Exception(self::$errors['parse_error']);
}
if (!isset($edgeHtmlVersion[1])) {
throw new Exception(self::$errors['parse_error']);
}
return array($edgeHtmlVersion[1], $edgeVersion[1]);
}
private static function writeEdgeVersions($versions)
{
$file = __DIR__ . '/../../src/edgeVersionMap.php';
$currentVersions = require $file;
foreach ($versions as $version) {
$currentVersions[$version[0]] = $version[1];
}
ksort($currentVersions);
$content = '';
foreach ($currentVersions as $edgeHtml => $edge) {
$content .= " '{$edgeHtml}' => '{$edge}'," . PHP_EOL;
}
$data = <<<PHP
<?php
return array(
%s
);
PHP;
file_put_contents($file, sprintf($data, trim($content)));
}
}