Skip to content

Commit d3cf3d5

Browse files
committed
Add example to migrate existing files
1 parent 23515bc commit d3cf3d5

File tree

1 file changed

+98
-24
lines changed

1 file changed

+98
-24
lines changed

docs/migration/wsc60/php.md

Lines changed: 98 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -205,38 +205,50 @@ Previously, the `UploadFormField` class was used to create file upload fields in
205205
Now, the new `FileProcessorFormField` should be used,
206206
which separates file validation and processing into a dedicated class, the `IFileProcessor`.
207207

208+
Only the fileID or several fileIDs now need to be saved in the database.
209+
These should have a foreign key to `wcf1_file.fileID`.
210+
211+
The previously required function (`getFooUploadFiles`) to get `UploadFile[]` is no longer needed and can be removed.
212+
213+
### Example
214+
215+
In this example, the `Foo` object will store the `imageID` of the uploaded file.
216+
208217
#### Example using `FileProcessorFormField`
209218

210-
With the new `FileProcessorFormField`, file validation and processing is handled by a separate class,
211-
the `IFileProcessor`.
212219
The form field now provides information about which `IFileProcessor` should be used for the file upload,
213220
by specifying the object type of `com.woltlab.wcf.file`.
214221

215222
```PHP
216-
#[\Override]
217-
protected function createForm(): void
223+
final class FooAddForm extends AbstractFormBuilderForm
218224
{
219-
parent::createForm();
220-
$this->form->appendChildren([
221-
FormContainer::create('imageContainer')
222-
->appendChildren([
223-
FileProcessorFormField::create('imageID')
224-
->singleFileUpload()
225-
->required()
226-
->context($this->getContent())
227-
->objectType('foo.bar.image')
228-
]),
229-
]);
230-
}
225+
#[\Override]
226+
protected function createForm(): void
227+
{
228+
parent::createForm();
229+
230+
$this->form->appendChildren([
231+
FormContainer::create('imageContainer')
232+
->appendChildren([
233+
FileProcessorFormField::create('imageID')
234+
->singleFileUpload()
235+
->required()
236+
->context($this->getContent())
237+
->objectType('foo.bar.image')
238+
]),
239+
]);
240+
}
231241

232-
protected function getContent(): array
233-
{
234-
if ($this->formObject !== null) {
235-
return [
236-
'fooID' => $this->formObject->fooID,
237-
];
242+
protected function getContent(): array
243+
{
244+
if ($this->formObject !== null) {
245+
return [
246+
'fooID' => $this->formObject->fooID,
247+
];
248+
}
249+
250+
return [];
238251
}
239-
return [];
240252
}
241253
```
242254

@@ -354,7 +366,7 @@ final class FooImageFileProcessor extends AbstractFileProcessor
354366
public function getObjectTypeName(): string
355367
{
356368
return 'foo.bar.image';
357-
}
369+
}
358370

359371
#[\Override]
360372
public function countExistingFiles(array $context): ?int
@@ -379,3 +391,65 @@ final class FooImageFileProcessor extends AbstractFileProcessor
379391
}
380392
```
381393

394+
### Migrating existing files
395+
396+
To insert existing files into the upload pipeline,
397+
a `RebuildDataWorker` should be used which calls `FileEditor::createFromExistingFile()`.
398+
399+
#### Example for a `RebuildDataWorker`
400+
401+
```PHP
402+
final class FooRebuildDataWorker extends AbstractLinearRebuildDataWorker
403+
{
404+
/**
405+
* @inheritDoc
406+
*/
407+
protected $objectListClassName = FooList::class;
408+
409+
/**
410+
* @inheritDoc
411+
*/
412+
protected $limit = 100;
413+
414+
#[\Override]
415+
public function execute()
416+
{
417+
parent::execute();
418+
419+
$fooToFileID = [];
420+
$defunctFileIDs = [];
421+
422+
foreach ($this->objectList as $foo) {
423+
if ($foo->imageID !== null) {
424+
continue;
425+
}
426+
427+
$file = FileEditor::createFromExistingFile(
428+
$foo->getLocation(),
429+
$foo->getFilename(),
430+
'foo.bar.image'
431+
);
432+
433+
if ($file === null) {
434+
$defunctFileIDs[] = $foo->fooID;
435+
continue;
436+
}
437+
438+
$fooToFileID[$foo->fooID] = $file->fileID;
439+
}
440+
441+
$this->saveFileIDs($fooToFileID);
442+
// disable or delete defunct foo objects
443+
}
444+
445+
/**
446+
* @param array<int,int> $fooToFileID
447+
*/
448+
private function saveFileIDs(array $fooToFileID): void
449+
{
450+
// store fileIDs in database
451+
}
452+
}
453+
```
454+
455+
See [WoltLab/WCF#5911](https://github.com/WoltLab/WCF/pull/5951) for more details.

0 commit comments

Comments
 (0)