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