Repository importer: first concept

This commit is contained in:
andre2ar 2018-04-26 15:51:49 -03:00
parent 080dd68272
commit a3ce9230a0
3 changed files with 103 additions and 33 deletions

View File

@ -78,6 +78,12 @@ abstract class Importer {
'url' => false, 'url' => false,
]; ];
private $is_repository = false;
private $steps = [];
private $current_step = 0;
public function __construct() { public function __construct() {
if (!session_id()) { if (!session_id()) {
@session_start(); @session_start();
@ -295,6 +301,19 @@ abstract class Importer {
return false; 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 * Removes method accepeted by the importer
* *
@ -424,7 +443,21 @@ abstract class Importer {
* run the process * run the process
*/ */
public function run(){ public function run(){
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
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 ) { if ( ( !isset($this->collection) || ! $this->collection instanceof Entities\Collection ) && $this->import_structure_and_mapping ) {
$new_collection = new Entities\Collection(); $new_collection = new Entities\Collection();
$new_collection->set_name('New Imported Collection'); $new_collection->set_name('New Imported Collection');
@ -446,3 +479,4 @@ abstract class Importer {
return sizeof($this->get_processed_items()); return sizeof($this->get_processed_items());
} }
} }
}

View File

@ -11,6 +11,7 @@ namespace Tainacan\Importer;
class Old_Tainacan extends Importer class Old_Tainacan extends Importer
{ {
public $avoid = [ public $avoid = [
'ID', 'ID',
'post_author', 'post_author',
@ -32,16 +33,53 @@ class Old_Tainacan extends Importer
'filter', 'filter',
'link', 'link',
'thumbnail' '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(); parent::__construct();
$this->set_repository();
$this->set_steps($this->steps);
$this->remove_import_method('file'); $this->remove_import_method('file');
$this->add_import_method('url'); $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 ){ public function fetch_from_remote( $url ){
@ -70,7 +108,6 @@ class Old_Tainacan extends Importer
} }
} }
if(!empty($link)) if(!empty($link))
{ {
$info = wp_remote_get( $link."/items/?includeMetadata=1" ); $info = wp_remote_get( $link."/items/?includeMetadata=1" );
@ -116,7 +153,6 @@ class Old_Tainacan extends Importer
$file_content = unserialize($file->fread($file->getSize())); $file_content = unserialize($file->fread($file->getSize()));
$item = $file_content->items[0]; $item = $file_content->items[0];
$fields = []; $fields = [];
//Default meta //Default meta

View File

@ -31,20 +31,20 @@ class ImporterTests extends TAINACAN_UnitTestCase {
/*public function test_automapping_old_tainacan() /*public function test_automapping_old_tainacan()
{ {
$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); //$Tainacan_Items = \Tainacan\Repositories\Items::get_instance();
$Tainacan_Fields = \Tainacan\Repositories\Fields::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(); $id = $old_tainacan->get_id();
$_SESSION['tainacan_importer'][$id]->set_items_per_step(50); $_SESSION['tainacan_importer'][$id]->set_items_per_step(50);
/*if(!copy('./tests/attachment/json_old_tainacan_base.txt', './tests/attachment/json_old_tainacan.txt')) // if(!copy('./tests/attachment/json_old_tainacan_base.txt', './tests/attachment/json_old_tainacan.txt'))
{ // {
return false; // 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]->fetch_from_remote( 'http://localhost/colecao/colecao-to-import/' );
$_SESSION['tainacan_importer'][$id]->run(); $_SESSION['tainacan_importer'][$id]->run();