|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class Wikipedia |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
const URL = 'https://en.wikipedia.org/w/api.php?action=query&titles=Microsoft_Edge&prop=revisions&rvprop=content&rvsection=4&format=xml'; |
|
6
|
|
|
|
|
7
|
|
|
private static $errors = array( |
|
8
|
|
|
'fetch_error' => 'Unable to fetch content', |
|
9
|
|
|
'parse_error' => 'Unable to parse content', |
|
10
|
|
|
); |
|
11
|
|
|
|
|
12
|
|
|
public static function fetch() |
|
13
|
|
|
{ |
|
14
|
|
|
$content = file_get_contents(self::URL); |
|
15
|
|
|
if (!$content) { |
|
16
|
|
|
throw new Exception(self::$errors['fetch_error']); |
|
17
|
|
|
} |
|
18
|
|
|
$content = explode('===Release history===', $content); |
|
19
|
|
|
if (!isset($content[1])) { |
|
20
|
|
|
throw new Exception(self::$errors['parse_error']); |
|
21
|
|
|
} |
|
22
|
|
|
$table = explode('|-', $content[1]); |
|
23
|
|
|
if (!isset($table[1])) { |
|
24
|
|
|
throw new Exception(self::$errors['parse_error']); |
|
25
|
|
|
} |
|
26
|
|
|
$table = array_slice($table, 1); |
|
27
|
|
|
$versions = array_map(array('Wikipedia', 'extractVersion'), $table); |
|
28
|
|
|
self::writeEdgeVersions($versions); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
private static function extractVersion($content) |
|
32
|
|
|
{ |
|
33
|
|
|
$lines = array_slice(array_filter( |
|
34
|
|
|
explode(PHP_EOL, $content), |
|
35
|
|
|
function ($val) { |
|
36
|
|
|
return trim($val) && strpos($val, '|') === 0; |
|
37
|
|
|
} |
|
38
|
|
|
), 0, 2); |
|
39
|
|
|
|
|
40
|
|
|
preg_match("/{[^}{]*Version[^}{]*\| ?([\d\.]+)}/", $lines[0], $edgeVersion); |
|
41
|
|
|
preg_match("/\| *(\d*\.\d*)/", $lines[1], $edgeHtmlVersion); |
|
42
|
|
|
|
|
43
|
|
|
if (!isset($edgeVersion[1])) { |
|
44
|
|
|
throw new Exception(self::$errors['parse_error']); |
|
45
|
|
|
} |
|
46
|
|
|
if (!isset($edgeHtmlVersion[1])) { |
|
47
|
|
|
throw new Exception(self::$errors['parse_error']); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return array($edgeHtmlVersion[1], $edgeVersion[1]); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
View Code Duplication |
private static function writeEdgeVersions($versions) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
|
|
$file = __DIR__ . '/../../src/edgeVersionMap.php'; |
|
56
|
|
|
$currentVersions = require $file; |
|
57
|
|
|
|
|
58
|
|
|
foreach ($versions as $version) { |
|
59
|
|
|
$currentVersions[$version[0]] = $version[1]; |
|
60
|
|
|
} |
|
61
|
|
|
ksort($currentVersions); |
|
62
|
|
|
|
|
63
|
|
|
$content = ''; |
|
64
|
|
|
foreach ($currentVersions as $edgeHtml => $edge) { |
|
65
|
|
|
$content .= " '{$edgeHtml}' => '{$edge}'," . PHP_EOL; |
|
66
|
|
|
} |
|
67
|
|
|
$data = <<<PHP |
|
68
|
|
|
<?php |
|
69
|
|
|
|
|
70
|
|
|
return array( |
|
71
|
|
|
%s |
|
72
|
|
|
); |
|
73
|
|
|
|
|
74
|
|
|
PHP; |
|
75
|
|
|
file_put_contents($file, sprintf($data, trim($content))); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.