From a3ce9230a07d1e0c7d97bba03ebdefafcb6a4b8b Mon Sep 17 00:00:00 2001 From: andre2ar Date: Thu, 26 Apr 2018 15:51:49 -0300 Subject: [PATCH] Repository importer: first concept --- src/importer/class-tainacan-importer.php | 74 ++++++++++++++------ src/importer/class-tainacan-old-tainacan.php | 46 ++++++++++-- tests/test-importer.php | 16 ++--- 3 files changed, 103 insertions(+), 33 deletions(-) diff --git a/src/importer/class-tainacan-importer.php b/src/importer/class-tainacan-importer.php index 652cd4b09..5d844e04f 100644 --- a/src/importer/class-tainacan-importer.php +++ b/src/importer/class-tainacan-importer.php @@ -78,6 +78,12 @@ abstract class Importer { 'url' => false, ]; + private $is_repository = false; + + private $steps = []; + + private $current_step = 0; + public function __construct() { if (!session_id()) { @session_start(); @@ -294,7 +300,20 @@ abstract class Importer { } return false; } - + + /** + * Sets importer as repository importer + */ + public function set_repository() + { + $this->is_repository = true; + } + + public function set_steps($steps) + { + $this->steps =$steps; + } + /** * Removes method accepeted by the importer * @@ -424,25 +443,40 @@ abstract class Importer { * run the process */ public function run(){ - - if ( ( !isset($this->collection) || ! $this->collection instanceof Entities\Collection ) && $this->import_structure_and_mapping ) { - $new_collection = new Entities\Collection(); - $new_collection->set_name('New Imported Collection'); - $new_collection->set_status('publish'); - $new_collection->validate(); - $new_collection = Tainacan\Repositories\Collections::get_instance()->insert($new_collection); - - $this->set_collection($new_collection); - - if (!method_exists($this, 'create_fields_and_mapping')) { - throw new Exception('Importers with import_structure_and_mapping true must implement create_fields_and_mapping method'); - } + if($this->is_repository && $this->current_step < count($this->steps)) + { + $process_name = key($this->steps); + $function_name = current($this->steps); + $continue = $this->{$function_name}();//If true still there is stuff to process - $this->create_fields_and_mapping(); - - } - - $this->process( $this->start ); - return sizeof($this->get_processed_items()); + if(!$continue) + { + //Move on to the next step + next($this->steps); + $this->current_step++; + } + } + else + { + if ( ( !isset($this->collection) || ! $this->collection instanceof Entities\Collection ) && $this->import_structure_and_mapping ) { + $new_collection = new Entities\Collection(); + $new_collection->set_name('New Imported Collection'); + $new_collection->set_status('publish'); + $new_collection->validate(); + $new_collection = Tainacan\Repositories\Collections::get_instance()->insert($new_collection); + + $this->set_collection($new_collection); + + if (!method_exists($this, 'create_fields_and_mapping')) { + throw new Exception('Importers with import_structure_and_mapping true must implement create_fields_and_mapping method'); + } + + $this->create_fields_and_mapping(); + + } + + $this->process( $this->start ); + return sizeof($this->get_processed_items()); + } } } \ No newline at end of file diff --git a/src/importer/class-tainacan-old-tainacan.php b/src/importer/class-tainacan-old-tainacan.php index 527d21693..b76fc0be7 100644 --- a/src/importer/class-tainacan-old-tainacan.php +++ b/src/importer/class-tainacan-old-tainacan.php @@ -11,6 +11,7 @@ namespace Tainacan\Importer; class Old_Tainacan extends Importer { + public $avoid = [ 'ID', 'post_author', @@ -32,16 +33,53 @@ class Old_Tainacan extends Importer 'filter', 'link', 'thumbnail' + ], + $steps = [ + 'Creating all taxonomies' => 'create_taxonomies', + 'Create empty collections' => 'create_collections', + 'Create repository metadata' => 'create_repo_meta', + 'Create collections metadata' => 'create_collection_metas', + 'Create collections items' => 'create_collection_items', + 'Setting relationships' => 'set_relationships' ]; - public function __construct($import_structure_and_mapping = false) + public function __construct() { parent::__construct(); - + $this->set_repository(); + $this->set_steps($this->steps); $this->remove_import_method('file'); $this->add_import_method('url'); + } - $this->import_structure_and_mapping = $import_structure_and_mapping; + public function create_taxonomies() + { + return false; + } + + public function create_collections() + { + return false; + } + + public function create_repo_meta() + { + return false; + } + + public function create_collection_metas() + { + return false; + } + + public function create_collection_items() + { + return false; + } + + public function set_relationships() + { + return false; } public function fetch_from_remote( $url ){ @@ -70,7 +108,6 @@ class Old_Tainacan extends Importer } } - if(!empty($link)) { $info = wp_remote_get( $link."/items/?includeMetadata=1" ); @@ -116,7 +153,6 @@ class Old_Tainacan extends Importer $file_content = unserialize($file->fread($file->getSize())); $item = $file_content->items[0]; - $fields = []; //Default meta diff --git a/tests/test-importer.php b/tests/test-importer.php index f043b9d8d..e39231430 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -31,20 +31,20 @@ class ImporterTests extends TAINACAN_UnitTestCase { /*public function test_automapping_old_tainacan() { - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance(); + //$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + //$Tainacan_Fields = \Tainacan\Repositories\Fields::get_instance(); - $old_tainacan = new Importer\Old_Tainacan(true); + $old_tainacan = new Importer\Old_Tainacan(); $id = $old_tainacan->get_id(); $_SESSION['tainacan_importer'][$id]->set_items_per_step(50); - /*if(!copy('./tests/attachment/json_old_tainacan_base.txt', './tests/attachment/json_old_tainacan.txt')) - { - return false; - } +// if(!copy('./tests/attachment/json_old_tainacan_base.txt', './tests/attachment/json_old_tainacan.txt')) +// { +// return false; +// } - $_SESSION['tainacan_importer'][$id]->set_file( './tests/attachment/json_old_tainacan.txt' ); + //$_SESSION['tainacan_importer'][$id]->set_file( './tests/attachment/json_old_tainacan.txt' ); $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/colecao/colecao-to-import/' ); $_SESSION['tainacan_importer'][$id]->run();