1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Tasks; |
4
|
|
|
|
5
|
|
|
use Monolog\Handler\StreamHandler; |
6
|
|
|
use Monolog\Logger; |
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
use SilverStripe\AssetAdmin\Helper\ImageThumbnailHelper; |
|
|
|
|
9
|
|
|
use SilverStripe\Assets\Dev\Tasks\LegacyThumbnailMigrationHelper; |
|
|
|
|
10
|
|
|
use SilverStripe\Assets\FileMigrationHelper; |
11
|
|
|
use SilverStripe\Assets\Storage\AssetStore; |
12
|
|
|
use SilverStripe\Control\Director; |
13
|
|
|
use SilverStripe\Core\Injector\Injector; |
14
|
|
|
use SilverStripe\Logging\PreformattedEchoHandler; |
15
|
|
|
use SilverStripe\Dev\BuildTask; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Migrates all 3.x file dataobjects to use the new DBFile field. |
19
|
|
|
*/ |
20
|
|
|
class MigrateFileTask extends BuildTask |
21
|
|
|
{ |
22
|
|
|
private static $segment = 'MigrateFileTask'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
protected $title = 'Migrate File dataobjects from 3.x and successive iterations in 4.x'; |
25
|
|
|
|
26
|
|
|
protected $defaultSubtasks = [ |
27
|
|
|
'move-files', |
28
|
|
|
'move-thumbnails', |
29
|
|
|
'generate-cms-thumbnails', |
30
|
|
|
'fix-folder-permissions' |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
private static $dependencies = [ |
|
|
|
|
34
|
|
|
'logger' => '%$' . LoggerInterface::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** @var Logger */ |
38
|
|
|
private $logger; |
39
|
|
|
|
40
|
|
|
public function run($request) |
41
|
|
|
{ |
42
|
|
|
$this->addLogHandlers(); |
43
|
|
|
|
44
|
|
|
$args = $request->getVars(); |
45
|
|
|
$this->validateArgs($args); |
46
|
|
|
|
47
|
|
|
$subtasks = !empty($args['only']) ? explode(',', $args['only']) : $this->defaultSubtasks; |
48
|
|
|
|
49
|
|
|
if (in_array('move-files', $subtasks)) { |
50
|
|
|
if (!class_exists(FileMigrationHelper::class)) { |
51
|
|
|
$this->logger->error("No file migration helper detected"); |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->logger->info('### Migrating filesystem and database records (move-files)'); |
56
|
|
|
|
57
|
|
|
$this->logger->info('If the task fails or times out, run it again and it will start where it left off.'); |
58
|
|
|
|
59
|
|
|
$migrated = FileMigrationHelper::singleton()->run(); |
60
|
|
|
if ($migrated) { |
61
|
|
|
$this->logger->info("{$migrated} File DataObjects upgraded"); |
62
|
|
|
} else { |
63
|
|
|
$this->logger->info("No File DataObjects need upgrading"); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (in_array('move-thumbnails', $subtasks)) { |
68
|
|
|
if (!class_exists(LegacyThumbnailMigrationHelper::class)) { |
69
|
|
|
$this->logger->error("LegacyThumbnailMigrationHelper not found"); |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->logger->info('### Migrating existing thumbnails (move-thumbnails)'); |
74
|
|
|
|
75
|
|
|
$moved = LegacyThumbnailMigrationHelper::singleton() |
76
|
|
|
->setLogger($this->logger) |
77
|
|
|
->run($this->getStore()); |
78
|
|
|
|
79
|
|
|
if ($moved) { |
80
|
|
|
$this->logger->info(sprintf("%d thumbnails moved", count($moved))); |
81
|
|
|
} else { |
82
|
|
|
$this->logger->info("No thumbnails moved"); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (in_array('generate-cms-thumbnails', $subtasks)) { |
87
|
|
|
if (!class_exists(ImageThumbnailHelper::class)) { |
88
|
|
|
$this->logger->error("ImageThumbnailHelper not found"); |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$this->logger->info('### Generating new CMS UI thumbnails (generate-cms-thumbnails)'); |
93
|
|
|
|
94
|
|
|
ImageThumbnailHelper::singleton()->run(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (in_array('fix-folder-permissions', $subtasks)) { |
98
|
|
|
if (!class_exists(FixFolderPermissionsHelper::class)) { |
|
|
|
|
99
|
|
|
$this->logger->error("FixFolderPermissionsHelper not found"); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->logger->info('### Fixing folder permissions (fix-folder-permissions)'); |
104
|
|
|
|
105
|
|
|
$updated = FixFolderPermissionsHelper::singleton()->run(); |
106
|
|
|
|
107
|
|
|
if ($updated > 0) { |
108
|
|
|
$this->logger->info("Repaired {$updated} folders with broken CanViewType settings"); |
109
|
|
|
} else { |
110
|
|
|
$this->logger->info("No folders required fixes"); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getDescription() |
116
|
|
|
{ |
117
|
|
|
return <<<TXT |
118
|
|
|
Imports all files referenced by File dataobjects into the new Asset Persistence Layer introduced in 4.0. |
119
|
|
|
Moves existing thumbnails, and generates new thumbnail sizes for the CMS UI. |
120
|
|
|
Fixes file permissions. |
121
|
|
|
If the task fails or times out, run it again and it will start where it left off. |
122
|
|
|
You need to flush your cache after running this task via CLI. |
123
|
|
|
See https://docs.silverstripe.org/en/4/developer_guides/files/file_migration/. |
124
|
|
|
TXT; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param LoggerInterface $logger |
129
|
|
|
*/ |
130
|
|
|
public function setLogger(LoggerInterface $logger) |
131
|
|
|
{ |
132
|
|
|
$this->logger = $logger; |
|
|
|
|
133
|
|
|
|
134
|
|
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return AssetStore |
139
|
|
|
*/ |
140
|
|
|
protected function getStore() |
141
|
|
|
{ |
142
|
|
|
return singleton(AssetStore::class); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @param array $args |
147
|
|
|
* @throws \InvalidArgumentException |
148
|
|
|
*/ |
149
|
|
|
protected function validateArgs($args) |
150
|
|
|
{ |
151
|
|
|
if (!empty($args['only'])) { |
152
|
|
|
if (array_diff(explode(',', $args['only']), $this->defaultSubtasks)) { |
153
|
|
|
throw new \InvalidArgumentException('Invalid subtasks detected: ' . $args['only']); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* TODO Refactor this whole mess into Symfony Console on a TaskRunner level, |
160
|
|
|
* with a thin wrapper to show coloured console output via a browser: |
161
|
|
|
* https://github.com/silverstripe/silverstripe-framework/issues/5542 |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
protected function addLogHandlers() |
165
|
|
|
{ |
166
|
|
|
if ($logger = Injector::inst()->get(LoggerInterface::class)) { |
167
|
|
|
if (Director::is_cli()) { |
168
|
|
|
$logger->pushHandler(new StreamHandler('php://stdout')); |
169
|
|
|
$logger->pushHandler(new StreamHandler('php://stderr', Logger::WARNING)); |
170
|
|
|
} else { |
171
|
|
|
$logger->pushHandler(new PreformattedEchoHandler()); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths