From 7ee77a1c11252f1fb0f6828c914b423d2984d89a Mon Sep 17 00:00:00 2001 From: Francesco Bigiarini Date: Tue, 26 Nov 2024 22:37:11 +0100 Subject: [PATCH] Move topological sort to separate function --- .../src/import/WP_Stream_Importer.php | 53 ++++++++++++++----- .../data-liberation/src/wxr/WP_WXR_Reader.php | 2 +- 2 files changed, 42 insertions(+), 13 deletions(-) diff --git a/packages/playground/data-liberation/src/import/WP_Stream_Importer.php b/packages/playground/data-liberation/src/import/WP_Stream_Importer.php index 6bda0ec1f9..a13dd26d44 100644 --- a/packages/playground/data-liberation/src/import/WP_Stream_Importer.php +++ b/packages/playground/data-liberation/src/import/WP_Stream_Importer.php @@ -176,8 +176,7 @@ public function next_step() { $this->stage = self::STAGE_TOPOLOGICAL_SORT; return true; case self::STAGE_TOPOLOGICAL_SORT: - // @TODO: Topologically sort the entities. - $this->stage = self::STAGE_FRONTLOAD_ASSETS; + $this->next_topological_sort_step(); return true; case self::STAGE_FRONTLOAD_ASSETS: $this->next_frontloading_step(); @@ -237,6 +236,42 @@ private function frontloading_advance_reentrancy_cursor() { } } + private function next_topological_sort_step() { + if ( null === $this->entity_iterator ) { + $this->downloader = new WP_Attachment_Downloader( $this->options ); + $this->entity_iterator = $this->create_entity_iterator(); + $this->topological_sorter = new WP_Topological_Sorter(); + } + + if ( ! $this->entity_iterator->valid() ) { + $this->stage = self::STAGE_FRONTLOAD_ASSETS; + $this->topological_sorter = null; + $this->downloader = null; + $this->entity_iterator = null; + $this->resume_at_entity = null; + return; + } + + // $cursor = $this->entity_iterator->get_reentrancy_cursor(); + $entity = $this->entity_iterator->current(); + $data = $entity->get_data(); + $upstream = $this->entity_iterator->get_entity_byte_offset(); + + switch ( $entity->get_type() ) { + case 'category': + case 'term': + $this->topological_sorter->map_term( $upstream, $data ); + break; + case 'post': + $this->topological_sorter->map_post( $upstream, $data ); + break; + } + + $this->entity_iterator->next(); + + return true; + } + /** * Downloads all the assets referenced in the imported entities. * @@ -296,22 +331,15 @@ private function next_frontloading_step() { $cursor = $this->entity_iterator->get_reentrancy_cursor(); $this->active_downloads[ $cursor ] = array(); - $data = $entity->get_data(); - $upstream = $this->entity_iterator->get_upstream(); + $data = $entity->get_data(); switch ( $entity->get_type() ) { - case 'category': - case 'term': - $this->topological_sorter->map_term( $upstream, $data ); - break; case 'site_option': if ( $data['option_name'] === 'home' ) { $this->source_site_url = $data['option_value']; } break; case 'post': - $this->topological_sorter->map_post( $upstream, $data ); - if ( isset( $data['post_type'] ) && $data['post_type'] === 'attachment' ) { $this->enqueue_attachment_download( $data['attachment_url'], null ); } elseif ( isset( $data['post_content'] ) ) { @@ -351,8 +379,9 @@ private function next_frontloading_step() { */ private function import_next_entity() { if ( null === $this->entity_iterator ) { - $this->entity_iterator = $this->create_entity_iterator(); - $this->importer = new WP_Entity_Importer(); + $this->downloader = new WP_Attachment_Downloader( $this->options ); + $this->entity_iterator = $this->create_entity_iterator(); + $this->topological_sorter = new WP_Topological_Sorter(); } if ( ! $this->entity_iterator->valid() ) { diff --git a/packages/playground/data-liberation/src/wxr/WP_WXR_Reader.php b/packages/playground/data-liberation/src/wxr/WP_WXR_Reader.php index 3b2988457e..806dfd50f5 100644 --- a/packages/playground/data-liberation/src/wxr/WP_WXR_Reader.php +++ b/packages/playground/data-liberation/src/wxr/WP_WXR_Reader.php @@ -389,7 +389,7 @@ protected function __construct( WP_XML_Processor $xml ) { $this->xml = $xml; } - public function get_upstream() { + public function get_entity_byte_offset() { return $this->entity_byte_offset; }