|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vendor\Sunnysideup\AssetsOverview\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
7
|
|
|
use SilverStripe\Dev\BuildTask; |
|
8
|
|
|
use SilverStripe\ORM\DB; |
|
9
|
|
|
use Sunnysideup\AssetsOverview\Api\AddAndRemoveFromDb; |
|
10
|
|
|
use Sunnysideup\AssetsOverview\Files\AllFilesInfo; |
|
11
|
|
|
|
|
12
|
|
|
class DeleteFilesFromDiskNotFoundInDatabase extends BuildTask |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
protected $title = 'Delete files from disk not found in database'; |
|
16
|
|
|
|
|
17
|
|
|
protected $description = 'This task will go through all files in assets and delete ones that are not in the database.'; |
|
18
|
|
|
|
|
19
|
|
|
private static $segment = 'delete-files-from-disk-not-found-in-database'; |
|
20
|
|
|
|
|
21
|
|
|
protected $dryRun = true; |
|
22
|
|
|
|
|
23
|
|
|
public function run($request) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->dryRun = $request && $request->getVar('forreal') ? false : true; |
|
26
|
|
|
$this->dryRunMessage(); |
|
27
|
|
|
$files = AllFilesInfo::inst()->getAllFiles(); |
|
28
|
|
|
DB::alteration_message('=== START ==='); |
|
29
|
|
|
foreach ($files as $file) { |
|
30
|
|
|
$array = $file->toMap(); |
|
31
|
|
|
if (!empty($array['ErrorDBNotPresent'])) { |
|
32
|
|
|
DB::alteration_message('--- Deleting from DB' . $array['Path'], 'deleted'); |
|
33
|
|
|
if ($this->dryRun) { |
|
34
|
|
|
DB::alteration_message('--- --- DRY RUN ONLY ---', 'deleted'); |
|
35
|
|
|
} else { |
|
36
|
|
|
try { |
|
37
|
|
|
unlink($array['AbsolutePath']); |
|
38
|
|
|
} catch (Exception $e) { |
|
39
|
|
|
DB::alteration_message('Error: ' . $e->getMessage(), 'deleted'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} else { |
|
43
|
|
|
echo '✓'; |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
echo PHP_EOL; |
|
47
|
|
|
DB::alteration_message('=== END ==='); |
|
48
|
|
|
|
|
49
|
|
|
$this->dryRunMessage(); |
|
50
|
|
|
DB::alteration_message('=== DONE ===', ''); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function dryRunMessage() |
|
54
|
|
|
{ |
|
55
|
|
|
if ($this->dryRun) { |
|
56
|
|
|
DB::alteration_message('Please set forreal=true to actually do this', 'created'); |
|
57
|
|
|
} else { |
|
58
|
|
|
DB::alteration_message('Doing it for real!', 'created'); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|