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 pathChangeWindows.php
More file actions
156 lines (139 loc) · 5.47 KB
/
ChangeWindows.php
File metadata and controls
156 lines (139 loc) · 5.47 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
<?php
/**
* Fetch versions from changewindows.org
* © 2014-present CHANGEWINDOWS
*
* Disclaimer
* All trademarks mentioned are the property of their respective owners. The content generated by this script
* comes from changewindows.org and may not be accurate.
*/
class ChangeWindows
{
private static $errors = array(
'could_not_fetch_version' => 'Could not fetch current version from ChangeWindows',
'invalid_version' => 'Windows version is invalid',
'could_not_fetch_page' => 'Could not fetch page from ChangeWindows'
);
public static function fetchVersions()
{
$windowsVersions = json_decode(file_get_contents(__DIR__ . '/windowsVersions.json'), true);
if (!count($windowsVersions)) {
$currentVersion = explode('.', self::fetchCurrentVersion(), 2);
if (!isset($currentVersion[0])) {
throw new Exception(self::$errors['invalid_version']);
}
$windowsVersions = self::fetchVersion($windowsVersions, $currentVersion[0]);
self::writeWindowsVersions($windowsVersions);
} else {
reset($windowsVersions);
$firstVersion = key($windowsVersions);
end($windowsVersions);
$lastVersion = key($windowsVersions);
try {
$result = self::fetchVersion($windowsVersions, $firstVersion);
$windowsVersions = $result;
} catch (Exception $e) {
}
$windowsVersions = self::fetchVersion($windowsVersions, $lastVersion);
self::writeWindowsVersions($windowsVersions);
}
}
private static function fetchVersion($windowsVersions, $version)
{
$siblingVersions = self::fetchPage($version);
$windowsVersions[$version] = true;
self::writeWindowsVersions($windowsVersions);
if (isset($siblingVersions[0]) && !isset($windowsVersions[$siblingVersions[0]])) {
$windowsVersions = self::fetchVersion($windowsVersions, $siblingVersions[0]);
}
if (isset($siblingVersions[1]) && !isset($windowsVersions[$siblingVersions[1]])) {
$windowsVersions = self::fetchVersion($windowsVersions, $siblingVersions[1]);
}
return $windowsVersions;
}
private static function writeWindowsVersions($windowsVersions)
{
ksort($windowsVersions);
file_put_contents(__DIR__ . '/windowsVersions.json', json_encode($windowsVersions, JSON_PRETTY_PRINT));
}
private static function fetchCurrentVersion()
{
$content = file_get_contents('https://changewindows.org/filter/pc/all/current/month/true');
if (!$content) {
throw new Exception(self::$errors['could_not_fetch_version']);
}
$content = explode('class="timeline"', $content, 2);
if (!isset($content[1])) {
throw new Exception(self::$errors['could_not_fetch_version']);
}
$content = explode('build"', $content[1], 2);
if (!isset($content[1])) {
throw new Exception(self::$errors['could_not_fetch_version']);
}
preg_match("/(\d*\.\d*)<\/div>/", $content[1], $matches);
if (!isset($matches[1])) {
throw new Exception(self::$errors['could_not_fetch_version']);
}
return $matches[1];
}
private static function fetchPage($version)
{
$url = "https://changewindows.org/build/{$version}/pc";
$content = file_get_contents($url);
$siblingVersions = self::fetchSiblingVersions($content);
self::fetchEdgeVersion($content);
return $siblingVersions;
}
private static function fetchEdgeVersion($content)
{
preg_match('/<h4[^>]*> *Edge ([\d\.]*) *<\/h4>/', $content, $edge);
preg_match('/<h4[^>]*>EdgeHTML ([\d\.]*)<\/h4>/', $content, $edgeHtml);
if (isset($edge[1]) && isset($edgeHtml[1])) {
self::writeEdgeVersion($edgeHtml[1], $edge[1]);
}
return null;
}
private static function writeEdgeVersion($edgeHtml, $edge)
{
$file = __DIR__ . '/../../src/edgeVersionMap.php';
$currentVersions = require $file;
if (!isset($currentVersions[$edgeHtml])) {
$currentVersions[$edgeHtml] = $edge;
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)));
}
}
private static function fetchSiblingVersions($content)
{
if (!$content) {
throw new Exception(self::$errors['could_not_fetch_page']);
}
$content = explode('build-sidebar', $content, 2);
if (!isset($content[1])) {
throw new Exception(self::$errors['could_not_fetch_page']);
}
$content = explode('fa-angle-left', $content[1]);
if (!isset($content[1])) {
throw new Exception(self::$errors['could_not_fetch_page']);
}
$content = explode('fa-angle-right', $content[1]);
if (!isset($content[0])) {
throw new Exception(self::$errors['could_not_fetch_page']);
}
preg_match_all("/> *(\d+) *</", $content[0], $matches);
if (!isset($matches[1])) {
throw new Exception(self::$errors['could_not_fetch_page']);
}
return $matches[1];
}
}