|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Composer\Package\Package; |
|
4
|
|
|
use Composer\Package\PackageInterface; |
|
5
|
|
|
use SilverStripe\ModuleRatings\CheckSuite; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Downloads an add-on and builds more details information about it. |
|
9
|
|
|
*/ |
|
10
|
|
|
class AddonBuilder |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
const ADDONS_DIR = 'addon-downloads'; |
|
14
|
|
|
|
|
15
|
|
|
const SCREENSHOTS_DIR = 'screenshots'; |
|
16
|
|
|
|
|
17
|
|
|
private $packagist; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var CheckSuite |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $checkSuite; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(PackagistService $packagist, CheckSuite $checkSuite) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->packagist = $packagist; |
|
27
|
|
|
$this->setCheckSuite($checkSuite); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function build(Addon $addon) |
|
31
|
|
|
{ |
|
32
|
|
|
putenv("GIT_SSH_COMMAND=\"ssh -o StrictHostKeyChecking=no\""); |
|
33
|
|
|
|
|
34
|
|
|
$composer = $this->packagist->getComposer(); |
|
35
|
|
|
$downloader = $composer->getDownloadManager(); |
|
|
|
|
|
|
36
|
|
|
$packageVersions = $this->packagist->getPackageVersions($addon->Name); |
|
|
|
|
|
|
37
|
|
|
$time = time(); |
|
38
|
|
|
|
|
39
|
|
|
if (!$packageVersions) { |
|
|
|
|
|
|
40
|
|
|
throw new Exception('Could not find corresponding Packagist versions'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Get the latest local and packagist version pair. |
|
44
|
|
|
$version = $addon->Versions()->filter('Development', true)->first(); |
|
|
|
|
|
|
45
|
|
|
if (!$version) { |
|
46
|
|
|
echo "No versions found for " . $addon->Name . "; deleting orphan record.\n"; |
|
|
|
|
|
|
47
|
|
|
$addon->delete(); |
|
48
|
|
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
foreach ($packageVersions as $packageVersion) { |
|
52
|
|
|
if ($packageVersion->getVersionNormalized() != $version->Version) { |
|
|
|
|
|
|
53
|
|
|
continue; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
if (defined('SS_ADDONS_DOWNLOAD_PATH')) { |
|
57
|
|
|
$path = SS_ADDONS_DOWNLOAD_PATH . '/' . $addon->Name; |
|
|
|
|
|
|
58
|
|
|
} else { |
|
59
|
|
|
$path = implode('/', array( |
|
60
|
|
|
TEMP_FOLDER, self::ADDONS_DIR, $addon->Name |
|
|
|
|
|
|
61
|
|
|
)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Convert PackagistAPI result into class compatible with Composer logic |
|
65
|
|
|
$package = new Package( |
|
66
|
|
|
$addon->Name, |
|
|
|
|
|
|
67
|
|
|
$packageVersion->getVersionNormalized(), |
|
|
|
|
|
|
68
|
|
|
$packageVersion->getVersion() |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
if ($extra = $packageVersion->getExtra()) { |
|
72
|
|
|
$package->setExtra((array) $extra); |
|
73
|
|
|
} |
|
74
|
|
|
if ($source = $packageVersion->getSource()) { |
|
|
|
|
|
|
75
|
|
|
$package->setSourceUrl($source->getUrl()); |
|
76
|
|
|
$package->setSourceType($source->getType()); |
|
77
|
|
|
$package->setSourceReference($source->getReference()); |
|
78
|
|
|
} |
|
79
|
|
|
if ($dist = $packageVersion->getDist()) { |
|
|
|
|
|
|
80
|
|
|
$package->setDistUrl($dist->getUrl()); |
|
81
|
|
|
$package->setDistType($dist->getType()); |
|
82
|
|
|
$package->setDistReference($dist->getReference()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
try { |
|
86
|
|
|
$this->download($package, $path); |
|
87
|
|
|
|
|
88
|
|
|
// If there's an error, mark this version as bad |
|
89
|
|
|
} catch (RuntimeException $e) { |
|
90
|
|
|
echo "Add-on " . $addon->Name . " couldn't be downloaded; deleting from database.\n"; |
|
|
|
|
|
|
91
|
|
|
echo "Error message: " . $e->getMessage() . "\n"; |
|
92
|
|
|
$addon->delete(); |
|
93
|
|
|
return; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->buildReadme($addon, $path); |
|
97
|
|
|
$this->buildScreenshots($addon, $package, $path); |
|
98
|
|
|
$this->rateModule($addon, $path); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$addon->LastBuilt = $time; |
|
|
|
|
|
|
102
|
|
|
$addon->write(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
protected function download(PackageInterface $package, $path) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->packagist |
|
108
|
|
|
->getComposer() |
|
109
|
|
|
->getDownloadManager() |
|
110
|
|
|
->download($package, $path); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Parses a readme file from markdown to HTML, then purifies it |
|
115
|
|
|
* @param Addon $addon |
|
116
|
|
|
* @param string $path |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function buildReadme(Addon $addon, $path) |
|
119
|
|
|
{ |
|
120
|
|
|
$candidates = array( |
|
121
|
|
|
'README.md', |
|
122
|
|
|
'README.markdown', |
|
123
|
|
|
'README.mdown', |
|
124
|
|
|
'docs/en/index.md' |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
foreach ($candidates as $candidate) { |
|
128
|
|
|
$lower = strtolower($candidate); |
|
129
|
|
|
$paths = array("$path/$candidate", "$path/$lower"); |
|
130
|
|
|
|
|
131
|
|
|
foreach ($paths as $path) { |
|
132
|
|
|
if (!file_exists($path)) { |
|
133
|
|
|
return; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$parser = GitHubMarkdownService::create(); |
|
137
|
|
|
if ($context = $this->getGitHubContext($addon)) { |
|
138
|
|
|
$parser->setContext($context); |
|
139
|
|
|
} |
|
140
|
|
|
$readme = $parser->toHtml(file_get_contents($path)); |
|
141
|
|
|
|
|
142
|
|
|
if (empty($readme)) { |
|
143
|
|
|
return; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$readme = $parser->toHtml(file_get_contents($path)); |
|
147
|
|
|
|
|
148
|
|
|
$purifier = new HTMLPurifier(); |
|
149
|
|
|
$readme = $purifier->purify($readme, array( |
|
150
|
|
|
'Cache.SerializerPath' => TEMP_FOLDER |
|
151
|
|
|
)); |
|
152
|
|
|
|
|
153
|
|
|
$readme = $this->replaceRelativeLinks($addon, $readme); |
|
154
|
|
|
$addon->Readme = $readme; |
|
|
|
|
|
|
155
|
|
|
return; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Determine if the repository is from GitHub, and if so then return the "context" (vendor/module) from the path |
|
162
|
|
|
* |
|
163
|
|
|
* @param Addon $addon |
|
164
|
|
|
* @return string|false |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getGitHubContext(Addon $addon) |
|
167
|
|
|
{ |
|
168
|
|
|
$repository = $addon->Repository; |
|
|
|
|
|
|
169
|
|
|
if (stripos($repository, '://github.com/') === false) { |
|
170
|
|
|
return false; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
preg_match('/^http(?:s?):\/\/github\.com\/(?<module>.*)(\.git)?$/U', $repository, $matches); |
|
174
|
|
|
|
|
175
|
|
|
if (isset($matches['module'])) { |
|
176
|
|
|
return $matches['module']; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return false; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Given an addon and a parsed HTML readme string, find and replace relative links with absolute |
|
184
|
|
|
* repository path links. This method applies to GitHub repositories only. |
|
185
|
|
|
* |
|
186
|
|
|
* @param Addon $addon |
|
187
|
|
|
* @param string $readme |
|
188
|
|
|
* @return string |
|
189
|
|
|
*/ |
|
190
|
|
|
public function replaceRelativeLinks(Addon $addon, $readme) |
|
191
|
|
|
{ |
|
192
|
|
|
if (!$this->hasGitHubRepository($addon)) { |
|
193
|
|
|
return $readme; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$dom = new DOMDocument; |
|
197
|
|
|
// LibXML needs a wrapper element... |
|
198
|
|
|
$dom->loadHTML('<div>' . $readme . '</div>', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); |
|
199
|
|
|
$xpath = new DOMXPath($dom); |
|
200
|
|
|
|
|
201
|
|
|
// Select all anchors and images in the readme document |
|
202
|
|
|
$query = $xpath->query('//*[self::a or self::img]'); |
|
203
|
|
|
|
|
204
|
|
|
foreach ($query as $element) { /** @var DOMElement $element */ |
|
205
|
|
|
$attribute = ($element->nodeName === 'a') ? 'href' : 'src'; |
|
206
|
|
|
$path = $element->getAttribute($attribute); |
|
207
|
|
|
if (!$this->isRelativeUri($path)) { |
|
208
|
|
|
continue; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
// See GitHub readmes for example |
|
212
|
|
|
$folder = ($attribute === 'href') ? 'blob' : 'raw'; |
|
213
|
|
|
$defaultBranch = 'master'; // Is this safe to assume? |
|
214
|
|
|
|
|
215
|
|
|
$element->setAttribute( |
|
216
|
|
|
$attribute, |
|
217
|
|
|
implode('/', array($addon->Repository, $folder, $defaultBranch, $path)) |
|
|
|
|
|
|
218
|
|
|
); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
// Return the inner HTML of the wrapper div... Reference: stackoverflow.com/a/39193507/2812842 |
|
222
|
|
|
$node = $dom->getElementsByTagName('div')->item(0); |
|
223
|
|
|
return implode(array_map([$node->ownerDocument, 'saveHTML'], iterator_to_array($node->childNodes))); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Decide whether a URI path is relative or not. The regex pattern matches prefixes that start with |
|
228
|
|
|
* a protocol, a slash or a hash. If they don't start with those things, then they are deemed to be |
|
229
|
|
|
* relative paths. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $path |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public function isRelativeUri($path) |
|
235
|
|
|
{ |
|
236
|
|
|
return !preg_match('/(^(?:https?:\/\/|\/|#).*$)/', $path); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Determine whether an addon is hosted on GitHub |
|
241
|
|
|
* |
|
242
|
|
|
* @param Addon $addon |
|
243
|
|
|
* @return bool |
|
244
|
|
|
*/ |
|
245
|
|
|
public function hasGitHubRepository(Addon $addon) |
|
246
|
|
|
{ |
|
247
|
|
|
return (strpos($addon->Repository, 'github.com') !== false); |
|
|
|
|
|
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
private function buildScreenshots(Addon $addon, PackageInterface $package, $path) |
|
251
|
|
|
{ |
|
252
|
|
|
$extra = $package->getExtra(); |
|
253
|
|
|
$screenshots = array(); |
|
254
|
|
|
$target = self::SCREENSHOTS_DIR . '/' . $addon->Name; |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
if (isset($extra['screenshots'])) { |
|
257
|
|
|
$screenshots = (array) $extra['screenshots']; |
|
258
|
|
|
} elseif (isset($extra['screenshot'])) { |
|
259
|
|
|
$screenshots = (array) $extra['screenshot']; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
// Delete existing screenshots. |
|
263
|
|
|
foreach ($addon->Screenshots() as $screenshot) { |
|
|
|
|
|
|
264
|
|
|
$screenshot->delete(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$addon->Screenshots()->removeAll(); |
|
|
|
|
|
|
268
|
|
|
|
|
269
|
|
|
foreach ($screenshots as $screenshot) { |
|
270
|
|
|
if (!is_string($screenshot)) { |
|
271
|
|
|
continue; |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
$scheme = parse_url($screenshot, PHP_URL_SCHEME); |
|
275
|
|
|
|
|
276
|
|
|
// Handle absolute image URLs. |
|
277
|
|
|
if ($scheme == 'http' || $scheme == 'https') { |
|
278
|
|
|
$temp = TEMP_FOLDER . '/' . md5($screenshot); |
|
279
|
|
|
|
|
280
|
|
|
if (!copy($screenshot, $temp)) { |
|
281
|
|
|
continue; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
$data = array( |
|
285
|
|
|
'name' => basename($screenshot), |
|
286
|
|
|
'size' => filesize($temp), |
|
287
|
|
|
'tmp_name' => $temp, |
|
288
|
|
|
'error' => 0 |
|
289
|
|
|
); |
|
290
|
|
|
} // Handle images that are included in the repository. |
|
291
|
|
|
else { |
|
292
|
|
|
$source = $path . '/' . ltrim($screenshot, '/'); |
|
293
|
|
|
|
|
294
|
|
|
// Prevent directory traversal. |
|
295
|
|
|
if ($source != realpath($source)) { |
|
296
|
|
|
continue; |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
if (!file_exists($source)) { |
|
300
|
|
|
continue; |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
$data = array( |
|
304
|
|
|
'name' => basename($source), |
|
305
|
|
|
'size' => filesize($source), |
|
306
|
|
|
'tmp_name' => $source, |
|
307
|
|
|
'error' => 0 |
|
308
|
|
|
); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
$upload = new Upload(); |
|
312
|
|
|
$upload->setValidator(new AddonBuilderScreenshotValidator()); |
|
313
|
|
|
$upload->load($data, $target); |
|
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
if ($file = $upload->getFile()) { |
|
316
|
|
|
$addon->Screenshots()->add($file); |
|
|
|
|
|
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* Use the Rating check runner to generate an automated rating for this module |
|
323
|
|
|
* |
|
324
|
|
|
* @param Addon $addon |
|
325
|
|
|
* @param string $modulePath |
|
326
|
|
|
*/ |
|
327
|
|
|
protected function rateModule(Addon $addon, $modulePath) |
|
328
|
|
|
{ |
|
329
|
|
|
$suite = $this->getCheckSuite(); |
|
330
|
|
|
$suite->setModuleRoot($modulePath); |
|
331
|
|
|
$repositoryUrl = $addon->Repository; |
|
|
|
|
|
|
332
|
|
|
// Get repository slug from URL |
|
333
|
|
|
preg_match('~.*\/(.+\/.+)(?:\.git)?$~', $repositoryUrl, $matches); |
|
334
|
|
|
$suite->setRepositorySlug(!empty($matches[1]) ? $matches[1] : ''); |
|
335
|
|
|
|
|
336
|
|
|
try { |
|
337
|
|
|
$suite->run(); |
|
338
|
|
|
} catch (\Exception $e) { |
|
339
|
|
|
echo $e->getMessage(); |
|
340
|
|
|
return; |
|
341
|
|
|
} |
|
342
|
|
|
/** @var array $checkDetails */ |
|
343
|
|
|
$checkDetails = $suite->getCheckDetails(); |
|
344
|
|
|
|
|
345
|
|
|
// Pull part of the check details out that we want (code and points) |
|
346
|
|
|
$details = array(); |
|
347
|
|
|
foreach ($checkDetails as $checkCode => $metrics) { |
|
348
|
|
|
$details[$checkCode] = $metrics['points']; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
// Total score for the check suite |
|
352
|
|
|
$addon->Rating = $suite->getScore(); |
|
|
|
|
|
|
353
|
|
|
// Individual checks and scores for each |
|
354
|
|
|
$addon->RatingDetails = Convert::raw2json($details); |
|
|
|
|
|
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
/** |
|
358
|
|
|
* @return CheckSuite |
|
359
|
|
|
*/ |
|
360
|
|
|
public function getCheckSuite() |
|
361
|
|
|
{ |
|
362
|
|
|
return $this->checkSuite; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/** |
|
366
|
|
|
* @param CheckSuite $checkSuite |
|
367
|
|
|
* @return $this |
|
368
|
|
|
*/ |
|
369
|
|
|
public function setCheckSuite($checkSuite) |
|
370
|
|
|
{ |
|
371
|
|
|
$this->checkSuite = $checkSuite; |
|
372
|
|
|
return $this; |
|
373
|
|
|
} |
|
374
|
|
|
} |
|
375
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.