diff --git a/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php b/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php index 4d88efcf9..c3131186c 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-background-processes-controller.php @@ -248,7 +248,7 @@ class REST_Background_Processes_Controller extends REST_Controller { if ( !isset($body['status']) || ($body['status'] != 'open' && $body['status'] != 'closed') ) { return new \WP_REST_Response([ 'error_message' => __('Status must be informed', 'tainacan' ), - 'session_id' => $session_id + 'session_id' => $id ], 400); } diff --git a/src/classes/class-tainacan-background-process.php b/src/classes/class-tainacan-background-process.php index a6376d733..d4d1cf9e8 100644 --- a/src/classes/class-tainacan-background-process.php +++ b/src/classes/class-tainacan-background-process.php @@ -108,12 +108,13 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $this->table, [ 'data' => maybe_serialize($this->data), + 'progress_value' => 0, 'user_id' => get_current_user_id(), 'priority' => $priority, 'action' => $this->action, 'name' => $this->get_name(), 'queued_on' => date('Y-m-d H:i:s'), - 'status' => 'waiting' + 'status' => 'waiting' ] ); $this->ID = $wpdb->insert_id; @@ -310,7 +311,7 @@ abstract class Background_Process extends \Tainacan_WP_Background_Process { $this->close( $batch->key, 'errored' ); $this->debug('Batch closed due to captured error'); - + $this->unlock_process(); }); $task = $batch; diff --git a/src/classes/importer/class-tainacan-bg-importer.php b/src/classes/importer/class-tainacan-bg-importer.php index ae1956c9b..5ba7521e8 100644 --- a/src/classes/importer/class-tainacan-bg-importer.php +++ b/src/classes/importer/class-tainacan-bg-importer.php @@ -9,9 +9,9 @@ class Background_Importer extends Background_Process { */ protected $action = 'import'; - /** - * @var int - */ + /** + * @var int + */ private $finish_status = 1; public function __construct() { @@ -20,11 +20,11 @@ class Background_Importer extends Background_Process { } /** - * @param $status - */ + * @param $status + */ private function set_finish_status( $status ){ - $this->finish_status = $status; - } + $this->finish_status = $status; + } function task($batch) { @@ -41,10 +41,10 @@ class Background_Importer extends Background_Process { $this->write_log($key, $object->get_log()); $this->write_error_log($key, $object->get_error_log()); - $batch->progress_label = $object->get_progress_label(); - $batch->progress_value = $object->get_progress_value(); + $batch->progress_label = $object->get_progress_label(); + $batch->progress_value = $object->get_progress_value(); - $batch->data = $object->_to_Array(true); + $batch->data = $object->_to_Array(true); if (true === $object->get_abort()) { throw new \Exception('Process aborted by Importer'); diff --git a/src/classes/importer/class-tainacan-importer.php b/src/classes/importer/class-tainacan-importer.php index 32f56d431..355d7b86e 100644 --- a/src/classes/importer/class-tainacan-importer.php +++ b/src/classes/importer/class-tainacan-importer.php @@ -5,14 +5,14 @@ use Tainacan\Entities; abstract class Importer { - /** - * The ID for this importer session - * - * When creating a new importer session via API, an id is returned and used to access this - * importer instance. This is temporarily stored in the database and discarded after the bg process is triggered - * - * @var identifier - */ + /** + * The ID for this importer session + * + * When creating a new importer session via API, an id is returned and used to access this + * importer instance. This is temporarily stored in the database and discarded after the bg process is triggered + * + * @var identifier + */ private $id; /** @@ -136,7 +136,7 @@ abstract class Importer { 'tmp_file' ]; - public function __construct($attributess = array()) { + public function __construct($attributess = array()) { $this->id = uniqid(); @@ -181,41 +181,41 @@ abstract class Importer { ///////////////////// // Getters and setters - /** - * @return string - */ - public function get_id(){ - return $this->id; - } + /** + * @return string + */ + public function get_id(){ + return $this->id; + } - /** - * Set URL - * @param $url string - * @return bool - */ - public function set_url($url) - { - if(!empty($url) && !is_array($url)) - { - $this->url = rtrim(trim($url), "/"); - return true; - } + /** + * Set URL + * @param $url string + * @return bool + */ + public function set_url($url) + { + if(!empty($url) && !is_array($url)) + { + $this->url = rtrim(trim($url), "/"); + return true; + } - return false; - } + return false; + } - /** - * @return string or bool - */ - public function get_url() - { - if(!empty($this->url)) - { - return $this->url; - } + /** + * @return string or bool + */ + public function get_url() + { + if(!empty($this->url)) + { + return $this->url; + } - return false; - } + return false; + } public function get_current_step() { return $this->current_step; @@ -250,12 +250,12 @@ abstract class Importer { } public function get_tmp_file(){ - return $this->tmp_file; - } + return $this->tmp_file; + } public function set_tmp_file($filepath){ - $this->tmp_file = $filepath; - } + $this->tmp_file = $filepath; + } public function get_collections() { return $this->collections; @@ -266,13 +266,13 @@ abstract class Importer { } /** - * Gets the options for this importer, including default values for options - * that were not set yet. - * @return array Importer options - */ - public function get_options() { - return array_merge($this->default_options, $this->options); - } + * Gets the options for this importer, including default values for options + * that were not set yet. + * @return array Importer options + */ + public function get_options() { + return array_merge($this->default_options, $this->options); + } /** * Set the options array @@ -294,13 +294,13 @@ abstract class Importer { } - public function set_steps($steps) { - $this->steps = $steps; - } + public function set_steps($steps) { + $this->steps = $steps; + } public function get_steps() { - return $this->steps; - } + return $this->steps; + } private function get_transients() { @@ -323,33 +323,33 @@ abstract class Importer { // Utilities - /** - * @param $file File to be managed by importer - * @return bool - */ - public function add_file( $file ){ - $new_file = $this->upload_file( $file ); - if ( is_numeric( $new_file ) ) { + /** + * @param $file File to be managed by importer + * @return bool + */ + public function add_file( $file ){ + $new_file = $this->upload_file( $file ); + if ( is_numeric( $new_file ) ) { $this->tmp_file = get_attached_file( $new_file ); return true; - } else { - return false; - } - } + } else { + return false; + } + } - /** - * log the actions from importer - * - * @param $type - * @param $messagelog - */ - public function add_log($message ){ - $this->log[] = $message; - } + /** + * log the actions from importer + * + * @param $type + * @param $messagelog + */ + public function add_log($message ){ + $this->log[] = $message; + } public function add_error_log($message ){ - $this->error_log[] = $message; - } + $this->error_log[] = $message; + } public function add_collection(array $collection) { if (isset($collection['id'])) { @@ -367,17 +367,17 @@ abstract class Importer { } } - /** - * internal function to upload the file - * - * @param $path_file - * @return array $response - */ - private function upload_file( $file_array ){ - //$name = basename( $path_file ); - //$file_array['name'] = $name; - //$file_array['tmp_name'] = $path_file; - //$file_array['size'] = filesize( $path_file ); + /** + * internal function to upload the file + * + * @param $path_file + * @return array $response + */ + private function upload_file( $file_array ){ + //$name = basename( $path_file ); + //$file_array['name'] = $name; + //$file_array['tmp_name'] = $path_file; + //$file_array['size'] = filesize( $path_file ); if ( !function_exists('media_handle_upload') ) { require_once(ABSPATH . "wp-admin" . '/includes/image.php'); @@ -386,36 +386,36 @@ abstract class Importer { } //var_dump(media_handle_sideload( $file_array, 0 )); die; return media_handle_sideload( $file_array, 0 ); - } + } - /** - * get the content form url and creates a file - * - * @param $url - * @return array - */ - public function fetch_from_remote( $url ){ - $tmp = wp_remote_get( $url ); - if( !is_wp_error($tmp) && isset( $tmp['body'] ) ){ - $file = fopen( $this->get_id().'.txt', 'w' ); - fwrite( $file, $tmp['body'] ); - fclose( $file ); - return $this->add_file( $this->get_id().'.txt' ); - } - } + /** + * get the content form url and creates a file + * + * @param $url + * @return array + */ + public function fetch_from_remote( $url ){ + $tmp = wp_remote_get( $url ); + if( !is_wp_error($tmp) && isset( $tmp['body'] ) ){ + $file = fopen( $this->get_id().'.txt', 'w' ); + fwrite( $file, $tmp['body'] ); + fclose( $file ); + return $this->add_file( $this->get_id().'.txt' ); + } + } - /** - * Gets one option from the options array. - * - * Checks if option exist or if it have a default value. Otherwise return an empty string - * - * @param string $key the desired option - * @return mixed the option value, the default value or an empty string - */ - public function get_option($key) { - $options = $this->get_options(); - return isset($options[$key]) ? $options[$key] : ''; - } + /** + * Gets one option from the options array. + * + * Checks if option exist or if it have a default value. Otherwise return an empty string + * + * @param string $key the desired option + * @return mixed the option value, the default value or an empty string + */ + public function get_option($key) { + $options = $this->get_options(); + return isset($options[$key]) ? $options[$key] : ''; + } /** * Adds a new method accepeted by the importer @@ -464,15 +464,15 @@ abstract class Importer { return null; } - public function is_finished() - { - if($this->current_step >= count($this->steps)) - { - return true; - } + public function is_finished() + { + if($this->current_step >= count($this->steps)) + { + return true; + } - return false; - } + return false; + } /** * Cancel Scheduled abortion at the end of run() @@ -628,26 +628,26 @@ abstract class Importer { // Abstract methods - /** - * get the metadata of file/url to allow mapping - * should return an array - * - * Used when $manual_mapping is set to true, to build the mapping interface - * - * @return array $metadata_source the metadata from the source - */ - public function get_source_metadata() {} + /** + * get the metadata of file/url to allow mapping + * should return an array + * + * Used when $manual_mapping is set to true, to build the mapping interface + * + * @return array $metadata_source the metadata from the source + */ + public function get_source_metadata() {} - /** - * get values for a single item - * - * @param $index - * @return array with metadatum_source's as the index and values for the - * item - * - * Ex: [ 'Metadatum1' => 'value1', 'Metadatum2' => [ 'value2','value3' ] - */ - abstract public function process_item( $index, $collection_id ); + /** + * get values for a single item + * + * @param $index + * @return array with metadatum_source's as the index and values for the + * item + * + * Ex: [ 'Metadatum1' => 'value1', 'Metadatum2' => [ 'value2','value3' ] + */ + abstract public function process_item( $index, $collection_id ); @@ -679,11 +679,11 @@ abstract class Importer { //////////////////////////////////////// // Core methods - /** - * process an item from the collections queue - * - */ - public function process_collections() { + /** + * process an item from the collections queue + * + */ + public function process_collections() { $current_collection = $this->get_current_collection(); $collections = $this->get_collections(); @@ -705,7 +705,7 @@ abstract class Importer { } return $this->next_item(); - } + } protected function next_item() { @@ -718,8 +718,8 @@ abstract class Importer { $this->set_current_collection_item($current_collection_item); if( $this->get_transient('change_total') ){ - $collection['total_items'] = $this->get_transient('change_total'); - } + $collection['total_items'] = $this->get_transient('change_total'); + } if ($current_collection_item >= $collection['total_items']) { return $this->next_collection(); @@ -763,23 +763,23 @@ abstract class Importer { return false; } - /** - * insert processed item from source to Tainacan - * - * @param array $processed_item Associative array with metadatum source's as index with - * its value or values - * @param integet $collection_index The index in the $this->collections array of the collection the item is beeing inserted into - * - * @return Tainacan\Entities\Item Item inserted - */ - public function insert( $processed_item, $collection_index ) { + /** + * insert processed item from source to Tainacan + * + * @param array $processed_item Associative array with metadatum source's as index with + * its value or values + * @param integet $collection_index The index in the $this->collections array of the collection the item is beeing inserted into + * + * @return Tainacan\Entities\Item Item inserted + */ + public function insert( $processed_item, $collection_index ) { remove_action( 'post_updated', 'wp_save_post_revision' ); $collections = $this->get_collections(); $collection_definition = isset($collections[$collection_index]) ? $collections[$collection_index] : false; if ( !$collection_definition || !is_array($collection_definition) || !isset($collection_definition['id']) || !isset($collection_definition['mapping']) ) { $this->add_error_log('Collection misconfigured'); - return false; + return false; } $collection = \Tainacan\Repositories\Collections::get_instance()->fetch($collection_definition['id']); @@ -790,96 +790,96 @@ abstract class Importer { } $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); + $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); $Tainacan_Items->disable_logs(); $Tainacan_Metadata->disable_logs(); $Tainacan_Item_Metadata->disable_logs(); - $item = new Entities\Item( ( $this->get_transient('item_id') ) ? $this->get_transient('item_id') : 0 ); + $item = new Entities\Item( ( $this->get_transient('item_id') ) ? $this->get_transient('item_id') : 0 ); $itemMetadataArray = []; - if( is_array( $processed_item ) ){ - foreach ( $processed_item as $metadatum_source => $values ){ - $tainacan_metadatum_id = array_search( $metadatum_source, $collection_definition['mapping'] ); - $metadatum = $Tainacan_Metadata->fetch( $tainacan_metadatum_id ); + if( is_array( $processed_item ) ){ + foreach ( $processed_item as $metadatum_source => $values ){ + $tainacan_metadatum_id = array_search( $metadatum_source, $collection_definition['mapping'] ); + $metadatum = $Tainacan_Metadata->fetch( $tainacan_metadatum_id ); - if( $metadatum instanceof Entities\Metadatum ){ - $singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadatum); // *empty item will be replaced by inserted in the next foreach - $singleItemMetadata->set_value( $values ); - $itemMetadataArray[] = $singleItemMetadata; - } else { + if( $metadatum instanceof Entities\Metadatum ){ + $singleItemMetadata = new Entities\Item_Metadata_Entity( $item, $metadatum); // *empty item will be replaced by inserted in the next foreach + $singleItemMetadata->set_value( $values ); + $itemMetadataArray[] = $singleItemMetadata; + } else { $this->add_error_log('Metadata ' . $metadatum_source . ' not found'); } - } - } + } + } - if( !empty( $itemMetadataArray ) && $collection instanceof Entities\Collection ){ + if( !empty( $itemMetadataArray ) && $collection instanceof Entities\Collection ){ $item->set_collection( $collection ); - if( $item->validate() ){ + if( $item->validate() ){ $insertedItem = $Tainacan_Items->insert( $item ); - } else { - $this->add_error_log( 'Error inserting item' ); - $this->add_error_log( $item->get_errors() ); - return false; - } + } else { + $this->add_error_log( 'Error inserting item' ); + $this->add_error_log( $item->get_errors() ); + return false; + } - foreach ( $itemMetadataArray as $itemMetadata ) { - $itemMetadata->set_item( $insertedItem ); // *I told you + foreach ( $itemMetadataArray as $itemMetadata ) { + $itemMetadata->set_item( $insertedItem ); // *I told you - if( $itemMetadata->validate() ){ + if( $itemMetadata->validate() ){ $result = $Tainacan_Item_Metadata->insert( $itemMetadata ); - } else { - $this->add_error_log('Error saving value for ' . $itemMetadata->get_metadatum()->get_name()); - $this->add_error_log($itemMetadata->get_errors()); - continue; - } + } else { + $this->add_error_log('Error saving value for ' . $itemMetadata->get_metadatum()->get_name()); + $this->add_error_log($itemMetadata->get_errors()); + continue; + } - //if( $result ){ - // $values = ( is_array( $itemMetadata->get_value() ) ) ? implode( PHP_EOL, $itemMetadata->get_value() ) : $itemMetadata->get_value(); - // $this->add_log( 'Item ' . $insertedItem->get_id() . - // ' has inserted the values: ' . $values . ' on metadata: ' . $itemMetadata->get_metadatum()->get_name() ); - //} else { - // $this->add_error_log( 'Item ' . $insertedItem->get_id() . ' has an error' ); - //} + //if( $result ){ + // $values = ( is_array( $itemMetadata->get_value() ) ) ? implode( PHP_EOL, $itemMetadata->get_value() ) : $itemMetadata->get_value(); + // $this->add_log( 'Item ' . $insertedItem->get_id() . + // ' has inserted the values: ' . $values . ' on metadata: ' . $itemMetadata->get_metadatum()->get_name() ); + //} else { + // $this->add_error_log( 'Item ' . $insertedItem->get_id() . ' has an error' ); + //} } $insertedItem->set_status('publish' ); - if($insertedItem->validate()) { + if($insertedItem->validate()) { $insertedItem = $Tainacan_Items->update( $insertedItem ); $this->after_inserted_item( $insertedItem, $collection_index ); - } else { - $this->add_error_log( 'Error publishing Item' ); - $this->add_error_log( $insertedItem->get_errors() ); - return false; + } else { + $this->add_error_log( 'Error publishing Item' ); + $this->add_error_log( $insertedItem->get_errors() ); + return false; } - return $insertedItem; + return $insertedItem; - } else { - $this->add_error_log( 'Collection not set'); - return false; - } + } else { + $this->add_error_log( 'Collection not set'); + return false; + } } /** * allow importers executes process after item is insertes * @param array $insertedItem Associative array with inserted item - * @param integer $collection_index The index in the $this->collections array of the collection the item is beeing inserted into + * @param integer $collection_index The index in the $this->collections array of the collection the item is beeing inserted into * */ public function after_inserted_item($insertedItem, $collection_index){} - /** - * runs one iteration - */ - public function run(){ + /** + * runs one iteration + */ + public function run(){ if ($this->is_finished()) { return false; @@ -911,41 +911,41 @@ abstract class Importer { } return $return; - } + } - /** - * @param $metadata_description - * @param $collection_id - * @return bool - * @throws \Exception - */ - public function create_new_metadata( $metadata_description, $collection_id, $parent_id = null){ - $taxonomy_repo = \Tainacan\Repositories\Taxonomies::get_instance(); - $metadata_repo = \Tainacan\Repositories\Metadata::get_instance(); + /** + * @param $metadata_description + * @param $collection_id + * @return bool + * @throws \Exception + */ + public function create_new_metadata( $metadata_description, $collection_id, $parent_id = null){ + $taxonomy_repo = \Tainacan\Repositories\Taxonomies::get_instance(); + $metadata_repo = \Tainacan\Repositories\Metadata::get_instance(); - if(is_array($metadata_description)) { - $parent_metadata_description = key($metadata_description); - $parent_compound = $this->create_new_metadata($parent_metadata_description, $collection_id); - if($parent_compound == false) return false; - $children_mapping = []; - foreach($metadata_description[$parent_metadata_description] as $children_metadata_description) { - $children_compound = $this->create_new_metadata($children_metadata_description, $collection_id, $parent_compound->get_id()); - if ( $children_compound == false ) - return false; - $children_mapping[$children_compound->get_id()] = $children_metadata_description; - } - return [$parent_compound, $children_mapping]; - } - $properties = array_filter( explode('|', $metadata_description) ); + if(is_array($metadata_description)) { + $parent_metadata_description = key($metadata_description); + $parent_compound = $this->create_new_metadata($parent_metadata_description, $collection_id); + if($parent_compound == false) return false; + $children_mapping = []; + foreach($metadata_description[$parent_metadata_description] as $children_metadata_description) { + $children_compound = $this->create_new_metadata($children_metadata_description, $collection_id, $parent_compound->get_id()); + if ( $children_compound == false ) + return false; + $children_mapping[$children_compound->get_id()] = $children_metadata_description; + } + return [$parent_compound, $children_mapping]; + } + $properties = array_filter( explode('|', $metadata_description) ); - if( is_array($properties) && count($properties) < 2 ){ - $properties[1] = 'text'; - } else if( !$properties ){ - return false; - } + if( is_array($properties) && count($properties) < 2 ){ + $properties[1] = 'text'; + } else if( !$properties ){ + return false; + } - $name = $properties[0]; - $type = $properties[1]; + $name = $properties[0]; + $type = $properties[1]; $supported_types = \Tainacan\Repositories\Metadata::get_instance()->fetch_metadata_types('NAME'); $supported_types = array_map('strtolower', $supported_types); @@ -956,89 +956,89 @@ abstract class Importer { $type = 'text'; } - $newMetadatum = new Entities\Metadatum(); - $newMetadatum->set_name($name); + $newMetadatum = new Entities\Metadatum(); + $newMetadatum->set_name($name); - $type = ucfirst($type); - $newMetadatum->set_metadata_type('Tainacan\Metadata_Types\\'.$type); - $newMetadatum->set_collection_id( (isset($collection_id)) ? $collection_id : 'default'); - $newMetadatum->set_status('auto-draft'); - if($newMetadatum->validate()) { - $newMetadatum = $metadata_repo->insert( $newMetadatum ); - } else { - $this->add_log('Error creating metadata ' . $name . ' in collection ' . $collection_id); - $this->add_log($newMetadatum->get_errors()); - return false; - } - $newMetadatum->set_status('publish'); + $type = ucfirst($type); + $newMetadatum->set_metadata_type('Tainacan\Metadata_Types\\'.$type); + $newMetadatum->set_collection_id( (isset($collection_id)) ? $collection_id : 'default'); + $newMetadatum->set_status('auto-draft'); + if($newMetadatum->validate()) { + $newMetadatum = $metadata_repo->insert( $newMetadatum ); + } else { + $this->add_log('Error creating metadata ' . $name . ' in collection ' . $collection_id); + $this->add_log($newMetadatum->get_errors()); + return false; + } + $newMetadatum->set_status('publish'); - if( strcmp(strtolower($type), "taxonomy") === 0 ){ - $taxonomy = new Entities\Taxonomy(); - $taxonomy->set_name($name); - $taxonomy->set_status('publish'); - $taxonomy->set_allow_insert('yes'); + if( strcmp(strtolower($type), "taxonomy") === 0 ){ + $taxonomy = new Entities\Taxonomy(); + $taxonomy->set_name($name); + $taxonomy->set_status('publish'); + $taxonomy->set_allow_insert('yes'); - if( $taxonomy->validate() ){ - $inserted_tax = $taxonomy_repo->insert( $taxonomy ); - if(is_array($properties) && in_array( 'multiple', $properties) ){ - $newMetadatum->set_metadata_type_options([ - 'taxonomy_id' => $inserted_tax->get_id(), - 'allow_new_terms' => 'yes', - 'input_type' => 'tainacan-taxonomy-checkbox' - ]); - } else { - $newMetadatum->set_metadata_type_options([ - 'taxonomy_id' => $inserted_tax->get_id(), - 'allow_new_terms' => 'no', - 'input_type' => 'tainacan-taxonomy-radio' - ]); - } - } - } + if( $taxonomy->validate() ){ + $inserted_tax = $taxonomy_repo->insert( $taxonomy ); + if(is_array($properties) && in_array( 'multiple', $properties) ){ + $newMetadatum->set_metadata_type_options([ + 'taxonomy_id' => $inserted_tax->get_id(), + 'allow_new_terms' => 'yes', + 'input_type' => 'tainacan-taxonomy-checkbox' + ]); + } else { + $newMetadatum->set_metadata_type_options([ + 'taxonomy_id' => $inserted_tax->get_id(), + 'allow_new_terms' => 'no', + 'input_type' => 'tainacan-taxonomy-radio' + ]); + } + } + } - /*Properties of metadatum*/ - if( is_array($properties) && in_array( 'required', $properties)){ - $newMetadatum->set_required('yes'); - } + /*Properties of metadatum*/ + if( is_array($properties) && in_array( 'required', $properties)){ + $newMetadatum->set_required('yes'); + } - if(is_array($properties) && in_array( 'multiple', $properties) ){ - $newMetadatum->set_multiple('yes'); - } + if(is_array($properties) && in_array( 'multiple', $properties) ){ + $newMetadatum->set_multiple('yes'); + } - if( is_array($properties) && in_array( 'display_yes', $properties) ){ - $newMetadatum->set_display('yes'); - } else if(is_array($properties) && in_array( 'display_no', $properties) ){ - $newMetadatum->set_display('no'); - } else if(is_array($properties) && in_array( 'display_never', $properties) ){ - $newMetadatum->set_display('never'); - } + if( is_array($properties) && in_array( 'display_yes', $properties) ){ + $newMetadatum->set_display('yes'); + } else if(is_array($properties) && in_array( 'display_no', $properties) ){ + $newMetadatum->set_display('no'); + } else if(is_array($properties) && in_array( 'display_never', $properties) ){ + $newMetadatum->set_display('never'); + } - if( is_array($properties) && in_array( 'status_public', $properties) ){ - $newMetadatum->set_status('publish'); - } else if( is_array($properties) && in_array( 'status_private', $properties) ){ - $newMetadatum->set_status('private'); - } + if( is_array($properties) && in_array( 'status_public', $properties) ){ + $newMetadatum->set_status('publish'); + } else if( is_array($properties) && in_array( 'status_private', $properties) ){ + $newMetadatum->set_status('private'); + } - if( is_array($properties) && in_array( 'collection_key_yes', $properties) ){ - $newMetadatum->set_collection_key('yes'); - } else if( is_array($properties) && in_array( 'collection_key_no', $properties) ){ - $newMetadatum->set_collection_key('no'); - } + if( is_array($properties) && in_array( 'collection_key_yes', $properties) ){ + $newMetadatum->set_collection_key('yes'); + } else if( is_array($properties) && in_array( 'collection_key_no', $properties) ){ + $newMetadatum->set_collection_key('no'); + } - if(isset($parent_id) && $parent_id != null) { - $newMetadatum->set_parent($parent_id); - } + if(isset($parent_id) && $parent_id != null) { + $newMetadatum->set_parent($parent_id); + } - if($newMetadatum->validate()){ - $inserted_metadata = $metadata_repo->insert( $newMetadatum ); + if($newMetadatum->validate()){ + $inserted_metadata = $metadata_repo->insert( $newMetadatum ); - $this->add_log('Metadata created: ' . $inserted_metadata->get_name()); - return $inserted_metadata; - } else { - $this->add_log('Error creating metadata ' . $name . ' in collection ' . $collection_id); - $this->add_log($newMetadatum->get_errors()); + $this->add_log('Metadata created: ' . $inserted_metadata->get_name()); + return $inserted_metadata; + } else { + $this->add_log('Error creating metadata ' . $name . ' in collection ' . $collection_id); + $this->add_log($newMetadatum->get_errors()); - return false; - } - } + return false; + } + } } diff --git a/src/classes/libs/wp-background-process.php b/src/classes/libs/wp-background-process.php index 24bcf908f..10ba39134 100644 --- a/src/classes/libs/wp-background-process.php +++ b/src/classes/libs/wp-background-process.php @@ -169,6 +169,7 @@ session_write_close(); if ( $this->is_process_running() ) { + $this->debug('process already running. To die...'); // Background process already running. wp_die(); } @@ -219,6 +220,7 @@ * in a background process. */ protected function is_process_running() { + $this->debug('Checking if process ' . $this->identifier . ' is running:'); if ( get_site_transient( $this->identifier . '_process_lock' ) ) { // Process already running. $this->debug('process already running'); @@ -236,10 +238,10 @@ * defined in the time_exceeded() method. */ protected function lock_process() { - $this->debug('locking process'); + $this->debug('locking process: ' . $this->identifier); $this->start_time = time(); // Set start time of current process. - - $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : 60; // 1 minute + $max_execution_time = ini_get('max_execution_time'); + $lock_duration = ( property_exists( $this, 'queue_lock_time' ) ) ? $this->queue_lock_time : ( empty($max_execution_time) ? 60 : $max_execution_time ); // 1 minute $lock_duration = apply_filters( $this->identifier . '_queue_lock_time', $lock_duration ); set_site_transient( $this->identifier . '_process_lock', microtime(), $lock_duration ); diff --git a/tests/test-importer.php b/tests/test-importer.php index d02c799f4..928243e9a 100644 --- a/tests/test-importer.php +++ b/tests/test-importer.php @@ -11,211 +11,211 @@ use Tainacan\Importer; 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 - ); + /*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() ); - }*/ + $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() ); + }*/ - public function test_automapping_old_tainacan() - { - //$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - //$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + public function test_automapping_old_tainacan() + { + //$Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + //$Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $old_tainacan = new Importer\Old_Tainacan(); - $id = $old_tainacan->get_id(); + $old_tainacan = new Importer\Old_Tainacan(); + $id = $old_tainacan->get_id(); - //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' ); - $url_repository = 'http://localhost/wordpress_tainacan/'; - $url_repository = ''; - if( $url_repository !== '' ){ - $_SESSION['tainacan_importer'][$id]->set_url($url_repository); + //$_SESSION['tainacan_importer'][$id]->set_file( './tests/attachment/json_old_tainacan.txt' ); + $url_repository = 'http://localhost/wordpress_tainacan/'; + $url_repository = ''; + if( $url_repository !== '' ){ + $_SESSION['tainacan_importer'][$id]->set_url($url_repository); - while (!$_SESSION['tainacan_importer'][$id]->is_finished()) - { - $_SESSION['tainacan_importer'][$id]->run(); - } + while (!$_SESSION['tainacan_importer'][$id]->is_finished()) + { + $_SESSION['tainacan_importer'][$id]->run(); + } - $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); - $collections = $Tainacan_Collections->fetch([], 'OBJECT'); - $this->assertEquals(3, count( $collections ), 'total collection in this url does not match'); + $Tainacan_Collections = \Tainacan\Repositories\Collections::get_instance(); + $collections = $Tainacan_Collections->fetch([], 'OBJECT'); + $this->assertEquals(3, count( $collections ), 'total collection in this url does not match'); - $this->assertTrue(true); - } - $this->assertTrue($old_tainacan instanceof \Tainacan\Importer\Old_Tainacan); - } + $this->assertTrue(true); + } + $this->assertTrue($old_tainacan instanceof \Tainacan\Importer\Old_Tainacan); + } - /*public function test_file_old_tainacan () { - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + /*public function test_file_old_tainacan () { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $old_tainacan = new Importer\Old_Tainacan(); - $id = $old_tainacan->get_id(); + $old_tainacan = new Importer\Old_Tainacan(); + $id = $old_tainacan->get_id(); - $_SESSION['tainacan_importer'][$id]->set_items_per_step(2); + $_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; - } + 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/wp-json/tainacan/v1/collections/970/items' ); - $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/colecao/colecao-to-import/' ); + //$_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wp-json/tainacan/v1/collections/970/items' ); + $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/colecao/colecao-to-import/' ); - // file isset on importer - $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); + // file isset on importer + $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); - $_SESSION['tainacan_importer'][$id]->run(); + $_SESSION['tainacan_importer'][$id]->run(); - $this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() ); + $this->assertEquals( 5, $_SESSION['tainacan_importer'][$id]->get_total_items() ); - // get metadata to mapping - $headers = $_SESSION['tainacan_importer'][$id]->get_metadata(); - $this->assertEquals( $headers[5], 'post_title' ); + // get metadata to mapping + $headers = $_SESSION['tainacan_importer'][$id]->get_metadata(); + $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 - ); + // 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 ); + // set the importer + $_SESSION['tainacan_importer'][$id]->set_collection( $collection ); - // get collection metadata to map - $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; + // get collection metadata to map + $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; - //create a random mapping - $map = []; - foreach ( $metadata as $index => $metadatum ){ - $map[$metadatum->get_id()] = $headers[$index]; - } + //create a random mapping + $map = []; + foreach ( $metadata as $index => $metadatum ){ + $map[$metadatum->get_id()] = $headers[$index]; + } - // set the mapping - $_SESSION['tainacan_importer'][$id]->set_mapping( $map ); + // set the mapping + $_SESSION['tainacan_importer'][$id]->set_mapping( $map ); - // check is equal - $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map ); + // check is equal + $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_mapping(), $map ); - //execute the process + //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(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'); + $this->assertEquals(5, $_SESSION['tainacan_importer'][$id]->run(), 'if call run again after finish, do nothing'); - $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); + $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); - $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) ); - }*/ - - /** - * @group importer - */ - public function test_file_csv () { - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $file_name = 'demosaved.csv'; - global $Tainacan_Importer_Handler; - $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); - $Tainacan_Importer_Handler->save_importer_instance($csv_importer); - $id = $csv_importer->get_id(); + $this->assertEquals( $_SESSION['tainacan_importer'][$id]->get_total_items(), count( $items ) ); + }*/ + + /** + * @group importer + */ + public function test_file_csv () { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $file_name = 'demosaved.csv'; + global $Tainacan_Importer_Handler; + $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); + $Tainacan_Importer_Handler->save_importer_instance($csv_importer); + $id = $csv_importer->get_id(); - // open the file "demosaved.csv" for writing - $file = fopen($file_name, 'w'); + // open the file "demosaved.csv" for writing + $file = fopen($file_name, 'w'); - // save the column headers - fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5')); + // save the column headers + fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5')); - // Sample data - $data = array( - array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'), - array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'), - array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'), - array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'), - array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55') - ); + // Sample data + $data = array( + array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'), + array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'), + array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'), + array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'), + array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55') + ); - // save each row of the data - foreach ($data as $row){ - fputcsv($file, $row); - } + // save each row of the data + foreach ($data as $row){ + fputcsv($file, $row); + } - // Close the file - fclose($file); + // Close the file + fclose($file); - $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); - $importer_instance->set_tmp_file( $file_name ); + $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); + $importer_instance->set_tmp_file( $file_name ); - // file isset on importer - $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); + // file isset on importer + $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); - // count size of csv - $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); + // count size of csv + $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); - // get metadata to mapping - $headers = $importer_instance->get_source_metadata(); - $this->assertEquals( $headers[4], 'Column 5' ); + // get metadata to mapping + $headers = $importer_instance->get_source_metadata(); + $this->assertEquals( $headers[4], 'Column 5' ); - // inserting the collection - $collection = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'Other', - 'description' => 'adasdasdsa', - 'default_order' => 'DESC', - 'status' => 'publish' - ), - true - ); + // inserting the collection + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Other', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC', + 'status' => 'publish' + ), + true + ); $collection_definition = [ 'id' => $collection->get_id(), 'total_items' => $importer_instance->get_source_number_of_items(), ]; - // get collection metadata to map - $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; + // get collection metadata to map + $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; - //create a random mapping - $map = []; - foreach ( $metadata as $index => $metadatum ){ - $map[$metadatum->get_id()] = $headers[$index]; - } + //create a random mapping + $map = []; + foreach ( $metadata as $index => $metadatum ){ + $map[$metadatum->get_id()] = $headers[$index]; + } $collection_definition['mapping'] = $map; - // add the collection - $importer_instance->add_collection( $collection_definition ); + // add the collection + $importer_instance->add_collection( $collection_definition ); - //execute the process + //execute the process $this->assertEquals(1, $importer_instance->run(), 'first step should import 1 item'); $this->assertEquals(2, $importer_instance->run(), 'second step should import 2 items'); $this->assertEquals(3, $importer_instance->run(), 'third step should import 3 items'); @@ -223,94 +223,94 @@ class ImporterTests extends TAINACAN_UnitTestCase { $this->assertEquals(false, $importer_instance->run(), '5 items and return false because its finished'); $this->assertEquals(false, $importer_instance->run(), 'if call run again after finish, do nothing'); - $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); + $items = $Tainacan_Items->fetch( [], $collection, 'OBJECT' ); - $this->assertEquals( $importer_instance->get_source_number_of_items(), count( $items ) ); - } + $this->assertEquals( $importer_instance->get_source_number_of_items(), count( $items ) ); + } - /** - * @group importer - */ - /* + /** + * @group importer + */ + /* public function test_fetch_file(){ - $csv_importer = new Importer\CSV(); - $id = $csv_importer->get_id(); - $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' ); + $csv_importer = new Importer\CSV(); + $id = $csv_importer->get_id(); + $_SESSION['tainacan_importer'][$id]->fetch_from_remote( 'http://localhost/wordpress-test/wp-json' ); - if(!isset( $_SESSION['tainacan_importer'][$id]->tmp_file )){ - #TODO: Remove dependence of web server (see fetch_from_remote) - $this->markTestSkipped('This test need a apache installation available.'); - } + if(!isset( $_SESSION['tainacan_importer'][$id]->tmp_file )){ + #TODO: Remove dependence of web server (see fetch_from_remote) + $this->markTestSkipped('This test need a apache installation available.'); + } - $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); - } - */ - - /** - * @group importer_created - */ - public function test_file_csv_multiple () { - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $file_name = 'demosaved.csv'; - global $Tainacan_Importer_Handler; - $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); - $Tainacan_Importer_Handler->save_importer_instance($csv_importer); - $id = $csv_importer->get_id(); - $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); + $this->assertTrue( isset( $_SESSION['tainacan_importer'][$id]->tmp_file ) ); + } + */ + + /** + * @group importer_created + */ + public function test_file_csv_multiple () { + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $file_name = 'demosaved.csv'; + global $Tainacan_Importer_Handler; + $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); + $Tainacan_Importer_Handler->save_importer_instance($csv_importer); + $id = $csv_importer->get_id(); + $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); - // open the file "demosaved.csv" for writing - $file = fopen($file_name, 'w'); + // open the file "demosaved.csv" for writing + $file = fopen($file_name, 'w'); - // save the column headers - fputcsv($file, array('author', 'Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5')); + // save the column headers + fputcsv($file, array('author', 'Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5')); - // Sample data - $data = array( - array(get_current_user_id(), 'Data 11', 'Data 12', 'Data 13||TESTE', 'Data 14', 'Data 15>>DATA 151'), - array(get_current_user_id(), 'Data 21', 'Data 22', 'this - is - having - multiple - lines', 'Data 24', 'Data 25'), - array(get_current_user_id(), 'Data 31', 'Data 32', utf8_decode( 'Data 33||Rééço' ), 'Data 34', 'Data 35'), - array(get_current_user_id(), 'Data 41', 'Data 42', 'Data 43||limbbo', 'Data 44', 'Data 45'), - array(get_current_user_id(), 'Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55>>DATA551') - ); + // Sample data + $data = array( + array(get_current_user_id(), 'Data 11', 'Data 12', 'Data 13||TESTE', 'Data 14', 'Data 15>>DATA 151'), + array(get_current_user_id(), 'Data 21', 'Data 22', 'this + is + having + multiple + lines', 'Data 24', 'Data 25'), + array(get_current_user_id(), 'Data 31', 'Data 32', utf8_decode( 'Data 33||Rééço' ), 'Data 34', 'Data 35'), + array(get_current_user_id(), 'Data 41', 'Data 42', 'Data 43||limbbo', 'Data 44', 'Data 45'), + array(get_current_user_id(), 'Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55>>DATA551') + ); - // save each row of the data - foreach ($data as $row){ - fputcsv($file, $row); - } + // save each row of the data + foreach ($data as $row){ + fputcsv($file, $row); + } - // Close the file - fclose($file); + // Close the file + fclose($file); - $importer_instance->set_tmp_file( $file_name ); + $importer_instance->set_tmp_file( $file_name ); - // file isset on importer - $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); + // file isset on importer + $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); - // count size of csv - $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); + // count size of csv + $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); - // get metadata to mapping - $headers = $importer_instance->get_source_metadata(); - $this->assertEquals( $headers[5], 'Column 5' ); + // get metadata to mapping + $headers = $importer_instance->get_source_metadata(); + $this->assertEquals( $headers[5], 'Column 5' ); - // inserting the collection - $collection = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'Other', - 'description' => 'adasdasdsa', - 'default_order' => 'DESC', - 'status' => 'publish' - ), - true - ); + // inserting the collection + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Other', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC', + 'status' => 'publish' + ), + true + ); - $metadatum = $this->tainacan_entity_factory->create_entity( + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Data multiplo', @@ -321,9 +321,9 @@ class ImporterTests extends TAINACAN_UnitTestCase { 'multiple' => 'yes' ), true - ); - - $metadatum = $this->tainacan_entity_factory->create_entity( + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Texto simples', @@ -334,28 +334,28 @@ class ImporterTests extends TAINACAN_UnitTestCase { 'multiple' => 'no' ), true - ); - - $taxonomy = $this->tainacan_entity_factory->create_entity( - 'taxonomy', - array( - 'name' => 'genero', + ); + + $taxonomy = $this->tainacan_entity_factory->create_entity( + 'taxonomy', + array( + 'name' => 'genero', 'status' => 'publish' - ), - true - ); - - $metadata_taxonomy =$this->tainacan_entity_factory->create_entity( - 'metadatum', - array( - 'name' => 'taxonomia', - 'collection_id' => $collection->get_id(), - 'metadata_type_options' => ['taxonomy_id' => $taxonomy->get_id(), 'allow_new_terms' => 'yes' ], - 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', - 'status' => 'publish' - ), - true - ); + ), + true + ); + + $metadata_taxonomy =$this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'taxonomia', + 'collection_id' => $collection->get_id(), + 'metadata_type_options' => ['taxonomy_id' => $taxonomy->get_id(), 'allow_new_terms' => 'yes' ], + 'metadata_type' => 'Tainacan\Metadata_Types\Taxonomy', + 'status' => 'publish' + ), + true + ); $collection_definition = [ @@ -363,32 +363,32 @@ class ImporterTests extends TAINACAN_UnitTestCase { 'total_items' => $importer_instance->get_source_number_of_items(), ]; - // get collection metadata to map - $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; + // get collection metadata to map + $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; - //create a random mapping - $map = []; - $index = 1; - foreach ( $metadata as $metadatum ){ - if ($metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Core_Author') { - $map[$metadatum->get_id()] = $headers[0]; - continue; - } - if( $index === 1){ - $map['create_metadata'] = $headers[$index]; - } else { - $map[$metadatum->get_id()] = $headers[$index]; - } - $index++; - } + //create a random mapping + $map = []; + $index = 1; + foreach ( $metadata as $metadatum ){ + if ($metadatum->get_metadata_type() == 'Tainacan\Metadata_Types\Core_Author') { + $map[$metadatum->get_id()] = $headers[0]; + continue; + } + if( $index === 1){ + $map['create_metadata'] = $headers[$index]; + } else { + $map[$metadatum->get_id()] = $headers[$index]; + } + $index++; + } $collection_definition['mapping'] = $map; - // add the collection - $importer_instance->add_collection( $collection_definition ); - $importer_instance->set_option('encode','iso88591'); + // add the collection + $importer_instance->add_collection( $collection_definition ); + $importer_instance->set_option('encode','iso88591'); - //execute the process + //execute the process $this->assertEquals(1, $importer_instance->run(), 'first step should import 1 item'); $this->assertEquals(2, $importer_instance->run(), 'second step should import 2 items'); $this->assertEquals(3, $importer_instance->run(), 'third step should import 3 items'); @@ -396,91 +396,91 @@ class ImporterTests extends TAINACAN_UnitTestCase { $this->assertEquals(false, $importer_instance->run(), '5 items and return false because its finished'); $this->assertEquals(false, $importer_instance->run(), 'if call run again after finish, do nothing'); - $items = $Tainacan_Items->fetch( ['order'=> 'ASC','orderby' => 'date'], $collection, 'OBJECT' ); + $items = $Tainacan_Items->fetch( ['order'=> 'ASC','orderby' => 'date'], $collection, 'OBJECT' ); - $this->assertEquals( $importer_instance->get_source_number_of_items(), count( $items ) ); - } + $this->assertEquals( $importer_instance->get_source_number_of_items(), count( $items ) ); + } - /** - * @group xis - */ - public function test_get_registered() { - global $Tainacan_Importer_Handler; - $csv_importer = new Importer\CSV(); - $registered = $Tainacan_Importer_Handler->get_importer_by_object($csv_importer); - $this->assertEquals('csv', $registered['slug']); - $this->assertEquals('CSV', $registered['name']); - } + /** + * @group xis + */ + public function test_get_registered() { + global $Tainacan_Importer_Handler; + $csv_importer = new Importer\CSV(); + $registered = $Tainacan_Importer_Handler->get_importer_by_object($csv_importer); + $this->assertEquals('csv', $registered['slug']); + $this->assertEquals('CSV', $registered['name']); + } - /** - * @group importer_csv_special_fields - */ - public function test_special_fields(){ - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $file_name = 'demosaved.csv'; - global $Tainacan_Importer_Handler; - $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); - $Tainacan_Importer_Handler->save_importer_instance($csv_importer); - $id = $csv_importer->get_id(); - $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); + /** + * @group importer_csv_special_fields + */ + public function test_special_fields(){ + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $file_name = 'demosaved.csv'; + global $Tainacan_Importer_Handler; + $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); + $Tainacan_Importer_Handler->save_importer_instance($csv_importer); + $id = $csv_importer->get_id(); + $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); - // open the file "demosaved.csv" for writing - $file = fopen($file_name, 'w'); + // open the file "demosaved.csv" for writing + $file = fopen($file_name, 'w'); - // save the column headers - fputcsv($file, array('Column 1', 'special_ID', 'Column 3', 'special_attachments', 'special_document')); + // save the column headers + fputcsv($file, array('Column 1', 'special_ID', 'Column 3', 'special_attachments', 'special_document')); - // Sample data - $data = array( - array('Data 11', '456', 'Data 13||TESTE', 'https://www.w3schools.com/w3css/img_lights.jpg', 'text:Vou dormir mais tarde'), - array('Data 21', '457', 'Data 23', 'photos/unnamed.jpg', 'url:https://www.youtube.com/watch?v=V8dpmD4HG5s&start_radio=1&list=RDEMZS6OrHEAut8dOA38mVtVpg'), - array( - 'Data 31', - '458', - utf8_decode( 'Data 33||Rééço' ), - 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg||https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/58f72418-b5ee-4765-8e80-e463623a921d/01-httparchive-opt-small.png', - 'file:https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg'), - array('Data 41', '459', 'Data 43||limbbo', 'photos/SamplePNGImage_100kbmb.png||audios/SampleAudio_0.4mb.mp3', 'url:http://www.pdf995.com/samples/pdf.pdf'), - array('Data 51', '500', 'Data 53', 'http://techslides.com/demos/samples/sample.mp4', '') - ); + // Sample data + $data = array( + array('Data 11', '456', 'Data 13||TESTE', 'https://www.w3schools.com/w3css/img_lights.jpg', 'text:Vou dormir mais tarde'), + array('Data 21', '457', 'Data 23', 'photos/unnamed.jpg', 'url:https://www.youtube.com/watch?v=V8dpmD4HG5s&start_radio=1&list=RDEMZS6OrHEAut8dOA38mVtVpg'), + array( + 'Data 31', + '458', + utf8_decode( 'Data 33||Rééço' ), + 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg||https://cloud.netlifyusercontent.com/assets/344dbf88-fdf9-42bb-adb4-46f01eedd629/58f72418-b5ee-4765-8e80-e463623a921d/01-httparchive-opt-small.png', + 'file:https://images.pexels.com/photos/248797/pexels-photo-248797.jpeg'), + array('Data 41', '459', 'Data 43||limbbo', 'photos/SamplePNGImage_100kbmb.png||audios/SampleAudio_0.4mb.mp3', 'url:http://www.pdf995.com/samples/pdf.pdf'), + array('Data 51', '500', 'Data 53', 'http://techslides.com/demos/samples/sample.mp4', '') + ); - // save each row of the data - foreach ($data as $row){ - fputcsv($file, $row); - } + // save each row of the data + foreach ($data as $row){ + fputcsv($file, $row); + } - // Close the file - fclose($file); + // Close the file + fclose($file); - $importer_instance->set_tmp_file( $file_name ); + $importer_instance->set_tmp_file( $file_name ); - // file isset on importer - $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); + // file isset on importer + $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); - // count total items - $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); + // count total items + $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); - // get metadata to mapping AVOIDING special fields - $headers = $importer_instance->get_source_metadata(); - $this->assertEquals( $headers[1], 'Column 3' ); + // get metadata to mapping AVOIDING special fields + $headers = $importer_instance->get_source_metadata(); + $this->assertEquals( $headers[1], 'Column 3' ); - $this->assertEquals( $importer_instance->get_option('attachment_index'), 3 ); - $this->assertEquals( $importer_instance->get_option('document_index'), 4 ); + $this->assertEquals( $importer_instance->get_option('attachment_index'), 3 ); + $this->assertEquals( $importer_instance->get_option('document_index'), 4 ); - // inserting the collection - $collection = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'Other', - 'description' => 'adasdasdsa', - 'default_order' => 'DESC', - 'status' => 'publish' - ), - true - ); + // inserting the collection + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Other', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC', + 'status' => 'publish' + ), + true + ); - $metadatum = $this->tainacan_entity_factory->create_entity( + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Data multiplo', @@ -491,9 +491,9 @@ class ImporterTests extends TAINACAN_UnitTestCase { 'multiple' => 'yes' ), true - ); - - $metadatum = $this->tainacan_entity_factory->create_entity( + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( 'metadatum', array( 'name' => 'Texto simples', @@ -509,177 +509,177 @@ class ImporterTests extends TAINACAN_UnitTestCase { $collection_definition = [ 'id' => $collection->get_id(), 'total_items' => $importer_instance->get_source_number_of_items(), - ]; - - // get collection metadata to map - $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; - - //create a random mapping - $map = []; - foreach ( $metadata as $index => $metadatum ){ - if(isset($headers[$index])) - $map[$metadatum->get_id()] = $headers[$index]; - } - - $collection_definition['mapping'] = $map; - - // add the collection - $importer_instance->add_collection( $collection_definition ); - $importer_instance->set_option('encode','iso88591'); - - while($importer_instance->run()) { - continue; - } - - $items = $Tainacan_Items->fetch( ['order'=> 'ASC', 'orderby' => 'ID'], $collection, 'OBJECT' ); - - $this->assertEquals( $importer_instance->get_source_number_of_items(), count( $items ) ); - - // test rows - $document_id = $items[0]->get_document(); - $this->assertFalse( is_numeric($document_id) ); - - $attachments = $items[0]->get_attachments(); - - if(@file_get_contents ( 'https://www.w3schools.com/w3css/img_lights.jpg' )) - $this->assertEquals( 1, count( $attachments ) ); - - $document_id = $items[2]->get_document(); - - if(@file_get_contents ( 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg' )) - $this->assertTrue( is_numeric($document_id) ); - - $document = $items[3]->get_document(); - $this->assertTrue( wp_http_validate_url($document) !== false ); - - $document_id = $items[4]->get_document(); - $this->assertFalse( is_numeric($document_id) ); - - } - - - /** - * @group importer_csv_special_fields - */ - public function test_special_fields_status_and_id(){ - global $Tainacan_Importer_Handler; - $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $file_name = 'demosaved.csv'; - $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); - $Tainacan_Importer_Handler->save_importer_instance($csv_importer); - $id = $csv_importer->get_id(); - $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); - // open the file "demosaved.csv" for writing - $file = fopen($file_name, 'w'); - - // save the column headers - fputcsv($file, array('Column 1', 'special_item_status', 'Unknow Column', 'special_comment_status')); - - // Sample data - $data = array( - array('Data 11', 'publish', 'nothing', 'open'), - array('Data 21', 'private', 'void', 'closed'), - array('Data 31', 'trash', 'empty', 'open'), - array('Data 41', 'future', 'null', 'closed'), - array('Data 51', 'trash', 'zero', 'open') - ); - - // save each row of the data - foreach ($data as $row){ - fputcsv($file, $row); - } - - // Close the file - fclose($file); - - $importer_instance->set_tmp_file( $file_name ); + ]; - // file isset on importer - $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); + // get collection metadata to map + $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; - // count total items - $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); + //create a random mapping + $map = []; + foreach ( $metadata as $index => $metadatum ){ + if(isset($headers[$index])) + $map[$metadatum->get_id()] = $headers[$index]; + } + + $collection_definition['mapping'] = $map; + + // add the collection + $importer_instance->add_collection( $collection_definition ); + $importer_instance->set_option('encode','iso88591'); + + while($importer_instance->run()) { + continue; + } + + $items = $Tainacan_Items->fetch( ['order'=> 'ASC', 'orderby' => 'ID'], $collection, 'OBJECT' ); + + $this->assertEquals( $importer_instance->get_source_number_of_items(), count( $items ) ); + + // test rows + $document_id = $items[0]->get_document(); + $this->assertFalse( is_numeric($document_id) ); + + $attachments = $items[0]->get_attachments(); + + if(@file_get_contents ( 'https://www.w3schools.com/w3css/img_lights.jpg' )) + $this->assertEquals( 1, count( $attachments ) ); + + $document_id = $items[2]->get_document(); + + if(@file_get_contents ( 'https://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg' )) + $this->assertTrue( is_numeric($document_id) ); + + $document = $items[3]->get_document(); + $this->assertTrue( wp_http_validate_url($document) !== false ); + + $document_id = $items[4]->get_document(); + $this->assertFalse( is_numeric($document_id) ); + + } + + + /** + * @group importer_csv_special_fields + */ + public function test_special_fields_status_and_id(){ + global $Tainacan_Importer_Handler; + $Tainacan_Items = \Tainacan\Repositories\Items::get_instance(); + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + $file_name = 'demosaved.csv'; + $csv_importer = $Tainacan_Importer_Handler->initialize_importer('csv'); + $Tainacan_Importer_Handler->save_importer_instance($csv_importer); + $id = $csv_importer->get_id(); + $importer_instance = $Tainacan_Importer_Handler->get_importer_instance_by_session_id($id); + // open the file "demosaved.csv" for writing + $file = fopen($file_name, 'w'); + + // save the column headers + fputcsv($file, array('Column 1', 'special_item_status', 'Unknow Column', 'special_comment_status')); + + // Sample data + $data = array( + array('Data 11', 'publish', 'nothing', 'open'), + array('Data 21', 'private', 'void', 'closed'), + array('Data 31', 'trash', 'empty', 'open'), + array('Data 41', 'future', 'null', 'closed'), + array('Data 51', 'trash', 'zero', 'open') + ); + + // save each row of the data + foreach ($data as $row){ + fputcsv($file, $row); + } + + // Close the file + fclose($file); + + $importer_instance->set_tmp_file( $file_name ); + + // file isset on importer + $this->assertTrue( !empty( $importer_instance->get_tmp_file() ) ); + + // count total items + $this->assertEquals( 5, $importer_instance->get_source_number_of_items() ); - // get metadata to mapping AVOIDING special fields - $headers = $importer_instance->get_source_metadata(); - $this->assertEquals( $headers[1], 'Unknow Column' ); + // get metadata to mapping AVOIDING special fields + $headers = $importer_instance->get_source_metadata(); + $this->assertEquals( $headers[1], 'Unknow Column' ); - $this->assertEquals( $importer_instance->get_option('item_status_index'), 1 ); + $this->assertEquals( $importer_instance->get_option('item_status_index'), 1 ); - // inserting the collection - $collection = $this->tainacan_entity_factory->create_entity( - 'collection', - array( - 'name' => 'Other', - 'description' => 'adasdasdsa', - 'default_order' => 'DESC', - 'status' => 'publish' - ), - true - ); + // inserting the collection + $collection = $this->tainacan_entity_factory->create_entity( + 'collection', + array( + 'name' => 'Other', + 'description' => 'adasdasdsa', + 'default_order' => 'DESC', + 'status' => 'publish' + ), + true + ); - $metadatum = $this->tainacan_entity_factory->create_entity( - 'metadatum', - array( - 'name' => 'Data multiplo', - 'description' => 'Descreve o dado do campo data.', - 'collection' => $collection, - 'status' => 'publish', - 'metadata_type' => 'Tainacan\Metadata_Types\Text', - 'multiple' => 'yes' - ), - true - ); - - $metadatum = $this->tainacan_entity_factory->create_entity( - 'metadatum', - array( - 'name' => 'Texto simples', - 'description' => 'Descreve o dado do campo data.', - 'collection' => $collection, - 'status' => 'publish', - 'metadata_type' => 'Tainacan\Metadata_Types\Text', - 'multiple' => 'no' - ), - true - ); - - $collection_definition = [ - 'id' => $collection->get_id(), - 'total_items' => $importer_instance->get_source_number_of_items(), - ]; + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Data multiplo', + 'description' => 'Descreve o dado do campo data.', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'multiple' => 'yes' + ), + true + ); + + $metadatum = $this->tainacan_entity_factory->create_entity( + 'metadatum', + array( + 'name' => 'Texto simples', + 'description' => 'Descreve o dado do campo data.', + 'collection' => $collection, + 'status' => 'publish', + 'metadata_type' => 'Tainacan\Metadata_Types\Text', + 'multiple' => 'no' + ), + true + ); + + $collection_definition = [ + 'id' => $collection->get_id(), + 'total_items' => $importer_instance->get_source_number_of_items(), + ]; - // get collection metadata to map - $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; + // get collection metadata to map + $metadata = $Tainacan_Metadata->fetch_by_collection( $collection ) ; - //create a random mapping - $map = []; - foreach ( $metadata as $index => $metadatum ){ - if(isset($headers[$index])) - $map[$metadatum->get_id()] = $headers[$index]; - } + //create a random mapping + $map = []; + foreach ( $metadata as $index => $metadatum ){ + if(isset($headers[$index])) + $map[$metadatum->get_id()] = $headers[$index]; + } - $collection_definition['mapping'] = $map; + $collection_definition['mapping'] = $map; - // add the collection - $importer_instance->add_collection( $collection_definition ); + // add the collection + $importer_instance->add_collection( $collection_definition ); - while($importer_instance->run()){ - continue; - } + while($importer_instance->run()){ + continue; + } - $items = $Tainacan_Items->fetch( ['order'=> 'DESC', 'orderby' => 'ID'], $collection, 'OBJECT' ); + $items = $Tainacan_Items->fetch( ['order'=> 'DESC', 'orderby' => 'ID'], $collection, 'OBJECT' ); - // only 3 items should be published - $this->assertEquals( 3, count( $items ) ); + // only 3 items should be published + $this->assertEquals( 3, count( $items ) ); - //foreach ($items as $item) { - // if ( \in_array( $item->get_description(), ['Data 11', 'Data 31', 'Data 51'] ) ) { - // $this->assertEquals( 'open', $item->get_comment_status() ); - // } else { - // $this->assertEquals( 'closed', $item->get_comment_status() ); - // } - //} - } + //foreach ($items as $item) { + // if ( \in_array( $item->get_description(), ['Data 11', 'Data 31', 'Data 51'] ) ) { + // $this->assertEquals( 'open', $item->get_comment_status() ); + // } else { + // $this->assertEquals( 'closed', $item->get_comment_status() ); + // } + //} + } }