|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\PerfectCmsImages\Tasks; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
|
6
|
|
|
use SilverStripe\Assets\Image; |
|
7
|
|
|
use SilverStripe\Control\HTTPRequest; |
|
8
|
|
|
use SilverStripe\Dev\BuildTask; |
|
9
|
|
|
use SilverStripe\ORM\DB; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class DeleteGeneratedImagesTask. |
|
13
|
|
|
* |
|
14
|
|
|
* Hack to allow removing manipulated images |
|
15
|
|
|
* This is needed occasionally when manipulation functions change |
|
16
|
|
|
* It isn't directly possible with core so this is a workaround |
|
17
|
|
|
* |
|
18
|
|
|
* @see https://github.com/silverstripe/silverstripe-assets/issues/109 |
|
19
|
|
|
* @codeCoverageIgnore |
|
20
|
|
|
*/ |
|
21
|
|
|
class FixSvgs extends BuildTask |
|
22
|
|
|
{ |
|
23
|
|
|
public function getTitle(): string |
|
24
|
|
|
{ |
|
25
|
|
|
return 'Fix Svg Images that are saved as Files rather than Images'; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getDescription(): string |
|
29
|
|
|
{ |
|
30
|
|
|
return 'Go through all the Files, check if they are SVGs and then change the classname to Image.'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Create test jobs for the purposes of testing. |
|
35
|
|
|
* |
|
36
|
|
|
* @param HTTPRequest $request |
|
37
|
|
|
*/ |
|
38
|
|
|
public function run($request) // phpcs:ignore |
|
39
|
|
|
{ |
|
40
|
|
|
$files = $this->getBatchOfFiles(); |
|
41
|
|
|
while ($files && $files->exists()) { |
|
42
|
|
|
foreach ($files as $file) { |
|
43
|
|
|
if ('svg' === $file->getExtension()) { |
|
44
|
|
|
DB::alteration_message('Fixing ' . $file->Link()); |
|
45
|
|
|
$wasPublished = $file->isPublished() && ! $file->isModifiedOnDraft(); |
|
46
|
|
|
$file->ClassName = Image::class; |
|
47
|
|
|
$file->write(); |
|
48
|
|
|
if ($wasPublished) { |
|
49
|
|
|
$file->publishSingle(); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
$files = $this->getBatchOfFiles(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function getBatchOfFiles() |
|
58
|
|
|
{ |
|
59
|
|
|
return File::get()->filter(['Name:PartialMatch' => '.svg'])->exclude(['ClassName' => Image::class])->limit(100); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|