forked from brentwpeterson/magento-patches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
universal-patch-downloader.php
159 lines (144 loc) · 4.11 KB
/
universal-patch-downloader.php
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
<?php
## from Fabian Blechschmidt
#
class PatchDownloader
{
/**
* * @var string
* */
private $infoUrl = "https://MAGEID:TOKEN@www.magentocommerce.com/products/downloads/info/filter/version/";
/**
* * @var string
* */
private $downloadUrl = "https://MAGEID:TOKEN@www.magentocommerce.com/products/downloads/file/";
/**
* * Cache for patches
* *
* * @var string[]
* */
private $patchCache = [];
/**
* * @var string[]
* */
private $versions = array(
'1.2.0.1',
'1.2.0.2',
'1.2.0.3',
'1.2.1.1',
'1.2.1.2',
'1.3.1.1',
'1.3.2.1',
'1.3.2.2',
'1.3.2.3',
'1.3.2.4',
'1.3.3.0',
'1.4.0.0',
'1.4.0.1',
'1.4.1.0',
'1.4.1.1',
'1.4.2.0',
'1.5.0.1',
'1.5.1.0',
'1.6.0.0',
'1.6.1.0',
'1.6.2.0',
'1.7.0.0',
'1.7.0.1',
'1.7.0.2',
'1.8.0.0',
'1.8.1.0',
'1.9.0.0',
'1.9.0.1',
'1.9.1.0',
'1.9.1.1',
'1.9.2.0',
'1.9.2.1',
'1.9.2.2',
'1.9.1.0',
'1.9.1.0',
'1.9.2.2',
);
public function __construct($mageId, $token)
{
$replace = [
'MAGEID' => $mageId,
'TOKEN' => $token
];
$this->downloadUrl = strtr($this->downloadUrl, $replace);
$this->infoUrl = strtr($this->infoUrl, $replace);
}
public function downloadPatches()
{
foreach ($this->versions as $version) {
$this->createDirectoryForVersion($version);
$this->downloadPatchForVersion($version);
}
}
private function downloadPatchForVersion($version)
{
$response = file_get_contents($this->infoUrl . $version);
if (strpos($response, 'No results found.') !== false ||
strpos($response, 'Community Edition - Patch') === false
) {
$this->cliNotice('No patches found for ' . $version);
return;
}
strtok($response, "\n");
while ($line = strtok("\n")) {
if (!preg_match("#(SUPEE-.*?) *?Community Edition - Patch *?$version *?(.*)#", $line, $matches)) {
continue;
}
$matches = array_map('trim', $matches);
list(, $name, $file) = $matches;
$name = str_replace(' ', '_', $name);
$filenameOnDisk = "$version/$name.sh";
if (file_exists($filenameOnDisk)) {
continue;
}
$this->downloadPatchIfNeeded($file);
$this->savePatchToVersionDirectory($version, $filenameOnDisk, $file);
}
}
private function cliError($msg)
{
echo "\033[0;31m" . $msg . "\033[0m" . PHP_EOL;
}
private function cliNotice($msg)
{
echo "\033[0;33m" . $msg . "\033[0m" . PHP_EOL;
}
private function cliSuccess($msg)
{
echo "\033[0;32m" . $msg . "\033[0m" . PHP_EOL;
}
private function createDirectoryForVersion($version)
{
if (!is_dir($version)) {
$directory = __DIR__ . '/' . $version;
mkdir($directory);
$this->cliSuccess('Created directory ' . $version);
}
}
/**
* * @param $file
* */
private function downloadPatchIfNeeded($file)
{
if (!isset($this->patchCache[$file])) {
$this->patchCache[$file] = file_get_contents($this->downloadUrl . $file);
$this->cliSuccess('Downloaded and cached patch: ' . $file);
}
}
/**
* * @param $version
* * @param $filenameOnDisk
* * @param $file
* */
private function savePatchToVersionDirectory($version, $filenameOnDisk, $file)
{
file_put_contents($filenameOnDisk, $this->patchCache[$file]);
$this->cliSuccess(sprintf('Saved %s for version %s', $file, $version));
}
}
$downloader = new PatchDownloader($mageid, $downloadToken);
$downloader->downloadPatches();