0, 'old_resource_migration_count' => 0, 'migrated_count' => 0 ]; /** * @var string[][] */ public $failedResources = []; /** * @var bool */ public $endReached = false; /** * @var ResourceCollection */ private $fileResourceCollection; /** * FileSerializerMigrationHelper constructor. * * @param bool $wetRun */ public function __construct($wetRun = false) { $this->isWetRun = $wetRun; } /** * Get the file resources and migrate them. * * @return void * @throws FileSerializerException */ public function migrateFiles() { $fileResources = $this->getFileResourceData(); foreach ($fileResources as $fileResourceData) { $resourceUri = $fileResourceData['fileResource']->getUri(); if (!isset($fileResourceData['property'])) { $this->failedResources[$resourceUri][] = 'Unable to find property'; continue; } if (!isset($fileResourceData['subject'])) { $this->failedResources[$resourceUri][] = 'Unable to find subject'; continue; } ++$this->migrationInformation['old_resource_count']; $this->migrateResource( $fileResourceData['fileResource'], $fileResourceData['property'], $fileResourceData['subject'] ); } } /** * Migrate a single resource. * * @param core_kernel_classes_Resource $fileResource * @param core_kernel_classes_Property $property * @param core_kernel_classes_Resource $subject * @return void * @throws FileSerializerException */ public function migrateResource(core_kernel_classes_Resource $fileResource, core_kernel_classes_Property $property, core_kernel_classes_Resource $subject) { /** @var Directory|File $unserializedFileResource */ $unserializedFileResource = $this->getResourceFileSerializer()->unserialize($fileResource); $migratedValue = $this->getUrlFileSerializer()->serialize($unserializedFileResource); if ($this->isWetRun) { $subject->editPropertyValues($property, $migratedValue); $fileResource->delete(); } ++$this->migrationInformation['migrated_count']; } /** * Get the URL file serializer */ private function getUrlFileSerializer() { if ($this->urlFileSerializer === null) { $this->urlFileSerializer = new UrlFileSerializer(); $this->urlFileSerializer->setServiceLocator($this->getServiceLocator()); } return $this->urlFileSerializer; } /** * Get the Resource file serializer */ private function getResourceFileSerializer() { if ($this->resourceFileSerializer === null) { $this->resourceFileSerializer = new ResourceFileSerializer(); $this->resourceFileSerializer->setServiceLocator($this->getServiceLocator()); } return $this->resourceFileSerializer; } /** * @return array */ private function getFileResourceData() { $mappedResourceData = []; if ($this->fileResourceCollection === null) { $this->fileResourceCollection = $this->getClass(GenerisRdf::CLASS_GENERIS_FILE)->getInstanceCollection(); $this->fileResourceCollection->useLimit(); } $fileResourceUris = []; foreach ($this->fileResourceCollection as $fileResource) { $fileResourceUris[] = $fileResource['subject']; } $this->endReached = $this->fileResourceCollection->getEndReached(); $parentFileResources = new ResourceCollection(null, 0); $parentFileResources->addFilter('object', 'IN', $fileResourceUris); foreach ($parentFileResources as $resourceUri => $parentFileResource) { $mappedResourceData[] = [ 'fileResource' => $this->getResource($parentFileResource['object']), 'property' => $this->getProperty($parentFileResource['predicate']), 'subject' => $this->getResource($parentFileResource['subject']), ]; } return $mappedResourceData; } }