Old tainacan mapping

This commit is contained in:
andre2ar 2018-04-23 14:43:41 -03:00
parent afbd8dd9a1
commit 6bd41ee6d0
2 changed files with 95 additions and 52 deletions

View File

@ -22,6 +22,7 @@ class Old_Tainacan extends Importer
public function fetch_from_remote( $url ){ public function fetch_from_remote( $url ){
$url_json = explode('/colecao/', $url)[0] . "/wp-json/tainacan/v1/collections"; $url_json = explode('/colecao/', $url)[0] . "/wp-json/tainacan/v1/collections";
$all_collections_info = wp_remote_get($url_json); $all_collections_info = wp_remote_get($url_json);
if(isset($all_collections_info['body'])) if(isset($all_collections_info['body']))
@ -54,19 +55,11 @@ class Old_Tainacan extends Importer
{ {
$items_array = json_decode($items['body']); $items_array = json_decode($items['body']);
//Get Metatype $file = fopen( $this->get_id().'.txt', 'w' );
$meta_type = wp_remote_get($link."/metadata"); fwrite( $file, serialize($items_array));
if(isset($meta_type['body'])) fclose( $file );
{
$meta_type_array = json_decode($meta_type['body']);
$file_info['items'] = $items_array;
$file_info['meta'] = $meta_type_array;
$file = fopen( $this->get_id().'.txt', 'w' ); return $this->set_file( $this->get_id().'.txt' );
fwrite( $file, serialize($file_info) );
fclose( $file );
return $this->set_file( $this->get_id().'.txt' );
}
} }
} }
} }
@ -83,13 +76,13 @@ class Old_Tainacan extends Importer
$file = new \SplFileObject( $this->tmp_file, 'r' ); $file = new \SplFileObject( $this->tmp_file, 'r' );
$file_content = unserialize($file->fread($file->getSize())); $file_content = unserialize($file->fread($file->getSize()));
$item = $file_content->items[0];
foreach($file_content['meta'] as $tab) $fields = array_keys((array)$item->item);
foreach ($item->metadata as $metadata)
{ {
foreach($tab->{"tab-properties"} as $meta) $fields[] = ['name' => $metadata->name, 'type' => $metadata->type];
{
$fields[] = ['name' => $meta->name, 'type' => $meta->type];
}
} }
return $fields; return $fields;
@ -113,27 +106,52 @@ class Old_Tainacan extends Importer
$file = new \SplFileObject( $this->tmp_file, 'r' ); $file = new \SplFileObject( $this->tmp_file, 'r' );
$file_content = unserialize($file->fread($file->getSize())); $file_content = unserialize($file->fread($file->getSize()));
/*to fix this*/ $values = $file_content->items[$index];
$values = $file_content['items']->items[$index]->item; foreach ($headers as $header)
{
if( count( $headers ) !== count( $values ) ){ if(isset($header['name']))
return false; {
} $item_index = $this->search_obj_in_array($values->metadata, $header['name']);
if(isset($values->metadata[ $item_index ]->values))
foreach ($headers as $header) { $processedItem[ $header['name'] ] = $values->metadata[ $item_index ]->values[0];
$processedItem[ $header['name'] ] = $values[ $header['name'] ]; else $processedItem[ $header['name'] ] = '';
}
else
{
if($header === 'link')
{
$processedItem[$header] = $values->item->link[0]->href;
}
else
{
$processedItem[$header] = $values->item->{$header};
}
}
} }
return $processedItem; return $processedItem;
} }
function create_fields_and_mapping() { public function search_obj_in_array($array, $name)
{
foreach ($array as $index => $obj)
{
if(strcmp($obj->name, $name) === 0)
{
return $index;
}
}
return false;
}
public function create_fields_and_mapping()
{
$fields_repository = \Tainacan\Repositories\Fields::get_instance(); $fields_repository = \Tainacan\Repositories\Fields::get_instance();
$file_fields = $this->get_fields(); $file_fields = $this->get_fields();
/*$avoid = [ $avoid = [
'ID', /*'ID',
'post_author', 'post_author',
'post_date', 'post_date',
'post_date_gmt', 'post_date_gmt',
@ -152,31 +170,55 @@ class Old_Tainacan extends Importer
'comment_count', 'comment_count',
'filter', 'filter',
'link', 'link',
'thumbnail' 'thumbnail'*/
];*/ ];
foreach($file_fields as $index => $meta_info) foreach($file_fields as $index => $meta_info)
{ {
$newField = new \Tainacan\Entities\Field(); if(is_array($meta_info))
{
$meta_name = $meta_info['name'];
$type = $this->define_type($meta_info['type']);
}
else
{
$meta_name = $meta_info;
$type = 'Text';
}
$newField->set_name($meta_info['name']); if(!in_array($meta_name, $avoid))
{
$newField = new \Tainacan\Entities\Field();
$type = 'Text'; $newField->set_name($meta_name);
$newField->set_field_type('Tainacan\Field_Types\\'.$type); $newField->set_field_type('Tainacan\Field_Types\\'.$type);
$newField->set_collection($this->collection); $newField->set_collection($this->collection);
$newField->validate(); // there is no user input here, so we can be sure it will validate. $newField->validate(); // there is no user input here, so we can be sure it will validate.
$newField = $fields_repository->insert($newField); $newField = $fields_repository->insert($newField);
$mapping[$newField->get_id()] = $file_fields[$index];
$this->set_mapping([ }
$newField->get_id() => $file_fields[$index]
]);
} }
$this->set_mapping($mapping);
} }
public function define_type($type)
{
$type = strtolower($type);
$tainacan_types = ['text', 'textarea', 'numerica', 'date'];
$types_to_work = ['item', 'tree'];
if(in_array($type, $tainacan_types))
{
$type = ucfirst($type);
}else $type = 'Text';
return $type;
}
/** /**
* Method implemented by the child importer class to return the number of items to be imported * Method implemented by the child importer class to return the number of items to be imported
@ -187,6 +229,6 @@ class Old_Tainacan extends Importer
$file = new \SplFileObject( $this->tmp_file, 'r' ); $file = new \SplFileObject( $this->tmp_file, 'r' );
$file_content = unserialize($file->fread($file->getSize())); $file_content = unserialize($file->fread($file->getSize()));
return $this->total_items = $file_content['items']->found_items; return $this->total_items = $file_content->found_items;
} }
} }

View File

@ -37,19 +37,20 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$old_tainacan = new Importer\Old_Tainacan(true); $old_tainacan = new Importer\Old_Tainacan(true);
$id = $old_tainacan->get_id(); $id = $old_tainacan->get_id();
$_SESSION['tainacan_importer'][$id]->set_items_per_step(2); $_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]->run(); $_SESSION['tainacan_importer'][$id]->run();
}*/ }*/
public function test_file_old_tainacan () { /*public function test_file_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();
@ -72,7 +73,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
$_SESSION['tainacan_importer'][$id]->run(); $_SESSION['tainacan_importer'][$id]->run();
/*// count size of old tainacan file
$this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() ); $this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() );
// get fields to mapping // get fields to mapping
@ -119,8 +120,8 @@ class ImporterTests extends TAINACAN_UnitTestCase {
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) );*/ $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) );
} }*/
/** /**
* @group importer * @group importer
*/ */
@ -151,14 +152,14 @@ class ImporterTests extends TAINACAN_UnitTestCase {
public function test_file_csv () { public function test_file_csv () {
$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();
$file_name = 'demosaved.csv';
$csv_importer = new Importer\CSV(); $csv_importer = new Importer\CSV();
$id = $csv_importer->get_id(); $id = $csv_importer->get_id();
$_SESSION['tainacan_importer'][$id]->set_items_per_step(2); $_SESSION['tainacan_importer'][$id]->set_items_per_step(2);
// open the file "demosaved.csv" for writing // open the file "demosaved.csv" for writing
$file = fopen('demosaved.csv', 'w'); $file = fopen($file_name, 'w');
// save the column headers // save the column headers
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5')); fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));
@ -180,7 +181,7 @@ class ImporterTests extends TAINACAN_UnitTestCase {
// Close the file // Close the file
fclose($file); fclose($file);
$_SESSION['tainacan_importer'][$id]->set_file( 'demosaved.csv' ); $_SESSION['tainacan_importer'][$id]->set_file( $file_name );
// file isset on importer // file isset on importer
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );