diff --git a/src/Plugin/migrate/process/ExtractFileContents.php b/src/Plugin/migrate/process/ExtractFileContents.php new file mode 100644 index 0000000..21d93ae --- /dev/null +++ b/src/Plugin/migrate/process/ExtractFileContents.php @@ -0,0 +1,54 @@ +get('page'); + + if (is_null($value) || trim($value) === '') { + $error = "Skipping because content filepath is empty for {$page}."; + } + elseif (!is_file($value)) { + $error = "Skipping because content file is missing for {$page}."; + } + else { + $contents = file_get_contents($value); + if ($contents !== FALSE) { + return $contents; + } + + $error = "Skipping because content file is unreadable for {$page}."; + } + + if ($error) { + Drush::output()->writeln($error); + throw new MigrateSkipRowException($error); + } + + return ''; + } + +} diff --git a/src/Plugin/migrate/process/ExtractFromParagraph.php b/src/Plugin/migrate/process/ExtractFromParagraph.php new file mode 100644 index 0000000..bdeda55 --- /dev/null +++ b/src/Plugin/migrate/process/ExtractFromParagraph.php @@ -0,0 +1,55 @@ +hasField('field_body')) { + $body = $paragraph->get('field_body'); + $item = $body->get(0); + + if ($item) { + return $item->getValue()['value']; + } + } + else { + throw new MigrateException("Paragraph does not have field_body."); + } + + throw new MigrateException("Unable to find paragraph."); + } + +} diff --git a/src/Plugin/migrate/process/GenerateRedirectUri.php b/src/Plugin/migrate/process/GenerateRedirectUri.php new file mode 100644 index 0000000..7c99098 --- /dev/null +++ b/src/Plugin/migrate/process/GenerateRedirectUri.php @@ -0,0 +1,62 @@ +writeln($error); + throw new MigrateSkipRowException($error); + } + + /** @var \Drupal\node\NodeStorageInterface $nodeStorage */ + try { + $nodeStorage = \Drupal::entityTypeManager()->getStorage('node'); + $nodes = $nodeStorage->loadByProperties([ + 'field_legacy_url' => trim($value), + ]); + + if (!empty($nodes)) { + $redirect = 'entity:node/' . reset($nodes)->id(); + } + } + catch (PluginException $e) { + $redirect = ''; + } + + if (!$redirect) { + $error = sprintf('Skipped... Unable to map legacy url "%s" to a node.', $value); + Drush::output()->writeln($error); + throw new MigrateSkipRowException($error); + } + + Drush::output()->writeln($redirect); + return $redirect; + } + +} diff --git a/src/Plugin/migrate/source/CDesaiCSV.php b/src/Plugin/migrate/source/CDesaiCSV.php new file mode 100644 index 0000000..ce8ae1a --- /dev/null +++ b/src/Plugin/migrate/source/CDesaiCSV.php @@ -0,0 +1,103 @@ +configuration['source_config_key'])) { + throw new \InvalidArgumentException('You must declare the "source_config_key" in your source settings.'); + } + // Source config key must be one of the valid values. + elseif (!array_key_exists($this->configuration['source_config_key'], DemoMigrateConstantsInterface::SOURCES)) { + throw new \InvalidArgumentException('Invalid value for "source_config_key".'); + } + + parent::__construct($configuration, $plugin_id, $plugin_definition, $migration); + } + + /** + * Return a string representing the source file path. + * + * @return string + * The file path. + */ + public function __toString() { + return $this->getSourceFileName(); + } + + /** + * Construct a new CSV reader. + * + * @return \League\Csv\Reader + * The reader. + */ + protected function createReader(): Reader { + $path = $this->getSourceFileName(); + if (!file_exists($path)) { + throw new \RuntimeException(sprintf('File "%s" was not found.', $path)); + } + $csv = fopen($path, 'r'); + if (!$csv) { + throw new \RuntimeException(sprintf('File "%s" could not be opened.', $path)); + } + return Reader::createFromStream($csv); + } + + /** + * Get source path from configs. + */ + private function getSourceFileName(): string { + // CODE REDACTED + + return "REDACTED"; + } + +} diff --git a/src/Plugin/migrate/source/CDesaiParagraphMigrateSource.php b/src/Plugin/migrate/source/CDesaiParagraphMigrateSource.php new file mode 100644 index 0000000..e9039aa --- /dev/null +++ b/src/Plugin/migrate/source/CDesaiParagraphMigrateSource.php @@ -0,0 +1,169 @@ +paragraphsSourceFile = (string) $configs->get('cdesai_migrate_paragraphs_source_path'); + $this->pageSourceFiles = [ + (string) $configs->get('cdesai_migrate_articles_source_path'), + (string) $configs->get('cdesai_migrate_basic_pages_source_path'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getIds(): array { + return [ + 'page' => [ + 'type' => 'string', + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function fields(): array { + return [ + 'page' => $this->t("Page"), + 'skip_migration' => $this->t('Skip Migration'), + 'scrapped' => $this->t('Scrapped Content'), + 'content_file' => $this->t('Paragraph Content File'), + ]; + } + + /** + * {@inheritdoc} + */ + public function __toString() { + $this->initializeIterator(); + + return "Content Archive: \n\t{$this->paragraphsSourceFile} \n" + . "Page Metadata Files:\n\t" . implode("\n\t", $this->pageSourceFiles); + } + + /** + * {@inheritdoc} + * + * @throws \Drupal\migrate\MigrateException + */ + protected function initializeIterator() { + $this->requiredFilesExist(); + $pages = $this->extractPageData(); + + // CODE REDACTED + + // Migrate needs an Iterator class, not just an array. + return new \ArrayIterator($pages); + } + + /** + * Make sure all required source files exists. + * + * @throws \Drupal\migrate\MigrateException + */ + private function requiredFilesExist() { + $fileExists = TRUE; + + // CODE REDACTED + + if (!$fileExists) { + $fileUploadUri = Url::fromRoute('cdesai_migrate.settings')->getInternalPath(); + throw new MigrateException('Upload required source files at ' . $fileUploadUri); + } + } + + /** + * Retrieves paragraph content files from the archive. + * + * @throws \Drupal\migrate\MigrateException + */ + private function getParagraphContentFiles(string $archive_file): array { + $contentFiles = []; + $extractedDir = $this->extractArchiveFile($archive_file); + /** @var \Drupal\Core\File\FileSystem $fileSystem */ + $fileSystem = \Drupal::service('file_system'); + $files = $fileSystem->scanDirectory($extractedDir, '/.*txt/'); + if (empty($files)) { + throw new MigrateException('There was an issue extracting files form ' . $archive_file); + } + + foreach ($files as $file) { + $key = substr($file->filename, 0, strlen($file->filename) - 4); + $contentFiles[$key] = $file->uri; + } + + return $contentFiles; + } + + /** + * Extract the archive in a temp directory. + * + * @param string $filepath + * The file path. + * + * @return string + * The temp directory path containing the extracted archive file. + */ + private function extractArchiveFile(string $filepath): string { + /** @var \Drupal\Core\Archiver\ArchiverInterface $archiver */ + $archiveManager = \Drupal::service('plugin.manager.archiver'); + $tempPath = ""; + + // CODE REDACTED + + return 'temporary://' . $tempPath; + } + + /** + * Extract page data from CSV source files. + * + * @return array + * An associative array containing page data. + */ + private function extractPageData(): array { + $pages = []; + + // CODE REDACTED + + return $pages; + } + +}