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,
];
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());
}
}
}

View File

@ -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

View File

@ -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();