Old Tainacan importer: Collections pagination

This commit is contained in:
andre2ar 2018-05-04 13:26:18 -03:00
parent 4512ca734c
commit d7942e80da
3 changed files with 77 additions and 47 deletions

View File

@ -354,6 +354,15 @@ abstract class Importer {
$this->steps =$steps; $this->steps =$steps;
} }
public function is_finished()
{
if($this->current_step >= count($this->steps))
{
return true;
}
return false;
}
/** /**
* Removes method accepeted by the importer * Removes method accepeted by the importer
* *

View File

@ -47,7 +47,8 @@ class Old_Tainacan extends Importer
'Create repository metadata' => 'create_repo_meta', 'Create repository metadata' => 'create_repo_meta',
'Create collections metadata' => 'create_collection_metas', 'Create collections metadata' => 'create_collection_metas',
'Create collections items' => 'create_collection_items', 'Create collections items' => 'create_collection_items',
'Setting relationships' => 'set_relationships' 'Setting relationships' => 'set_relationships',
"Finishing" => 'clear'
], $tainacan_api_address, $wordpress_api_address; ], $tainacan_api_address, $wordpress_api_address;
@ -63,25 +64,14 @@ class Old_Tainacan extends Importer
$Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance();
$categories_array = $this->remove_same_name($categories_array); $categories_array = $this->remove_same_name($categories_array);
$start = $this->get_start();
$total_items = count($categories_array);
if($start >= $total_items) list($start, $end) = $this->get_begin_end($categories_array);
{ if($start === false) return false;
return false;
}
$end = $this->get_start() + $this->get_items_per_step();
if($end > $total_items)
{
$end = $total_items;
}
$created_categories = []; $created_categories = [];
while($start < $end) while($start < $end)
{ {
for($i = $start; $i < $end; $i++) $category = $categories_array[$start];
{
$category = $categories_array[$i];
$taxonomy = new \Tainacan\Entities\Taxonomy(); $taxonomy = new \Tainacan\Entities\Taxonomy();
@ -100,11 +90,12 @@ class Old_Tainacan extends Importer
{ {
$this->add_all_terms($inserted_taxonomy, $category->children); $this->add_all_terms($inserted_taxonomy, $category->children);
} }
}
$start++; $start++;
} }
}
$this->save_in_file("categories", $created_categories); $this->save_in_file("categories", $created_categories);
}
return $start; return $start;
} }
@ -114,10 +105,16 @@ class Old_Tainacan extends Importer
$collections = wp_remote_get($collections_link); $collections = wp_remote_get($collections_link);
$collections_array = $this->verify_process_result($collections); $collections_array = $this->verify_process_result($collections);
$created_collections = [];
if($collections_array) if($collections_array)
{ {
foreach ($collections_array as $collection) list($start, $end) = $this->get_begin_end($collections_array);
if($start === false) return false;
while($start < $end)
{ {
$collection = $collections_array[$start];
$new_collection = new \Tainacan\Entities\Collection(); $new_collection = new \Tainacan\Entities\Collection();
$new_collection->set_name($collection->post_title); $new_collection->set_name($collection->post_title);
$new_collection->set_status('publish'); $new_collection->set_status('publish');
@ -125,11 +122,14 @@ class Old_Tainacan extends Importer
$new_collection = \Tainacan\Repositories\Collections::get_instance()->insert($new_collection); $new_collection = \Tainacan\Repositories\Collections::get_instance()->insert($new_collection);
/*Add old id*/ /*Add old id*/
add_post_meta($new_collection->get_id(), "old_tainacan_collection_id", $collection->ID); $created_collections[] = $collection->ID.",".$new_collection->get_id();
$start++;
} }
$this->save_in_file("collections", $created_collections);
} }
return false; return $start;
} }
public function create_repo_meta() public function create_repo_meta()
@ -168,9 +168,9 @@ class Old_Tainacan extends Importer
$newField->set_field_type('Tainacan\Field_Types\\'.$type); $newField->set_field_type('Tainacan\Field_Types\\'.$type);
if(strcmp($type, "Category") == 0) if(strcmp($type, "Category") == 0)
{ {
$taxonomy_id = $meta->metadata->taxonomy; /*$taxonomy_id = $meta->metadata->taxonomy;
$new_category_id = $this->created_categories[$taxonomy_id]; $new_category_id = $this->created_categories[$taxonomy_id];
$newField->set_field_type_options(['taxonomy_id' => $new_category_id]); $newField->set_field_type_options(['taxonomy_id' => $new_category_id]);*/
} }
$newField->set_collection_id('default'); $newField->set_collection_id('default');
@ -199,6 +199,13 @@ class Old_Tainacan extends Importer
return false; return false;
} }
public function clear()
{
unlink($this->get_id()."_categories.txt");
unlink($this->get_id()."_collections.txt");
return false;
}
/*Aux functions*/ /*Aux functions*/
private function save_in_file($name, $ids) private function save_in_file($name, $ids)
{ {
@ -216,6 +223,25 @@ class Old_Tainacan extends Importer
fclose($fp); fclose($fp);
} }
private function get_begin_end($items)
{
$start = $this->get_start();
$total_items = count($items);
if($start >= $total_items)
{
return [false, false];
}
$end = $this->get_start() + $this->get_items_per_step();
if($end > $total_items)
{
$end = $total_items;
}
return [$start, $end];
}
private function add_all_terms($taxonomy_father, $children, $term_father = null) private function add_all_terms($taxonomy_father, $children, $term_father = null)
{ {
$Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance(); $Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance();

View File

@ -48,15 +48,10 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$url = 'http://localhost/'; $url = 'http://localhost/';
$_SESSION['tainacan_importer'][$id]->set_url($url); $_SESSION['tainacan_importer'][$id]->set_url($url);
// One run for each step while (!$_SESSION['tainacan_importer'][$id]->is_finished())
//Create categories {
$_SESSION['tainacan_importer'][$id]->run();
//Create empty collections
$_SESSION['tainacan_importer'][$id]->run();
//Create repository metadata
$_SESSION['tainacan_importer'][$id]->run(); $_SESSION['tainacan_importer'][$id]->run();
}
$this->assertTrue(true); $this->assertTrue(true);
} }