1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* main class running all the updates |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class UpdateModules extends BuildTask |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
protected $enabled = true; |
11
|
|
|
|
12
|
|
|
protected $title = "Update Modules"; |
13
|
|
|
|
14
|
|
|
protected $description = "Adds files necessary for publishing a module to GitHub. The list of modules is specified in standard config or else it retrieves a list of modules from GitHub."; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* e.g. |
18
|
|
|
* - moduleA |
19
|
|
|
* - moduleB |
20
|
|
|
* - moduleC |
21
|
|
|
* |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $modules_to_update = array(); |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* e.g. |
29
|
|
|
* - ClassNameForUpdatingFileA |
30
|
|
|
* - ClassNameForUpdatingFileB |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private static $files_to_update = array(); |
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* e.g. |
37
|
|
|
* - ClassNameForUpdatingFileA |
38
|
|
|
* - ClassNameForUpdatingFileB |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private static $commands_to_run = array(); |
|
|
|
|
43
|
|
|
|
44
|
|
|
public static $unsolvedItems = array(); |
45
|
|
|
|
46
|
|
|
public function run($request) { |
47
|
|
|
increase_time_limit_to(3600); |
48
|
|
|
|
49
|
|
|
//Check temp module folder is empty |
50
|
|
|
$tempFolder = GitHubModule::Config()->get('absolute_temp_folder'); |
51
|
|
|
$tempDirFiles = scandir($tempFolder); |
52
|
|
|
if (count($tempDirFiles) > 2) { |
53
|
|
|
die ( '<h2>' . $tempFolder . ' is not empty, please delete or move files </h2>'); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
//Get list of all modules from GitHub |
57
|
|
|
$gitUserName = $this->Config()->get('git_user_name'); |
|
|
|
|
58
|
|
|
|
59
|
|
|
$modules = GitHubModule::getRepoList(); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$updateComposerJson = $this->Config()->get('update_composer_json'); |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
$limitedModules = $this->Config()->get('modules_to_update'); |
68
|
|
|
|
69
|
|
|
if($limitedModules && count($limitedModules)) { |
70
|
|
|
$modules = array_intersect($modules, $limitedModules); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/* |
76
|
|
|
* Get files to add to modules |
77
|
|
|
* */ |
78
|
|
|
$files = ClassInfo::subclassesFor('AddFileToModule'); |
79
|
|
|
|
80
|
|
|
array_shift($files); |
81
|
|
|
$limitedFileClasses = $this->Config()->get('files_to_update'); |
82
|
|
|
if($limitedFileClasses && count($limitedFileClasses)) { |
83
|
|
|
$files = array_intersect($files, $limitedFileClasses); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/* |
87
|
|
|
* Get commands to run on modules |
88
|
|
|
* */ |
89
|
|
|
|
90
|
|
|
$commands = ClassInfo::subclassesFor('RunCommandLineMethodOnModule'); |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
array_shift($commands); |
94
|
|
|
$limitedCommands = $this->Config()->get('commands_to_run'); |
95
|
|
|
if($limitedCommands && count($limitedCommands)) { |
96
|
|
|
$commands = array_intersect($commands, $limitedCommands); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
set_error_handler ('errorHandler', E_ALL); |
101
|
|
|
foreach($modules as $count => $module) { |
|
|
|
|
102
|
|
|
$this->currentModule = $module; |
|
|
|
|
103
|
|
|
try { |
104
|
|
|
|
105
|
|
|
$this->processOneModule($module, $count, $files, $commands, $updateComposerJson); |
106
|
|
|
} |
107
|
|
|
catch (Exception $e) { |
108
|
|
|
GeneralMethods::outputToScreen ("<li> Could not complete processing $module: " . $e->getMessage() . " </li>"); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
restore_error_handler(); |
115
|
|
|
|
116
|
|
|
$this->writeLog(); |
117
|
|
|
//to do .. |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function errorHandler(int $errno , string $errstr) { |
|
|
|
|
121
|
|
|
|
122
|
|
|
GeneralMethods::outputToScreen ("<li> Could not complete processing module: " . $errstr . " </li>"); |
123
|
|
|
|
124
|
|
|
UpdateModules::addUnsolvedProblem($this->currentModule, "Could not complete processing module: " . $errstr); |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function processOneModule($module, $count, $files, $commands, $updateComposerJson) { |
130
|
|
|
|
131
|
|
|
if ( stripos($module, 'silverstripe-') === false ) { |
132
|
|
|
$module = "silverstripe-" . $module; |
133
|
|
|
} |
134
|
|
|
echo "<h2>" . ($count+1) . ". ".$module."</h2>"; |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
$moduleObject = GitHubModule::get_or_create_github_module($module); |
138
|
|
|
|
139
|
|
|
$this->checkUpdateTag($moduleObject); |
140
|
|
|
|
141
|
|
|
// Check if all necessary files are perfect on GitHub repo already, |
142
|
|
|
// if so we can skip that module. But! ... if there are commands to run |
143
|
|
|
// over the files in the repo, then we need to clone the repo anyhow, |
144
|
|
|
// so skip the check |
145
|
|
|
if (count($commands) == 0 && ! $updateComposerJson) { |
146
|
|
|
$moduleFilesOK = true; |
147
|
|
|
|
148
|
|
|
foreach($files as $file) { |
149
|
|
|
$fileObj = $file::create($moduleObject); |
150
|
|
|
$checkFileName = $fileObj->getFileLocation(); |
151
|
|
|
$GitHubFileText = $moduleObject -> getRawFileFromGithub($checkFileName); |
152
|
|
|
if ($GitHubFileText) { |
153
|
|
|
$fileCheck = $fileObj->compareWithText($GitHubFileText); |
154
|
|
|
if ( ! $fileCheck) { |
155
|
|
|
$moduleFilesOK = false; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
else { |
159
|
|
|
$moduleFilesOK = false; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
|
164
|
|
|
|
165
|
|
|
if ($moduleFilesOK) { |
166
|
|
|
GeneralMethods::outputToScreen ("<li> All files in $module OK, skipping to next module ... </li>"); |
167
|
|
|
continue; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$repository = $moduleObject->checkOrSetGitCommsWrapper($forceNew = true); |
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
$this->moveOldReadMe($moduleObject); |
175
|
|
|
|
176
|
|
|
$this->checkConfigYML($moduleObject); |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
if ($updateComposerJson) { |
180
|
|
|
$composerJsonObj = new ComposerJson ($moduleObject); |
|
|
|
|
181
|
|
|
$composerJsonObj->updateJsonData(); |
182
|
|
|
$moduleObject->setDescription($composerJsonObj->getDescription()); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
$excludedWords = $this->Config()->get('excluded_words'); |
186
|
|
|
|
187
|
|
|
|
188
|
|
|
if (count($excludedWords) > 0) |
189
|
|
|
{ |
190
|
|
|
$folder = GitHubModule::Config()->get('absolute_temp_folder') . '/' . $moduleObject->moduleName . '/'; |
191
|
|
|
|
192
|
|
|
$results = $this->checkDirExcludedWords($folder.'/'.$moduleObject->modulename, $excludedWords); |
193
|
|
|
|
194
|
|
|
|
195
|
|
|
if ($results && count ($results > 0)) |
|
|
|
|
196
|
|
|
{ |
197
|
|
|
$msg = "<h4>The following excluded words were found: </h4><ul>"; |
198
|
|
|
foreach ($results as $file => $words) { |
199
|
|
|
foreach ($words as $word) { |
200
|
|
|
$msg .= "<li>$word in $file</li>"; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
$msg .= '</ul>'; |
204
|
|
|
|
205
|
|
|
//trigger_error ("excluded words found in files(s)"); |
|
|
|
|
206
|
|
|
GeneralMethods::outputToScreen ($msg); |
207
|
|
|
UpdateModules::$unsolvedItems[$moduleObject->ModuleName] = $msg; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
foreach($files as $file) { |
214
|
|
|
//run file update |
215
|
|
|
|
216
|
|
|
$obj = $file::create($moduleObject); |
217
|
|
|
$obj->run(); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
$moduleDir = $moduleObject->Directory(); |
221
|
|
|
|
222
|
|
|
foreach($commands as $command) { |
223
|
|
|
//run file update |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
$obj = $command::create($moduleDir); |
227
|
|
|
$obj->run(); |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
//run command |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
//Update Repository description |
234
|
|
|
//$moduleObject->updateGitHubInfo(array()); |
|
|
|
|
235
|
|
|
|
236
|
|
|
if( ! $moduleObject->add()) { die("ERROR in add"); } |
|
|
|
|
237
|
|
|
if( ! $moduleObject->commit()) { die("ERROR in commit"); } |
|
|
|
|
238
|
|
|
if( ! $moduleObject->push()) { die("ERROR in push"); } |
|
|
|
|
239
|
|
|
if( ! $moduleObject->removeClone()) { die("ERROR in removeClone"); } |
|
|
|
|
240
|
|
|
|
241
|
|
|
$moduleObject->addRepoToScrutinzer(); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
|
246
|
|
|
protected function renameTest($moduleObject) { |
247
|
|
|
|
248
|
|
|
$oldName = $moduleObject->Directory() . "/tests/ModuleTest.php"; |
249
|
|
|
|
250
|
|
|
if ( ! file_exists($oldName) ) |
251
|
|
|
{ |
252
|
|
|
print_r ($oldName); |
253
|
|
|
return false; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
|
258
|
|
|
$newName = $moduleObject->Directory() . "tests/" . $moduleObject->ModuleName . "Test.php"; |
259
|
|
|
|
260
|
|
|
GeneralMethods::outputToScreen ("Renaming $oldName to $newName"); |
261
|
|
|
|
262
|
|
|
unlink ($newName); |
263
|
|
|
|
264
|
|
|
rename($oldName, $newName); |
265
|
|
|
|
266
|
|
|
|
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public static function addUnsolvedProblem($moduleName, $problemString) { |
270
|
|
|
if (!isset (UpdateModules::$unsolvedItems[$moduleName]) ) |
271
|
|
|
{ |
272
|
|
|
UpdateModules::$unsolvedItems[$moduleName] = array(); |
273
|
|
|
} |
274
|
|
|
array_push (UpdateModules::$unsolvedItems[$moduleName], $problemString); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
protected function writeLog() { |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
|
281
|
|
|
$mailTo = $this->Config()->get('report_email'); |
|
|
|
|
282
|
|
|
$debug = $this->Config()->get('debug'); |
|
|
|
|
283
|
|
|
|
284
|
|
|
$dateStr = date("Y/m/d H:i:s"); |
285
|
|
|
|
286
|
|
|
$html = '<h1> Modules checker report at ' .$dateStr . '</h1>'; |
287
|
|
|
|
288
|
|
|
if (count (UpdateModules::$unsolvedItems) == 0) { |
289
|
|
|
$html .= ' <h2> No unresolved problems in modules</h2>'; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
else { |
293
|
|
|
$html .= ' |
294
|
|
|
<h2> Unresolved problems in modules</h2> |
295
|
|
|
|
296
|
|
|
<table border = 1> |
297
|
|
|
<tr><th>Module</th><th>Problem</th></tr>'; |
298
|
|
|
|
299
|
|
|
foreach (UpdateModules::$unsolvedItems as $moduleName => $problems) { |
300
|
|
|
|
301
|
|
|
foreach ($problems as $problem) { |
302
|
|
|
|
303
|
|
|
$html .= '<tr><td>'.$moduleName.'</td><td>'. $problem .'</td></tr>'; |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
$html .= '</table>'; |
307
|
|
|
; |
308
|
|
|
|
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
$logFolder = getcwd() . '/../modulechecks/logs/'; |
313
|
|
|
|
314
|
|
|
$filename = $logFolder . date('U') . '.html'; |
315
|
|
|
|
316
|
|
|
GeneralMethods::outputToScreen ("Writing to $filename"); |
317
|
|
|
|
318
|
|
|
$result = file_put_contents ( $filename, $html); |
319
|
|
|
|
320
|
|
|
if ( ! $result ) |
321
|
|
|
{ |
322
|
|
|
GeneralMethods::outputToScreen ("Could not write log file"); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
|
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
protected function checkConfigYML($module) |
330
|
|
|
{ |
331
|
|
|
$configYml = ConfigYML::create($module)->reWrite(); |
|
|
|
|
332
|
|
|
|
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
private function checkFile($module, $filename) { |
336
|
|
|
$folder = GitHubModule::Config()->get('absolute_temp_folder'); |
337
|
|
|
return file_exists($folder.'/'.$module.'/'.$filename); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
private function checkReadMe($module) { |
341
|
|
|
return $this->checkFile($module, "README.MD"); |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
private function checkDirExcludedWords($directory, $wordArray) { |
345
|
|
|
$filesAndFolders = scandir ($directory); |
346
|
|
|
|
347
|
|
|
$problem_files = array(); |
348
|
|
|
foreach ($filesAndFolders as $fileOrFolder) { |
349
|
|
|
|
350
|
|
|
if ($fileOrFolder == '.' || $fileOrFolder == '..' || $fileOrFolder == '.git' ) { |
351
|
|
|
continue; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
$fileOrFolderFullPath = $directory . '/' . $fileOrFolder; |
355
|
|
|
if (is_dir($fileOrFolderFullPath)) { |
356
|
|
|
$dir = $fileOrFolderFullPath; |
357
|
|
|
$problem_files = array_merge ($this->checkDirExcludedWords ($dir, $wordArray) , $problem_files); |
358
|
|
|
} |
359
|
|
|
if (is_file($fileOrFolderFullPath)) { |
360
|
|
|
$file = $fileOrFolderFullPath; |
361
|
|
|
$matchedWords = $this->checkFileExcludedWords($file, $wordArray); |
362
|
|
|
|
363
|
|
|
if ($matchedWords) { |
364
|
|
|
|
365
|
|
|
$problem_files[$file] = $matchedWords; |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $problem_files; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
private function checkFileExcludedWords($fileName, $wordArray) { |
374
|
|
|
|
375
|
|
|
|
376
|
|
|
$matchedWords = array(); |
377
|
|
|
|
378
|
|
|
$fileName = str_replace ('////', '/', $fileName); |
379
|
|
|
if (filesize ($fileName) == 0 ) return $matchedWords; |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
$fileContent = file_get_contents($fileName); |
383
|
|
|
if (!$fileContent) (die ("could not open $fileName</br>")); |
|
|
|
|
384
|
|
|
|
385
|
|
|
foreach ($wordArray as $word) { |
386
|
|
|
|
387
|
|
|
|
388
|
|
|
$matches = array(); |
|
|
|
|
389
|
|
|
$matchCount = preg_match_all('/' . $word . '/i', $fileContent); |
390
|
|
|
|
391
|
|
|
|
392
|
|
|
|
393
|
|
|
|
394
|
|
|
|
395
|
|
|
if ($matchCount > 0) { |
396
|
|
|
array_push ($matchedWords, $word); |
397
|
|
|
|
398
|
|
|
} |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
return $matchedWords; |
402
|
|
|
|
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
private function checkUpdateTag($moduleObject) { |
406
|
|
|
|
407
|
|
|
$aWeekAgo = strtotime("-1 weeks"); |
408
|
|
|
$tag = $moduleObject->getLatestTag(); |
409
|
|
|
|
410
|
|
|
$commitTime = $moduleObject->getLatestCommitTime(); |
411
|
|
|
|
412
|
|
|
if (! $commitTime) // if no commits, cannot create a tag |
413
|
|
|
{ |
414
|
|
|
return false; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$createTag = false; |
418
|
|
|
|
419
|
|
|
if ( ! $tag ) { |
420
|
|
|
$createTag = true; |
421
|
|
|
$newTagString = '1.0.0'; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
|
425
|
|
|
|
426
|
|
|
else if ($tag && $commitTime > $tag['timestamp'] && $commitTime < $aWeekAgo) { |
427
|
|
|
$createTag = true; |
428
|
|
|
$tag['tagparts'][1] = $tag['tagparts'][1] + 1; |
429
|
|
|
$newTagString = trim(implode ('.', $tag['tagparts'])); |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
if ($createTag) { |
433
|
|
|
|
434
|
|
|
GeneralMethods::outputToScreen ('<li> Creating new tag '.$newTagString.' ... </li>'); |
|
|
|
|
435
|
|
|
|
436
|
|
|
//git tag -a 0.0.1 -m "testing tag" |
437
|
|
|
$options = array ( |
438
|
|
|
'a' => $newTagString, |
439
|
|
|
'm' => $this->Config()->get('tag_create_message') |
440
|
|
|
); |
441
|
|
|
|
442
|
|
|
$moduleObject->createTag ($options); |
443
|
|
|
|
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return true; |
447
|
|
|
|
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
protected function moveOldReadMe($moduleObject) { |
451
|
|
|
$tempDir = GitHubModule::Config()->get('absolute_temp_folder'); |
452
|
|
|
$oldReadMe = $tempDir . '/' . $moduleObject->ModuleName . '/' .'README.md'; |
453
|
|
|
|
454
|
|
|
if (!file_exists($oldReadMe)) |
455
|
|
|
{ |
456
|
|
|
return false; |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
|
460
|
|
|
$oldreadmeDestinationFiles = array ( |
461
|
|
|
'docs/en/INDEX.md', |
462
|
|
|
'docs/en/README.old.md', |
463
|
|
|
); |
464
|
|
|
|
465
|
|
|
|
466
|
|
|
$copied = false; |
467
|
|
|
foreach ($oldreadmeDestinationFiles as $file) { |
468
|
|
|
$filePath = $tempDir . '/' . $moduleObject->ModuleName . '/' . $file; |
469
|
|
|
|
470
|
|
|
if (!file_exists($filePath)) { |
471
|
|
|
$copied = true; |
472
|
|
|
copy($oldReadMe, $filePath); |
473
|
|
|
} |
474
|
|
|
|
475
|
|
|
} |
476
|
|
|
if ($copied) |
477
|
|
|
{ |
478
|
|
|
unlink ($oldReadMe); |
479
|
|
|
} |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
|
483
|
|
|
} |
484
|
|
|
|
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.