Old tainacan import (pre-alfa)
This commit is contained in:
parent
b071bf3a00
commit
a7a75cf71b
|
@ -319,8 +319,8 @@ abstract class Importer {
|
||||||
public function process( $start ){
|
public function process( $start ){
|
||||||
|
|
||||||
$end = $start + $this->items_per_step;
|
$end = $start + $this->items_per_step;
|
||||||
|
|
||||||
while ( $start < $end && count( $this->get_processed_items() ) <= $this->get_total_items() ) {
|
while ( $start < $end && count( $this->get_processed_items() ) < $this->get_total_items() ) {
|
||||||
$processed_item = $this->process_item( $start );
|
$processed_item = $this->process_item( $start );
|
||||||
if( $processed_item) {
|
if( $processed_item) {
|
||||||
$this->insert( $start, $processed_item );
|
$this->insert( $start, $processed_item );
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: andre
|
||||||
|
* Date: 03/04/18
|
||||||
|
* Time: 08:39
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Tainacan\Importer;
|
||||||
|
|
||||||
|
|
||||||
|
class Old_Tainacan extends Importer
|
||||||
|
{
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->remove_import_method('file');
|
||||||
|
$this->add_import_method('url');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get the fields of file/url to allow mapping
|
||||||
|
* should return an array
|
||||||
|
*
|
||||||
|
* @return array $fields_source the fields from the source
|
||||||
|
*/
|
||||||
|
public function get_fields()
|
||||||
|
{
|
||||||
|
// TODO: Implement get_fields() method.
|
||||||
|
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||||
|
$json = json_decode($file->fread($file->getSize()), true);
|
||||||
|
|
||||||
|
$item = $json['items'][0]['item'];
|
||||||
|
return array_keys($item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get values for a single item
|
||||||
|
*
|
||||||
|
* @param $index
|
||||||
|
* @return array with field_source's as the index and values for the
|
||||||
|
* item
|
||||||
|
*
|
||||||
|
* Ex: [ 'Field1' => 'value1', 'Field2' => [ 'value2','value3' ]
|
||||||
|
*/
|
||||||
|
public function process_item($index)
|
||||||
|
{
|
||||||
|
$processedItem = [];
|
||||||
|
$headers = $this->get_fields();
|
||||||
|
|
||||||
|
// search the index in the file and get values
|
||||||
|
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||||
|
$json = json_decode($file->fread($file->getSize()), true);
|
||||||
|
|
||||||
|
$values = $json['items'][$index]['item'];
|
||||||
|
|
||||||
|
if( count( $headers ) !== count( $values ) ){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($headers as $header) {
|
||||||
|
$processedItem[ $header ] = $values[ $header ];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $processedItem;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method implemented by the child importer class to return the number of items to be imported
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function get_total_items_from_source()
|
||||||
|
{
|
||||||
|
$file = new \SplFileObject( $this->tmp_file, 'r' );
|
||||||
|
$json = json_decode($file->fread($file->getSize()), true);
|
||||||
|
|
||||||
|
return $this->total_items = $json['found_items'];
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,152 @@
|
||||||
|
{
|
||||||
|
"found_items": "5",
|
||||||
|
"items_per_page": 5,
|
||||||
|
"page": 1,
|
||||||
|
"items": [
|
||||||
|
{
|
||||||
|
"item": {
|
||||||
|
"ID": 1019,
|
||||||
|
"post_author": "1",
|
||||||
|
"post_date": "2018-04-03 13:47:04",
|
||||||
|
"post_date_gmt": "2018-04-03 16:47:04",
|
||||||
|
"post_content": "",
|
||||||
|
"post_title": "Tesla Model X - Teste Webmotors em Las Vegas",
|
||||||
|
"post_excerpt": "",
|
||||||
|
"post_status": "publish",
|
||||||
|
"comment_status": "open",
|
||||||
|
"ping_status": "closed",
|
||||||
|
"post_name": "tesla-model-x-teste-webmotors-em-las-vegas",
|
||||||
|
"post_modified": "2018-04-03 13:47:04",
|
||||||
|
"post_modified_gmt": "2018-04-03 16:47:04",
|
||||||
|
"post_content_filtered": "",
|
||||||
|
"post_parent": 970,
|
||||||
|
"guid": "http:\/\/localhost\/colecao\/colecao-de-testes\/tesla-model-x-teste-webmotors-em-las-vegas",
|
||||||
|
"comment_count": "0",
|
||||||
|
"filter": "raw",
|
||||||
|
"link": [
|
||||||
|
{
|
||||||
|
"rel": "self",
|
||||||
|
"href": "http:\/\/localhost\/wp-json\/tainacan\/v1\/collections\/970\/items\/1019"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"thumbnail": "http:\/\/localhost\/wp-content\/uploads\/2018\/04\/unnamed-file.jpg"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"item": {
|
||||||
|
"ID": 1018,
|
||||||
|
"post_author": "1",
|
||||||
|
"post_date": "2018-04-03 13:44:27",
|
||||||
|
"post_date_gmt": "2018-04-03 16:44:27",
|
||||||
|
"post_content": "",
|
||||||
|
"post_title": "Roads_Autumn_Forests_Trees_7867x5172",
|
||||||
|
"post_excerpt": "",
|
||||||
|
"post_status": "publish",
|
||||||
|
"comment_status": "open",
|
||||||
|
"ping_status": "closed",
|
||||||
|
"post_name": "roads_autumn_forests_trees_7867x5172-2",
|
||||||
|
"post_modified": "2018-04-03 13:44:33",
|
||||||
|
"post_modified_gmt": "2018-04-03 16:44:33",
|
||||||
|
"post_content_filtered": "",
|
||||||
|
"post_parent": 970,
|
||||||
|
"guid": "http:\/\/localhost\/colecao\/colecao-de-testes\/roads_autumn_forests_trees_7867x5172-2",
|
||||||
|
"comment_count": "0",
|
||||||
|
"filter": "raw",
|
||||||
|
"link": [
|
||||||
|
{
|
||||||
|
"rel": "self",
|
||||||
|
"href": "http:\/\/localhost\/wp-json\/tainacan\/v1\/collections\/970\/items\/1018"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"thumbnail": "http:\/\/localhost\/wp-content\/uploads\/2018\/04\/Roads_Autumn_Forests_Trees_7867x5172.jpg"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"item": {
|
||||||
|
"ID": 1017,
|
||||||
|
"post_author": "1",
|
||||||
|
"post_date": "2018-04-03 13:44:26",
|
||||||
|
"post_date_gmt": "2018-04-03 16:44:26",
|
||||||
|
"post_content": "",
|
||||||
|
"post_title": "Times_Square_new_york_usa_city_cities_neon_lights_traffic_crowd_people__o_6000x4000",
|
||||||
|
"post_excerpt": "",
|
||||||
|
"post_status": "publish",
|
||||||
|
"comment_status": "open",
|
||||||
|
"ping_status": "closed",
|
||||||
|
"post_name": "times_square_new_york_usa_city_cities_neon_lights_traffic_crowd_people__o_6000x4000-2",
|
||||||
|
"post_modified": "2018-04-03 13:44:32",
|
||||||
|
"post_modified_gmt": "2018-04-03 16:44:32",
|
||||||
|
"post_content_filtered": "",
|
||||||
|
"post_parent": 970,
|
||||||
|
"guid": "http:\/\/localhost\/colecao\/colecao-de-testes\/times_square_new_york_usa_city_cities_neon_lights_traffic_crowd_people__o_6000x4000-2",
|
||||||
|
"comment_count": "0",
|
||||||
|
"filter": "raw",
|
||||||
|
"link": [
|
||||||
|
{
|
||||||
|
"rel": "self",
|
||||||
|
"href": "http:\/\/localhost\/wp-json\/tainacan\/v1\/collections\/970\/items\/1017"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"thumbnail": "http:\/\/localhost\/wp-content\/uploads\/2018\/04\/Times_Square_new_york_usa_city_cities_neon_lights_traffic_crowd_people__o_6000x4000.jpg"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"item": {
|
||||||
|
"ID": 1002,
|
||||||
|
"post_author": "1",
|
||||||
|
"post_date": "2018-04-03 13:41:51",
|
||||||
|
"post_date_gmt": "2018-04-03 16:41:51",
|
||||||
|
"post_content": "Item 2",
|
||||||
|
"post_title": "Item 2",
|
||||||
|
"post_excerpt": "",
|
||||||
|
"post_status": "publish",
|
||||||
|
"comment_status": "open",
|
||||||
|
"ping_status": "closed",
|
||||||
|
"post_name": "item-2",
|
||||||
|
"post_modified": "2018-04-03 13:43:37",
|
||||||
|
"post_modified_gmt": "2018-04-03 16:43:37",
|
||||||
|
"post_content_filtered": "",
|
||||||
|
"post_parent": 970,
|
||||||
|
"guid": "http:\/\/localhost\/colecao\/colecao-de-testes\/item-2",
|
||||||
|
"comment_count": "0",
|
||||||
|
"filter": "raw",
|
||||||
|
"link": [
|
||||||
|
{
|
||||||
|
"rel": "self",
|
||||||
|
"href": "http:\/\/localhost\/wp-json\/tainacan\/v1\/collections\/970\/items\/1002"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"thumbnail": "http:\/\/localhost\/wp-content\/uploads\/2018\/04\/giau-pass-2880x1800-mountain-pass-fall-sunset-italy-4k-11444.jpg"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"item": {
|
||||||
|
"ID": 978,
|
||||||
|
"post_author": "1",
|
||||||
|
"post_date": "2018-04-03 13:40:49",
|
||||||
|
"post_date_gmt": "2018-04-03 16:40:49",
|
||||||
|
"post_content": "Item 1",
|
||||||
|
"post_title": "Item 1",
|
||||||
|
"post_excerpt": "",
|
||||||
|
"post_status": "publish",
|
||||||
|
"comment_status": "open",
|
||||||
|
"ping_status": "closed",
|
||||||
|
"post_name": "item-1",
|
||||||
|
"post_modified": "2018-04-03 13:41:46",
|
||||||
|
"post_modified_gmt": "2018-04-03 16:41:46",
|
||||||
|
"post_content_filtered": "",
|
||||||
|
"post_parent": 970,
|
||||||
|
"guid": "http:\/\/localhost\/colecao\/colecao-de-testes\/item-1",
|
||||||
|
"comment_count": "0",
|
||||||
|
"filter": "raw",
|
||||||
|
"link": [
|
||||||
|
{
|
||||||
|
"rel": "self",
|
||||||
|
"href": "http:\/\/localhost\/wp-json\/tainacan\/v1\/collections\/970\/items\/978"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"thumbnail": "http:\/\/localhost\/wp-content\/uploads\/2018\/04\/autumn-leaves-2880x1800-hd-4k-11825-1.jpg"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
|
@ -10,10 +10,103 @@ use Tainacan\Importer;
|
||||||
|
|
||||||
class ImporterTests extends TAINACAN_UnitTestCase {
|
class ImporterTests extends TAINACAN_UnitTestCase {
|
||||||
|
|
||||||
|
public function test_intance_old_tainacan()
|
||||||
|
{
|
||||||
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'OtherOldTainacan',
|
||||||
|
'description' => 'Collection from old tainacan',
|
||||||
|
'default_order' => 'DESC',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
$old_tainacan_importer = new Importer\Old_Tainacan();
|
||||||
|
$id = $old_tainacan_importer->get_id();
|
||||||
|
$_SESSION['tainacan_importer'][$id]->set_collection( $collection );
|
||||||
|
$this->assertEquals( $collection->get_id(), $_SESSION['tainacan_importer'][$id]->collection->get_id() );
|
||||||
|
|
||||||
|
/*$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wp-json/tainacan/v1/collections/860/items' );
|
||||||
|
$_SESSION['tainacan_importer'][$id]->process_item(2);*/
|
||||||
|
}
|
||||||
|
public function test_file_old_tainacan () {
|
||||||
|
$Tainacan_Items = \Tainacan\Repositories\Items::getInstance();
|
||||||
|
$Tainacan_Fields = \Tainacan\Repositories\Fields::getInstance();
|
||||||
|
|
||||||
|
$old_tainacan = new Importer\Old_Tainacan();
|
||||||
|
$id = $old_tainacan->get_id();
|
||||||
|
|
||||||
|
$_SESSION['tainacan_importer'][$id]->set_items_per_step(2);
|
||||||
|
|
||||||
|
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]->fetch_from_remote( 'http://localhost/wp-json/tainacan/v1/collections/970/items' );
|
||||||
|
|
||||||
|
// file isset on importer
|
||||||
|
$this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) );
|
||||||
|
|
||||||
|
// count size of old tainacan file
|
||||||
|
$this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() );
|
||||||
|
|
||||||
|
// get fields to mapping
|
||||||
|
$headers = $_SESSION['tainacan_importer'][$id]->get_fields();
|
||||||
|
$this->assertEquals( $headers[5], 'post_title' );
|
||||||
|
|
||||||
|
// inserting the collection
|
||||||
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
|
'collection',
|
||||||
|
array(
|
||||||
|
'name' => 'OtherOldTainacan',
|
||||||
|
'description' => 'Collection from old tainacan',
|
||||||
|
'default_order' => 'DESC',
|
||||||
|
'status' => 'publish'
|
||||||
|
),
|
||||||
|
true
|
||||||
|
);
|
||||||
|
|
||||||
|
// set the importer
|
||||||
|
$_SESSION['tainacan_importer'][$id]->set_collection( $collection );
|
||||||
|
|
||||||
|
// get collection fields to map
|
||||||
|
$fields = $Tainacan_Fields->fetch_by_collection( $collection, [], 'OBJECT' ) ;
|
||||||
|
|
||||||
|
//create a random mapping
|
||||||
|
$map = [];
|
||||||
|
foreach ( $fields as $index => $field ){
|
||||||
|
$map[$field->get_id()] = $headers[$index];
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the mapping
|
||||||
|
$_SESSION['tainacan_importer'][$id]->set_mapping( $map );
|
||||||
|
|
||||||
|
// check is equal
|
||||||
|
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map );
|
||||||
|
|
||||||
|
//execute the process
|
||||||
|
|
||||||
|
$this->assertEquals(2, $_SESSION['tainacan_importer'][$id]->run(), 'first step should import 2 items');
|
||||||
|
$this->assertEquals(4, $_SESSION['tainacan_importer'][$id]->run(), 'second step should import 2 items');
|
||||||
|
$this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'third step should import 1 item');
|
||||||
|
|
||||||
|
$this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing');
|
||||||
|
|
||||||
|
$items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' );
|
||||||
|
|
||||||
|
$this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) );
|
||||||
|
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* @group importer
|
* @group importer
|
||||||
*/
|
*/
|
||||||
public function test_instance_csv () {
|
public function test_instance_csv () {
|
||||||
|
|
||||||
$collection = $this->tainacan_entity_factory->create_entity(
|
$collection = $this->tainacan_entity_factory->create_entity(
|
||||||
'collection',
|
'collection',
|
||||||
array(
|
array(
|
||||||
|
|
Loading…
Reference in New Issue