Skip to content

Commit

Permalink
Move topological sort to separate function
Browse files Browse the repository at this point in the history
  • Loading branch information
zaerl committed Nov 26, 2024
1 parent b332a04 commit 7ee77a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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'] ) ) {
Expand Down Expand Up @@ -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() ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 7ee77a1

Please sign in to comment.