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