diff --git a/src/classes/class-tainacan-media.php b/src/classes/class-tainacan-media.php index f80f19316..72ba499a8 100644 --- a/src/classes/class-tainacan-media.php +++ b/src/classes/class-tainacan-media.php @@ -103,7 +103,6 @@ class Media { * @return string the file path */ public function save_remote_file($url) { - set_time_limit(0); $filename = tempnam(sys_get_temp_dir(), basename($url)); @@ -134,7 +133,7 @@ class Media { # Assign a callback function to the CURL Write-Function curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback); - # Exceute the download - note we DO NOT put the result into a variable! + # Execute the download - note we DO NOT put the result into a variable! curl_exec($ch); if (curl_errno($ch)) { $error_msg = curl_error($ch); diff --git a/src/classes/importer/class-tainacan-csv.php b/src/classes/importer/class-tainacan-csv.php index f767d2dba..afcce8e57 100644 --- a/src/classes/importer/class-tainacan-csv.php +++ b/src/classes/importer/class-tainacan-csv.php @@ -36,13 +36,15 @@ class CSV extends Importer { } $columns = []; - - if( $rawColumns ) { + if ($rawColumns) { foreach( $rawColumns as $index => $rawColumn ) { if( strpos($rawColumn,'special_') === 0 ) { if( $rawColumn === 'special_document' ) { $this->set_option('document_index', $index); - } else if( $rawColumn === 'special_attachments' || + } else if ($rawColumn === 'special_document|REPLACE') { + $this->set_option('document_import_mode', 'replace'); + $this->set_option('document_index', $index); + } else if( $rawColumn === 'special_attachments' || $rawColumn === 'special_attachments|APPEND' || $rawColumn === 'special_attachments|REPLACE' ) { $this->set_option('attachment_index', $index); @@ -514,7 +516,12 @@ class CSV extends Importer { } } else if( strpos($column_value,'file:') === 0 ) { $correct_value = trim(substr($column_value, 5)); - if( isset(parse_url($correct_value)['scheme'] ) ) { + if (isset(parse_url($correct_value)['scheme'] )) { + if ($this->get_option('document_import_mode') === 'replace' ) { + $this->add_log('Item Document will be replaced ... '); + $this->delete_previous_document_imgs($item_inserted->get_id(), $item_inserted->get_document()); + $this->add_log('Deleted previous Item Documents ... '); + } $id = $TainacanMedia->insert_attachment_from_url($correct_value, $item_inserted->get_id()); if(!$id){ @@ -571,15 +578,7 @@ class CSV extends Importer { break; case 'REPLACE': $this->add_log('Attachment REPLACE file '); - $args['post_parent'] = $item_inserted->get_id(); - $args['post_type'] = 'attachment'; - $args['post_status'] = 'any'; - $args['post__not_in'] = [$item_inserted->get_document()]; - $posts_query = new \WP_Query(); - $query_result = $posts_query->query( $args ); - foreach ( $query_result as $post ) { - wp_delete_attachment( $post->ID, true ); - } + $this->delete_previous_document_imgs($item_inserted->get_id(), $item_inserted->get_document()); break; } @@ -624,10 +623,10 @@ class CSV extends Importer { $line = substr($line, $cut_start); } - $end = substr($line, ( strlen($line) - strlen($this->get_option('enclosure')) ) , strlen($this->get_option('enclosure'))); + $end = substr($line, ( strlen($line) - strlen($this->get_option('enclosure')) ) , strlen($this->get_option('enclosure'))); if( $this->get_option('enclosure') === $end ) { - $line = substr($line, 0, ( strlen($line) - strlen($this->get_option('enclosure')) ) ); + $line = substr($line, 0, ( strlen($line) - strlen($this->get_option('enclosure')) ) ); } $delimiter = $this->get_option('enclosure').$this->get_option('delimiter').$this->get_option('enclosure'); @@ -681,7 +680,7 @@ class CSV extends Importer { * its value or values * @param integer $collection_index The index in the $this->collections array of the collection the item is being inserted into * - * @return Tainacan\Entities\Item Item inserted + * @return bool|Tainacan\Entities\Item Item inserted */ public function insert( $processed_item, $collection_index ) { remove_action( 'post_updated', 'wp_save_post_revision' ); @@ -916,10 +915,9 @@ class CSV extends Importer { * @param $metadatum the metadata * @param $values the categories names * - * @return array empty with no category or array with IDs + * @return bool|array empty with no category or array with IDs */ - private function insert_hierarchy( $metadatum, $values ){ - + private function insert_hierarchy( $metadatum, $values ) { if (empty($values)) { return false; } @@ -982,14 +980,13 @@ class CSV extends Importer { /** * @param $collection_id * - * @return array/bool false if has no mapping or associated array with metadata id and header + * @return array|bool false if has no mapping or associated array with metadata id and header */ public function get_mapping( $collection_id ){ $mapping = get_post_meta( $collection_id, 'metadata_mapping', true ); - return ( $mapping ) ? $mapping : false; + return $mapping ?: false; } - /** * @inheritdoc * @@ -1081,4 +1078,19 @@ class CSV extends Importer { return $message; } + private function delete_previous_document_imgs($item_id, $item_document) { + $previous_imgs = [ + 'post_parent' => $item_id, + 'post_type' => 'attachment', + 'post_status' => 'any', + 'post__not_in' => [$item_document] + ]; + $posts_query = new \WP_Query(); + $attachs = $posts_query->query($previous_imgs); + foreach ($attachs as $att) { + $this->add_log( "Deleting attachment [". $att->ID . "] " . $att->post_title); + wp_delete_attachment($att->ID, true); + } + } + } diff --git a/src/classes/importer/class-tainacan-importer.php b/src/classes/importer/class-tainacan-importer.php index 02aaaf2ae..8112ddaee 100644 --- a/src/classes/importer/class-tainacan-importer.php +++ b/src/classes/importer/class-tainacan-importer.php @@ -59,7 +59,7 @@ abstract class Importer { private $options = []; /** - * Stores the default options for the importer options + * Stores default options for importer options * @var array */ protected $default_options = []; @@ -114,7 +114,7 @@ abstract class Importer { private $error_log = []; /** - * Wether to abort importer execution. + * Whether to abort importer execution. * @var bool */ private $abort = false; @@ -137,7 +137,6 @@ abstract class Importer { ]; public function __construct($attributess = array()) { - $this->id = uniqid(); $author = get_current_user_id(); @@ -153,8 +152,6 @@ abstract class Importer { } } } - - } public function _to_Array($short = false) { @@ -193,8 +190,7 @@ abstract class Importer { * @param $url string * @return bool */ - public function set_url($url) - { + public function set_url($url) { if(!empty($url) && !is_array($url)) { $this->url = rtrim(trim($url), "/"); @@ -207,8 +203,7 @@ abstract class Importer { /** * @return string or bool */ - public function get_url() - { + public function get_url() { if(!empty($this->url)) { return $this->url; @@ -293,7 +288,6 @@ abstract class Importer { $this->default_options = $options; } - public function set_steps($steps) { $this->steps = $steps; } @@ -302,7 +296,6 @@ abstract class Importer { return $this->steps; } - private function get_transients() { return $this->transients; } @@ -559,7 +552,6 @@ abstract class Importer { $step = $steps[$current_step]; if ($step['callback'] == 'process_collections') { - $totalItems = 0; $currentItem = $this->get_current_collection_item(); $current_collection = $this->get_current_collection(); @@ -577,18 +569,12 @@ abstract class Importer { if ($totalItems > 0) { $value = round( ($currentItem/$totalItems) * 100 ); } - - } else { - if ( isset($step['total']) && is_numeric($step['total']) && $step['total'] > 0 ) { $current = $this->get_in_step_count(); $value = round( ($current/$step['total']) * 100 ); } - } - - } return $value; } diff --git a/src/classes/importer/class-tainacan-oaipmh-importer.php b/src/classes/importer/class-tainacan-oaipmh-importer.php index d9db9e3ab..fc3a702d5 100644 --- a/src/classes/importer/class-tainacan-oaipmh-importer.php +++ b/src/classes/importer/class-tainacan-oaipmh-importer.php @@ -5,872 +5,868 @@ use Tainacan\Entities; class Oaipmh_Importer extends Importer { - protected $steps = [ - [ - 'name' => 'Create Collections', - 'progress_label' => 'Create Collections', - 'callback' => 'create_collections' - ], - [ - 'name' => 'Import Items', - 'progress_label' => 'Import Items', - 'callback' => 'process_collections', - 'total' => 2 - ], - ]; - - protected $NAME_FOR_SETS = 'Sets'; - protected $tainacan_api_address, $wordpress_api_address, $actual_collection; - protected $has_sets = true; - protected $items_per_page = 100; - - /** - * tainacan old importer construct - */ - public function __construct($attributes = array()) { - parent::__construct($attributes); - $this->set_default_options([ - 'delimiter' => ',' - ]); - - $this->col_repo = \Tainacan\Repositories\Collections::get_instance(); - $this->items_repo = \Tainacan\Repositories\Items::get_instance(); - $this->metadata_repo = \Tainacan\Repositories\Metadata::get_instance(); - $this->item_metadata_repo = \Tainacan\Repositories\Item_Metadata::get_instance(); - $this->tax_repo = \Tainacan\Repositories\Taxonomies::get_instance(); - $this->term_repo = \Tainacan\Repositories\Terms::get_instance(); - - $this->remove_import_method('file'); - $this->add_import_method('url'); - - } - - /** - * Method implemented by the child importer class to proccess each item - * @return int - */ - public function process_item( $index, $collection_id ){ - $this->add_log('Proccess item index' . $index . ' in set ' . $collection_id['source_id'] ); - $records = [ 'records' => [] , 'collection_definition' => $collection_id ]; - $record_processed = []; - - if( $index === 0 ){ - - if( $collection_id['source_id'] !== 'sets' && $this->has_sets ){ - $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $collection_id['source_id'] ); - $this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $collection_id['source_id']); - } else { - $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc" ); - $this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc"); - } - - } else { - $token = $this->get_transient('resumptionToken'); - $info = $this->requester( $this->get_url() . "?verb=ListRecords&resumptionToken=" . $token); - $this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&resumptionToken=" . $token); - } - - if( !isset($info['body']) ){ - $this->add_log('no answer'); - return false; - } - - - try { - $xml = new \SimpleXMLElement($info['body']); - - if ( isset($xml->ListRecords) && isset($xml->ListRecords->resumptionToken) ){ - - $resumptionToken = $xml->ListRecords->resumptionToken; - if ($resumptionToken) { - $this->add_transient('resumptionToken',(string) $resumptionToken); - } - - // if there is no total in resumption token and exists cursor - // it will change dynamic the total of items - - $resumptionToken_attributes = $xml->ListRecords->resumptionToken->attributes(); - $real_total = $this->get_transient('total_general'); - - foreach ($resumptionToken_attributes as $tag => $attribute) { - - if ( $tag == 'cursor' && $real_total == ( (string) $attribute ) && !$this->hasCompleteSize( $resumptionToken_attributes ) ) { - - $real_total = $real_total + intval($this->get_transient('items_per_page')); - $this->add_transient('total_general', $real_total); - - $total = ( $this->get_transient('change_total') ) ? $this->get_transient('change_total') : 1; - - $this->add_transient('change_total', $total + 1); - break; - } - } - } - - } catch (Exception $e) { - $this->add_log('error on read xml and get '); - return false; - } - - if( $xml->ListRecords ){ - $j = 0; - - while ( isset($xml->ListRecords->record[$j]) ) { - $record = $record = $xml->ListRecords->record[$j]; - $dc = $record->metadata->children("http://www.openarchives.org/OAI/2.0/oai_dc/"); - $header = $record->header; - - $is_inserted = $this->get_transient($header->identifier); - if( $is_inserted ){ - continue; - } - - if( $this->get_option('using_set') == 'taxonomy' && ( isset($header) && isset($header->setSpec) ) ){ - foreach ($header->setSpec as $item ) { - $record_processed['sets'][] = (string) $item; - } - } - - if ($record->metadata->Count() > 0 ) { - $metadata = $dc->children('http://purl.org/dc/elements/1.1/'); - $tam_metadata = count($metadata); - for ($i = 0; $i < $tam_metadata; $i++) { - - $value = (string) $metadata[$i]; - $identifier = $this->get_identifier($metadata[$i]); - $record_processed['dc:' . $identifier ][] = $value; + protected $steps = [ + [ + 'name' => 'Create Collections', + 'progress_label' => 'Create Collections', + 'callback' => 'create_collections' + ], + [ + 'name' => 'Import Items', + 'progress_label' => 'Import Items', + 'callback' => 'process_collections', + 'total' => 2 + ], + ]; + + protected $NAME_FOR_SETS = 'Sets'; + protected $tainacan_api_address, $wordpress_api_address, $actual_collection; + protected $has_sets = true; + protected $items_per_page = 100; + + /** + * tainacan old importer construct + */ + public function __construct($attributes = array()) { + parent::__construct($attributes); + $this->set_default_options([ + 'delimiter' => ',' + ]); + + $this->col_repo = \Tainacan\Repositories\Collections::get_instance(); + $this->items_repo = \Tainacan\Repositories\Items::get_instance(); + $this->metadata_repo = \Tainacan\Repositories\Metadata::get_instance(); + $this->item_metadata_repo = \Tainacan\Repositories\Item_Metadata::get_instance(); + $this->tax_repo = \Tainacan\Repositories\Taxonomies::get_instance(); + $this->term_repo = \Tainacan\Repositories\Terms::get_instance(); + + $this->remove_import_method('file'); + $this->add_import_method('url'); + } + + /** + * Method implemented by child importer class to proccess each item + * @return int + */ + public function process_item( $index, $collection_id ){ + $this->add_log('Proccess item index' . $index . ' in set ' . $collection_id['source_id'] ); + $records = [ 'records' => [] , 'collection_definition' => $collection_id ]; + $record_processed = []; + + if ($index === 0) { + if( $collection_id['source_id'] !== 'sets' && $this->has_sets ){ + $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $collection_id['source_id'] ); + $this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $collection_id['source_id']); + } else { + $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc" ); + $this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc"); + } + } else { + $token = $this->get_transient('resumptionToken'); + $info = $this->requester( $this->get_url() . "?verb=ListRecords&resumptionToken=" . $token); + $this->add_log('fetching ' . $this->get_url() . "?verb=ListRecords&resumptionToken=" . $token); + } + + if (!isset($info['body'])) { + $this->add_log('no answer'); + return false; + } + + try { + $xml = new \SimpleXMLElement($info['body']); + + if ( isset($xml->ListRecords) && isset($xml->ListRecords->resumptionToken) ){ + + $resumptionToken = $xml->ListRecords->resumptionToken; + if ($resumptionToken) { + $this->add_transient('resumptionToken',(string) $resumptionToken); + } + + // if there is no total in resumption token and exists cursor + // it will change dynamic the total of items + + $resumptionToken_attributes = $xml->ListRecords->resumptionToken->attributes(); + $real_total = $this->get_transient('total_general'); + + foreach ($resumptionToken_attributes as $tag => $attribute) { + + if ( $tag == 'cursor' && $real_total == ( (string) $attribute ) && !$this->hasCompleteSize( $resumptionToken_attributes ) ) { + + $real_total = $real_total + intval($this->get_transient('items_per_page')); + $this->add_transient('total_general', $real_total); + + $total = ( $this->get_transient('change_total') ) ? $this->get_transient('change_total') : 1; + + $this->add_transient('change_total', $total + 1); + break; + } + } + } + + } catch (Exception $e) { + $this->add_log('error on read xml and get '); + return false; + } + + if( $xml->ListRecords ){ + $j = 0; + + while ( isset($xml->ListRecords->record[$j]) ) { + $record = $record = $xml->ListRecords->record[$j]; + $dc = $record->metadata->children("http://www.openarchives.org/OAI/2.0/oai_dc/"); + $header = $record->header; + + $is_inserted = $this->get_transient($header->identifier); + if( $is_inserted ){ + continue; + } + + if( $this->get_option('using_set') == 'taxonomy' && ( isset($header) && isset($header->setSpec) ) ){ + foreach ($header->setSpec as $item ) { + $record_processed['sets'][] = (string) $item; + } + } + + if ($record->metadata->Count() > 0 ) { + $metadata = $dc->children('http://purl.org/dc/elements/1.1/'); + $tam_metadata = count($metadata); + for ($i = 0; $i < $tam_metadata; $i++) { + + $value = (string) $metadata[$i]; + $identifier = $this->get_identifier($metadata[$i]); + $record_processed['dc:' . $identifier ][] = $value; + + } + } + + if( $record_processed ){ + $records['records'][] = $record_processed; + $record_processed = []; + } + + $j++; + } + } + + if( $records['records'] ){ + return $records; + } else { + $this->add_log('proccessing an item empty or xml not found'); + return false; + } + } + + /** + * create all collections and its metadata + * + */ + public function create_collections(){ + + $this->add_log('Creating collections'); + $collection_xml = $this->fetch_collections(); + + if( $collection_xml ){ + + if( !$this->get_option('using_set') || $this->get_option('using_set') == 'collection' ){ + + foreach ($collection_xml as $set ) { - } - } - - if( $record_processed ){ - $records['records'][] = $record_processed; - $record_processed = []; - } - - $j++; - } - } - - if( $records['records'] ){ - return $records; - } else { - $this->add_log('proccessing an item empty or xml not found'); - return false; - } - } - - /** - * create all collections and its metadata - * - */ - public function create_collections(){ - - $this->add_log('Creating collections'); - $collection_xml = $this->fetch_collections(); - - if( $collection_xml ){ - - if( !$this->get_option('using_set') || $this->get_option('using_set') == 'collection' ){ - - foreach ($collection_xml as $set ) { - - $setSpec = (string) $set->setSpec; - $setName = (string) $set->setName; - - $collection = $this->create_collection( $setSpec, $setName ); - - $metadata_map = $this->create_collection_metadata($collection); - $total = intval($this->get_total_items_from_source($setSpec)); - $this->add_log('total in collection: ' . $total); - $this->add_log('collection id ' . (string) $collection->get_id()); - - $this->add_collection([ - 'id' => $collection->get_id(), - 'mapping' => $metadata_map, - 'total_items' => ceil( $total / $this->items_per_page ), - 'source_id' => $setSpec, - ]); - } - } else if( $this->get_option('using_set') == 'taxonomy') { - - $collection = $this->create_collection( 'set', $this->getRepoName() ); - $metadata_map = $this->create_collection_metadata($collection); - $total = intval( $this->get_total_items_from_source(false) ); - $this->add_log('total in collection: ' . $total); - $this->add_log('collection id ' . (string) $collection->get_id()); - - $tax = new Entities\Taxonomy(); - $tax->set_name( $this->NAME_FOR_SETS ); - $tax->set_allow_insert('yes'); - $tax->set_status('publish'); - - if ($tax->validate()) { - - $is_tax_created = $this->get_transient('set_taxonomy_id'); - if( $is_tax_created ){ - $tax = new Entities\Taxonomy( $is_tax_created ); - } else { - $tax = $this->tax_repo->insert($tax); - $this->add_transient('set_taxonomy_id', $tax->get_id()); - } - - $metadatum_set_id = $this->create_set_metadata( $collection->get_id(), $tax->get_id() ); - - if( $metadatum_set_id ){ - $this->add_transient('set_metadatum_id', $metadatum_set_id); - - $this->add_collection([ - 'id' => $collection->get_id(), - 'mapping' => $metadata_map, - 'total_items' =>ceil( $total / $this->items_per_page ), - 'source_id' => 'sets', - 'metadatum_id' => $metadatum_set_id - ]); - } - - $this->add_log('Taxonomy ' . $tax->get_name() . ' created' ); - - foreach ($collection_xml as $set) { - - $setSpec = (string)$set->setSpec; - $setName = (string)$set->setName; - - $this->createTerms( $tax, $setName, $setSpec ); - } - - } else { - $this->add_log('Error creating taxonomy Sets' ); - $this->add_log($tax->get_errors()); - - } - - } - - } - // if there is no set - else { - $collection = $this->create_collection( 'set', $this->getRepoName() ); - $metadata_map = $this->create_collection_metadata($collection); - $total = intval( $this->get_total_items_from_source(false) ); - $this->add_log('total in collection: ' . $total); - $this->add_log('collection id ' . (string) $collection->get_id()); - - $this->add_collection([ - 'id' => $collection->get_id(), - 'mapping' => $metadata_map, - 'total_items' =>ceil( $total / $this->items_per_page ), - ]); - - $this->has_sets = false; - } - - $resumptionToken = $this->get_transient('collection_resump'); - if( $resumptionToken !== ''){ - return 1; - } else { - 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 ) { - $this->items_repo->disable_logs(); - $records = $processed_item['records']; - $collection_id = $processed_item['collection_definition']; - $collection = new Entities\Collection($collection_id['id']); - $map = $collection_id['mapping']; - - foreach ( $records as $record ) { - $item = new Entities\Item(); - $item->set_status('publish'); - $item->set_collection( $collection ); - $item->set_title( ( isset($record['dc:title']) ) ? $record['dc:title'][0] : 'title' ); - $item->set_description( '' ); - - $this->add_log( ( isset($record['dc:title']) ) ? $record['dc:title'][0] : 'title' ); - if( $record && $item->validate() ){ - $insertedItem = $this->items_repo->insert( $item ); - - if( isset($record['sets']) ){ - $terms = []; - $metadatum_set_id = $this->get_transient('set_metadatum_id'); - - foreach ($record['sets'] as $set) { - $term_id = $this->get_transient($set); - - if( $term_id ) $terms[] = $term_id; - } - - if( $metadatum_set_id && $terms ){ - $newMetadatum = new Entities\Metadatum($metadatum_set_id); - - $item_metadata = new Entities\Item_Metadata_Entity( $insertedItem, $newMetadatum ); - $item_metadata->set_value($terms); - - if( $item_metadata->validate() ){ - $this->item_metadata_repo->insert( $item_metadata ); - } - } - - unset($record['sets']); - } - - foreach ( $record as $index => $value ){ - - if( in_array( $index, $map ) && $insertedItem->get_id()){ - $metadatum_id = array_search($index, $map ); - $newMetadatum = new Entities\Metadatum($metadatum_id); - - $item_metadata = new Entities\Item_Metadata_Entity( $insertedItem, $newMetadatum ); - - $unique = !$item_metadata->is_multiple(); - $value_final = ( is_array($value) && $unique ) ? $value[0] : $value; - $item_metadata->set_value($value_final); - - if( $item_metadata->validate() ){ - $inserted = $this->item_metadata_repo->insert( $item_metadata ); - // $this->add_log('Item Metadata inserted for item ' .$item->get_title() . ' and metadata ' . $newMetadatum->get_name() ); - } else { - $this->add_log( 'Error inserting metadatum' . $newMetadatum->get_name() ); - $this->add_log( 'Values' . $value ); - $this->add_log( $item_metadata->get_errors() ); - } - } - - } - } else { - $this->add_log('item not inserted '); - } - - } - - return isset($insertedItem) ? $insertedItem : false; - } - - //protected functions - - /** - * @signature - get_identifyier($metadata) - * @param \SimpleXMLElement $metadata - * @return string O identifier - */ - protected function get_identifier($metadata) { - $attributes = $metadata->attributes(); // atributos - if ($attributes) { - foreach ($attributes as $a => $b) { - return $metadata->getName().'_'.(string) $b; - } - } else { - return $metadata->getName(); - } - } - - - /** - * 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( $setSpec ) { - - if($setSpec) - $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $setSpec); - else - $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc"); - - if( !isset($info['body']) ){ - $this->add_log('ERROR'); - $this->add_error_log('Error in fetch remote total items'); - $this->abort(); - return false; - } - - try { - $xml = new \SimpleXMLElement($info['body']); - - if( isset($xml->ListRecords) && !isset($xml->ListRecords->resumptionToken) ){ - $cont = 0; - foreach ($xml->ListRecords->record as $record) $cont++; - - $this->add_transient('total_general', (string) $cont ); - return $cont; - } elseif ( isset($xml->ListRecords) && isset($xml->ListRecords->resumptionToken) ){ - - $resumptionToken_attributes = $xml->ListRecords->resumptionToken->attributes(); - - foreach ($resumptionToken_attributes as $tag => $attribute) { - if ($tag == 'completeListSize') { - $this->add_transient('total_general', (string) $attribute); - return (string) $attribute; - } - } - - foreach ($resumptionToken_attributes as $tag => $attribute) { - if ($tag == 'cursor') { - $this->items_per_page = $attribute; - $this->add_transient('items_per_page', (string) $this->items_per_page); - } - } - - // if the total is not found - $this->add_transient('total_general', (string) $this->items_per_page); - return $this->items_per_page; - } - } catch (Exception $e) { - $this->add_log('ERROR'); - return 0; - } - - return 0; - } - - /** - * create the collection in tainacan - * - * @return Entities\Collection - */ - protected function create_collection( $setSpec, $setName ){ - $is_created = $this->get_transient('collection_' . $setSpec. '_name'); - if( $is_created ){ - $new_collection = new Entities\Collection( $is_created ); - return $new_collection; - } - - $new_collection = new Entities\Collection(); - $new_collection->set_name($setName); - $new_collection->set_status('publish'); - $new_collection->validate(); - - - if($new_collection->validate()){ - $new_collection =$this->col_repo->insert($new_collection); - - $this->add_log('Collection created: ' . $new_collection->get_name()); - - if( $new_collection ) - $this->add_transient('collection_' . $setSpec. '_name', $new_collection->get_id()); - - return $new_collection; - } else{ - $this->add_error_log('Error creating collection ' . $setName ); - $this->add_error_log($new_collection->get_errors()); - $this->abort(); - return false; - } - - } - - /** - * @param $collection_object - * @throws \ErrorException - */ - protected function create_collection_metadata( $collection_object ){ - $Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance(); - $mapper_obj = $Tainacan_Mappers->check_class_name('dublin-core', true, $Tainacan_Mappers::MAPPER_CLASS_PREFIX); - $mapper = new $mapper_obj; - $array_metadata = []; - - $mapper_metadata = $mapper->metadata; - if(is_array($mapper_metadata) ) { - $id = $collection_object->get_id(); - - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - foreach ($mapper_metadata as $slug => $mapper_metadatum) { - if( array_key_exists('core_metadatum', $mapper_metadatum) ) { - $method = 'get_core_' . $mapper_metadatum['core_metadatum'] . '_metadatum'; - if (method_exists($collection_object, $method)) { - $core_meta = $collection_object->$method(); - if ( ! $core_meta ) { - $Tainacan_Metadata->register_core_metadata( $collection_object, true ); - $core_meta = $collection_object->$method(); - } - $_meta_mapping = $core_meta->get_exposer_mapping(); - $_meta_mapping[$mapper->slug] = $slug; - $core_meta->set_exposer_mapping($_meta_mapping); - if ($core_meta->validate()) { - $new_metadata = $Tainacan_Metadata->insert($core_meta); - $array_metadata[$new_metadata->get_id()] = $slug; - } - } - continue; - } - - $metadatum = new \Tainacan\Entities\Metadatum(); - if( - array_key_exists('metadata_type', $mapper_metadatum) && - $mapper_metadatum['metadata_type'] != false && - class_exists($mapper_metadatum['metadata_type']) - ) { - $metadatum->set_metadata_type($mapper_metadatum['metadata_type']); - } else { - $metadatum->set_metadata_type('Tainacan\Metadata_Types\Text'); - } - $metadatum->set_name($mapper_metadatum['label']); - $metadatum->set_semantic_uri($mapper->get_url($slug)); - $metadatum->set_exposer_mapping([ - $mapper->slug => $slug - ]); - $metadatum->set_status('publish'); - $metadatum->set_collection_id($id); - $metadatum->set_slug($slug); - - if($metadatum->validate()){ - - $metadatum_id_created = $this->get_transient('collection_' . $id . '_' . $slug ); - if( $metadatum_id_created ){ - $array_metadata[$metadatum_id_created] = $slug; - } else { - $new_metadata = $Tainacan_Metadata->insert($metadatum); - $array_metadata[$new_metadata->get_id()] = $slug; - } - - } - } - } - - return $array_metadata; - } - - /** - * return all taxonomies from tainacan old - * @return array - */ - protected function fetch_collections(){ - - $collections_array = []; - // block terms with same set spec - $resumptionToken = $this->get_transient('collection_resump'); - if( $resumptionToken ){ - $collections_link = $this->get_url() . "?verb=ListSets&resumptionToken=" . $resumptionToken; - } else { - $collections_link = $this->get_url() . "?verb=ListSets"; - } - - $collections = $this->requester($collections_link); - $xml = $this->decode_request($collections, $collections_link); - - if( isset($xml->ListSets->set) ) { - foreach ($xml->ListSets->set as $set) { - - $collections_array[] = $set; - } - } - - if( isset($xml->ListSets) && isset($xml->ListSets->resumptionToken) ){ - $this->add_transient('collection_resump',(string) $xml->ListSets->resumptionToken); - } else { - $this->add_transient('collection_resump', (string) $xml->ListSets->resumptionToken); - } - - // TODO: verify if exists resumption token - - return ($collections_array) ? $collections_array : []; - } - - /** - * decode request from wp_remote - * @return array/bool - */ - protected function decode_request($result, $url){ - if (is_wp_error($result)) { - - $this->add_error_log($result->get_error_message()); - $this->add_error_log('Error in fetch remote' . $url); - $this->abort(); - return false; - - } else if (isset($result['body'])){ - - try { - $xml = new \SimpleXMLElement($result['body']); - return $xml; - } catch (Exception $e) { - return false; - } - } - - $this->add_error_log('Error in fetch remote'); - $this->abort(); - return false; - } - - /** - * executes the request - */ - protected function requester( $link ){ - $has_response = false; - $requests = 0; - - $args = array( - 'timeout' => 60, - 'redirection' => 30, - 'sslverify' => false - ); - - $this->add_log('fetching init ' . $link ); - $result = wp_remote_get($link, $args); - - while( !$has_response ){ - - if (is_wp_error($result)) { - - $this->add_log($result->get_error_message()); - $this->add_log('Error in fetch remote' . $link); - $this->add_log('request number ' . $requests); - - } else if (isset($result['body'])){ - $this->add_log('fetch OK '); - return $result; - } - - if( $requests > 10 ){ - break; - } - - if( $requests > 3 ){ - $this->add_log('taking a moment to breathe, waiting for ' . ( $requests * 10 ) . ' seconds ' ); - sleep( $requests * 10 ); - } - - $args = array( - 'timeout' => 60, - 'redirection' => 30, - 'sslverify' => false - ); - - $result = wp_remote_get($link, $args); - - $requests++; - $this->add_log('going to ' . $requests ); - } - - - - $this->add_error_log('Error in fetch remote, expired the 10 requests limit ' . $link); - $this->abort(); - return false; - } - - /** - * @param $taxonomy_father - * @param $name - * @param $slug - * @return bool - */ - public function createTerms( $taxonomy_father, $name, $slug ){ - $new_term = new Entities\Term(); - $new_term->set_taxonomy($taxonomy_father->get_db_identifier()); - $new_term->set_name($name); - - // block terms with same set spec - $map = $this->get_transient($slug); - - if($map){ - return false; - } - - if ($new_term->validate()) { - $inserted_term = $this->term_repo->insert($new_term); - } else { - $this->add_log( implode(',', $new_term->get_errors()) ); - return false; - } - - - if (is_wp_error($inserted_term)) { - - $this->add_log($inserted_term->get_error_message()); - - } else { - $this->add_transient($slug, $inserted_term->get_id()); - $this->add_log('Added term: ' . $inserted_term->get_name() . ' in tax: ' . $taxonomy_father->get_name()); - return true; - } - - return false; - } - - /** - * @param $collection_id - * @param $taxonomy_id - * @return bool|int - * @throws \Exception - */ - public function create_set_metadata( $collection_id, $taxonomy_id ){ - $newMetadatum = new Entities\Metadatum(); - - $name = $this->NAME_FOR_SETS; - $type = 'Taxonomy'; - - $newMetadatum->set_name($name); - $newMetadatum->set_metadata_type('Tainacan\Metadata_Types\\'.$type); - $newMetadatum->set_collection_id( (isset($collection_id)) ? $collection_id : 'default'); - $newMetadatum->set_status('publish'); - $newMetadatum->set_metadata_type_options(['taxonomy_id' => $taxonomy_id ]); - $newMetadatum->set_multiple('yes'); - - if($newMetadatum->validate()){ - $is_meta_created = $this->get_transient('set_metadatum_id'); - if( $is_meta_created ){ - $inserted_metadata = new Entities\Metadatum($is_meta_created); - - $this->add_log('Metadata get: ' . $inserted_metadata->get_name()); - } else { - $inserted_metadata = $this->metadata_repo->insert( $newMetadatum ); - - $this->add_log('Metadata created: ' . $inserted_metadata->get_name()); - } - - return $inserted_metadata->get_id(); - } else{ - return false; - } - } - - public function getRepoName(){ - $info = $this->requester( $this->get_url() . "?verb=Identify"); - - if( !isset($info['body']) ){ - $this->add_log('ERROR on get repo name'); - $this->add_error_log('Error in fetch remote total items'); - $this->abort(); - return __('Imported Repo'); - } else { - - try { - $xml = new \SimpleXMLElement($info['body']); - - if( isset($xml->Identify) && isset($xml->Identify->repositoryName) && !empty($xml->Identify->repositoryName) ){ - return (string) $xml->Identify->repositoryName; - } - } catch (Exception $e) { - return __('Imported Repo'); - } - - return __('Imported Repo'); - - } - } - - /** - * @param $attributes - * @return bool - */ - public function hasCompleteSize( $attributes ) { - foreach ( $attributes as $tag => $attribute ) { - if ( $tag == 'completeListSize' ) { - return true; - } - } - return false; - } - - /** - * Gets the current value to build the progress bar and give feedback to the user - * on the background process that is running the importer. - * - * It does so by comparing the "size" attribute with the $in_step_count class attribute - * where size indicates the total size of iterations the step will take and $this->in_step_count - * is the current iteration. - * - * For the step with "process_items" as a callback, this method will look for the the $this->collections array - * and sum the value of all "total_items" attributes of each collection. Then it will look for - * $this->get_current_collection and $this->set_current_collection_item to calculate the progress. - * - * The value must be from 0 to 100 - * - * If a negative value is passed, it is assumed that the progress is unknown - */ - public function get_progress_value() { - $current_step = $this->get_current_step(); - $steps = $this->get_steps(); - $value = -1; - - if ( isset($steps[$current_step]) ) { - $step = $steps[$current_step]; - - if ($step['callback'] == 'process_collections') { - - $totalItems = 0; - $currentItem = $this->get_current_collection_item(); - $current_collection = $this->get_current_collection(); - $collections = $this->get_collections(); - - foreach ($collections as $i => $col) { - if ( isset($col['total_items']) && is_numeric($col['total_items']) ) { - $totalItems += intval($col['total_items']); - if ($i < $current_collection) { - $currentItem += $col['total_items']; - } - } - } - - if ($totalItems > 0) { - $totalItems = ($this->get_transient('change_total')) ? $this->get_transient('change_total') : $totalItems; - $value = round( ($currentItem/$totalItems) * 100 ); - } - - - } else { - - if ( isset($step['total']) && is_numeric($step['total']) && $step['total'] > 0 ) { - $total = ($this->get_transient('change_total')) ? $this->get_transient('change_total') : $step['total']; - - $current = $this->get_in_step_count(); - $value = round( ($current/$total) * 100 ); - } - - } - - - } - return $value; - } - - public function options_form(){ - ob_start(); - ?> -
-
-
- - - - - - - - -
-
-
-
-

-
-
-
-
-
- -
-
-
-
-
- setSpec; + $setName = (string) $set->setName; + + $collection = $this->create_collection( $setSpec, $setName ); + + $metadata_map = $this->create_collection_metadata($collection); + $total = intval($this->get_total_items_from_source($setSpec)); + $this->add_log('total in collection: ' . $total); + $this->add_log('collection id ' . (string) $collection->get_id()); + + $this->add_collection([ + 'id' => $collection->get_id(), + 'mapping' => $metadata_map, + 'total_items' => ceil( $total / $this->items_per_page ), + 'source_id' => $setSpec, + ]); + } + } else if( $this->get_option('using_set') == 'taxonomy') { + + $collection = $this->create_collection( 'set', $this->getRepoName() ); + $metadata_map = $this->create_collection_metadata($collection); + $total = intval( $this->get_total_items_from_source(false) ); + $this->add_log('total in collection: ' . $total); + $this->add_log('collection id ' . (string) $collection->get_id()); + + $tax = new Entities\Taxonomy(); + $tax->set_name( $this->NAME_FOR_SETS ); + $tax->set_allow_insert('yes'); + $tax->set_status('publish'); + + if ($tax->validate()) { + + $is_tax_created = $this->get_transient('set_taxonomy_id'); + if( $is_tax_created ){ + $tax = new Entities\Taxonomy( $is_tax_created ); + } else { + $tax = $this->tax_repo->insert($tax); + $this->add_transient('set_taxonomy_id', $tax->get_id()); + } + + $metadatum_set_id = $this->create_set_metadata( $collection->get_id(), $tax->get_id() ); + + if( $metadatum_set_id ){ + $this->add_transient('set_metadatum_id', $metadatum_set_id); + + $this->add_collection([ + 'id' => $collection->get_id(), + 'mapping' => $metadata_map, + 'total_items' =>ceil( $total / $this->items_per_page ), + 'source_id' => 'sets', + 'metadatum_id' => $metadatum_set_id + ]); + } + + $this->add_log('Taxonomy ' . $tax->get_name() . ' created' ); + + foreach ($collection_xml as $set) { + + $setSpec = (string)$set->setSpec; + $setName = (string)$set->setName; + + $this->createTerms( $tax, $setName, $setSpec ); + } + + } else { + $this->add_log('Error creating taxonomy Sets' ); + $this->add_log($tax->get_errors()); + + } + + } + + } + // if there is no set + else { + $collection = $this->create_collection( 'set', $this->getRepoName() ); + $metadata_map = $this->create_collection_metadata($collection); + $total = intval( $this->get_total_items_from_source(false) ); + $this->add_log('total in collection: ' . $total); + $this->add_log('collection id ' . (string) $collection->get_id()); + + $this->add_collection([ + 'id' => $collection->get_id(), + 'mapping' => $metadata_map, + 'total_items' =>ceil( $total / $this->items_per_page ), + ]); + + $this->has_sets = false; + } + + $resumptionToken = $this->get_transient('collection_resump'); + if( $resumptionToken !== ''){ + return 1; + } else { + 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 ) { + $this->items_repo->disable_logs(); + $records = $processed_item['records']; + $collection_id = $processed_item['collection_definition']; + $collection = new Entities\Collection($collection_id['id']); + $map = $collection_id['mapping']; + + foreach ( $records as $record ) { + $item = new Entities\Item(); + $item->set_status('publish'); + $item->set_collection( $collection ); + $item->set_title( ( isset($record['dc:title']) ) ? $record['dc:title'][0] : 'title' ); + $item->set_description( '' ); + + $this->add_log( ( isset($record['dc:title']) ) ? $record['dc:title'][0] : 'title' ); + if( $record && $item->validate() ){ + $insertedItem = $this->items_repo->insert( $item ); + + if( isset($record['sets']) ){ + $terms = []; + $metadatum_set_id = $this->get_transient('set_metadatum_id'); + + foreach ($record['sets'] as $set) { + $term_id = $this->get_transient($set); + + if( $term_id ) $terms[] = $term_id; + } + + if( $metadatum_set_id && $terms ){ + $newMetadatum = new Entities\Metadatum($metadatum_set_id); + + $item_metadata = new Entities\Item_Metadata_Entity( $insertedItem, $newMetadatum ); + $item_metadata->set_value($terms); + + if( $item_metadata->validate() ){ + $this->item_metadata_repo->insert( $item_metadata ); + } + } + + unset($record['sets']); + } + + foreach ( $record as $index => $value ){ + + if( in_array( $index, $map ) && $insertedItem->get_id()){ + $metadatum_id = array_search($index, $map ); + $newMetadatum = new Entities\Metadatum($metadatum_id); + + $item_metadata = new Entities\Item_Metadata_Entity( $insertedItem, $newMetadatum ); + + $unique = !$item_metadata->is_multiple(); + $value_final = ( is_array($value) && $unique ) ? $value[0] : $value; + $item_metadata->set_value($value_final); + + if( $item_metadata->validate() ){ + $inserted = $this->item_metadata_repo->insert( $item_metadata ); + // $this->add_log('Item Metadata inserted for item ' .$item->get_title() . ' and metadata ' . $newMetadatum->get_name() ); + } else { + $this->add_log( 'Error inserting metadatum' . $newMetadatum->get_name() ); + $this->add_log( 'Values' . $value ); + $this->add_log( $item_metadata->get_errors() ); + } + } + + } + } else { + $this->add_log('item not inserted '); + } + + } + + return isset($insertedItem) ? $insertedItem : false; + } + + //protected functions + + /** + * @signature - get_identifyier($metadata) + * @param \SimpleXMLElement $metadata + * @return string O identifier + */ + protected function get_identifier($metadata) { + $attributes = $metadata->attributes(); // atributos + if ($attributes) { + foreach ($attributes as $a => $b) { + return $metadata->getName().'_'.(string) $b; + } + } else { + return $metadata->getName(); + } + } + + + /** + * 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( $setSpec ) { + + if($setSpec) + $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc&set=" . $setSpec); + else + $info = $this->requester( $this->get_url() . "?verb=ListRecords&metadataPrefix=oai_dc"); + + if( !isset($info['body']) ){ + $this->add_log('ERROR'); + $this->add_error_log('Error in fetch remote total items'); + $this->abort(); + return false; + } + + try { + $xml = new \SimpleXMLElement($info['body']); + + if( isset($xml->ListRecords) && !isset($xml->ListRecords->resumptionToken) ){ + $cont = 0; + foreach ($xml->ListRecords->record as $record) $cont++; + + $this->add_transient('total_general', (string) $cont ); + return $cont; + } elseif ( isset($xml->ListRecords) && isset($xml->ListRecords->resumptionToken) ){ + + $resumptionToken_attributes = $xml->ListRecords->resumptionToken->attributes(); + + foreach ($resumptionToken_attributes as $tag => $attribute) { + if ($tag == 'completeListSize') { + $this->add_transient('total_general', (string) $attribute); + return (string) $attribute; + } + } + + foreach ($resumptionToken_attributes as $tag => $attribute) { + if ($tag == 'cursor') { + $this->items_per_page = $attribute; + $this->add_transient('items_per_page', (string) $this->items_per_page); + } + } + + // if the total is not found + $this->add_transient('total_general', (string) $this->items_per_page); + return $this->items_per_page; + } + } catch (Exception $e) { + $this->add_log('ERROR'); + return 0; + } + + return 0; + } + + /** + * create the collection in tainacan + * + * @return Entities\Collection + */ + protected function create_collection( $setSpec, $setName ){ + $is_created = $this->get_transient('collection_' . $setSpec. '_name'); + if( $is_created ){ + $new_collection = new Entities\Collection( $is_created ); + return $new_collection; + } + + $new_collection = new Entities\Collection(); + $new_collection->set_name($setName); + $new_collection->set_status('publish'); + $new_collection->validate(); + + + if($new_collection->validate()){ + $new_collection =$this->col_repo->insert($new_collection); + + $this->add_log('Collection created: ' . $new_collection->get_name()); + + if( $new_collection ) + $this->add_transient('collection_' . $setSpec. '_name', $new_collection->get_id()); + + return $new_collection; + } else{ + $this->add_error_log('Error creating collection ' . $setName ); + $this->add_error_log($new_collection->get_errors()); + $this->abort(); + return false; + } + + } + + /** + * @param $collection_object + * @throws \ErrorException + */ + protected function create_collection_metadata( $collection_object ){ + $Tainacan_Mappers = \Tainacan\Mappers_Handler::get_instance(); + $mapper_obj = $Tainacan_Mappers->check_class_name('dublin-core', true, $Tainacan_Mappers::MAPPER_CLASS_PREFIX); + $mapper = new $mapper_obj; + $array_metadata = []; + + $mapper_metadata = $mapper->metadata; + if(is_array($mapper_metadata) ) { + $id = $collection_object->get_id(); + + $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); + foreach ($mapper_metadata as $slug => $mapper_metadatum) { + if( array_key_exists('core_metadatum', $mapper_metadatum) ) { + $method = 'get_core_' . $mapper_metadatum['core_metadatum'] . '_metadatum'; + if (method_exists($collection_object, $method)) { + $core_meta = $collection_object->$method(); + if ( ! $core_meta ) { + $Tainacan_Metadata->register_core_metadata( $collection_object, true ); + $core_meta = $collection_object->$method(); + } + $_meta_mapping = $core_meta->get_exposer_mapping(); + $_meta_mapping[$mapper->slug] = $slug; + $core_meta->set_exposer_mapping($_meta_mapping); + if ($core_meta->validate()) { + $new_metadata = $Tainacan_Metadata->insert($core_meta); + $array_metadata[$new_metadata->get_id()] = $slug; + } + } + continue; + } + + $metadatum = new \Tainacan\Entities\Metadatum(); + if( + array_key_exists('metadata_type', $mapper_metadatum) && + $mapper_metadatum['metadata_type'] != false && + class_exists($mapper_metadatum['metadata_type']) + ) { + $metadatum->set_metadata_type($mapper_metadatum['metadata_type']); + } else { + $metadatum->set_metadata_type('Tainacan\Metadata_Types\Text'); + } + $metadatum->set_name($mapper_metadatum['label']); + $metadatum->set_semantic_uri($mapper->get_url($slug)); + $metadatum->set_exposer_mapping([ + $mapper->slug => $slug + ]); + $metadatum->set_status('publish'); + $metadatum->set_collection_id($id); + $metadatum->set_slug($slug); + + if($metadatum->validate()){ + + $metadatum_id_created = $this->get_transient('collection_' . $id . '_' . $slug ); + if( $metadatum_id_created ){ + $array_metadata[$metadatum_id_created] = $slug; + } else { + $new_metadata = $Tainacan_Metadata->insert($metadatum); + $array_metadata[$new_metadata->get_id()] = $slug; + } + + } + } + } + + return $array_metadata; + } + + /** + * return all taxonomies from tainacan old + * @return array + */ + protected function fetch_collections(){ + + $collections_array = []; + // block terms with same set spec + $resumptionToken = $this->get_transient('collection_resump'); + if( $resumptionToken ){ + $collections_link = $this->get_url() . "?verb=ListSets&resumptionToken=" . $resumptionToken; + } else { + $collections_link = $this->get_url() . "?verb=ListSets"; + } + + $collections = $this->requester($collections_link); + $xml = $this->decode_request($collections, $collections_link); + + if( isset($xml->ListSets->set) ) { + foreach ($xml->ListSets->set as $set) { + + $collections_array[] = $set; + } + } + + if( isset($xml->ListSets) && isset($xml->ListSets->resumptionToken) ){ + $this->add_transient('collection_resump',(string) $xml->ListSets->resumptionToken); + } else { + $this->add_transient('collection_resump', (string) $xml->ListSets->resumptionToken); + } + + // TODO: verify if exists resumption token + + return ($collections_array) ? $collections_array : []; + } + + /** + * decode request from wp_remote + * @return array/bool + */ + protected function decode_request($result, $url){ + if (is_wp_error($result)) { + + $this->add_error_log($result->get_error_message()); + $this->add_error_log('Error in fetch remote' . $url); + $this->abort(); + return false; + + } else if (isset($result['body'])){ + + try { + $xml = new \SimpleXMLElement($result['body']); + return $xml; + } catch (Exception $e) { + return false; + } + } + + $this->add_error_log('Error in fetch remote'); + $this->abort(); + return false; + } + + /** + * executes the request + */ + protected function requester( $link ){ + $has_response = false; + $requests = 0; + + $args = array( + 'timeout' => 60, + 'redirection' => 30, + 'sslverify' => false + ); + + $this->add_log('fetching init ' . $link ); + $result = wp_remote_get($link, $args); + + while( !$has_response ){ + + if (is_wp_error($result)) { + + $this->add_log($result->get_error_message()); + $this->add_log('Error in fetch remote' . $link); + $this->add_log('request number ' . $requests); + + } else if (isset($result['body'])){ + $this->add_log('fetch OK '); + return $result; + } + + if( $requests > 10 ){ + break; + } + + if( $requests > 3 ){ + $this->add_log('taking a moment to breathe, waiting for ' . ( $requests * 10 ) . ' seconds ' ); + sleep( $requests * 10 ); + } + + $args = array( + 'timeout' => 60, + 'redirection' => 30, + 'sslverify' => false + ); + + $result = wp_remote_get($link, $args); + + $requests++; + $this->add_log('going to ' . $requests ); + } + + + + $this->add_error_log('Error in fetch remote, expired the 10 requests limit ' . $link); + $this->abort(); + return false; + } + + /** + * @param $taxonomy_father + * @param $name + * @param $slug + * @return bool + */ + public function createTerms( $taxonomy_father, $name, $slug ){ + $new_term = new Entities\Term(); + $new_term->set_taxonomy($taxonomy_father->get_db_identifier()); + $new_term->set_name($name); + + // block terms with same set spec + $map = $this->get_transient($slug); + + if($map){ + return false; + } + + if ($new_term->validate()) { + $inserted_term = $this->term_repo->insert($new_term); + } else { + $this->add_log( implode(',', $new_term->get_errors()) ); + return false; + } + + + if (is_wp_error($inserted_term)) { + + $this->add_log($inserted_term->get_error_message()); + + } else { + $this->add_transient($slug, $inserted_term->get_id()); + $this->add_log('Added term: ' . $inserted_term->get_name() . ' in tax: ' . $taxonomy_father->get_name()); + return true; + } + + return false; + } + + /** + * @param $collection_id + * @param $taxonomy_id + * @return bool|int + * @throws \Exception + */ + public function create_set_metadata( $collection_id, $taxonomy_id ){ + $newMetadatum = new Entities\Metadatum(); + + $name = $this->NAME_FOR_SETS; + $type = 'Taxonomy'; + + $newMetadatum->set_name($name); + $newMetadatum->set_metadata_type('Tainacan\Metadata_Types\\'.$type); + $newMetadatum->set_collection_id( (isset($collection_id)) ? $collection_id : 'default'); + $newMetadatum->set_status('publish'); + $newMetadatum->set_metadata_type_options(['taxonomy_id' => $taxonomy_id ]); + $newMetadatum->set_multiple('yes'); + + if($newMetadatum->validate()){ + $is_meta_created = $this->get_transient('set_metadatum_id'); + if( $is_meta_created ){ + $inserted_metadata = new Entities\Metadatum($is_meta_created); + + $this->add_log('Metadata get: ' . $inserted_metadata->get_name()); + } else { + $inserted_metadata = $this->metadata_repo->insert( $newMetadatum ); + + $this->add_log('Metadata created: ' . $inserted_metadata->get_name()); + } + + return $inserted_metadata->get_id(); + } else{ + return false; + } + } + + public function getRepoName(){ + $info = $this->requester( $this->get_url() . "?verb=Identify"); + + if( !isset($info['body']) ){ + $this->add_log('ERROR on get repo name'); + $this->add_error_log('Error in fetch remote total items'); + $this->abort(); + return __('Imported Repo'); + } else { + + try { + $xml = new \SimpleXMLElement($info['body']); + + if( isset($xml->Identify) && isset($xml->Identify->repositoryName) && !empty($xml->Identify->repositoryName) ){ + return (string) $xml->Identify->repositoryName; + } + } catch (Exception $e) { + return __('Imported Repo'); + } + + return __('Imported Repo'); + + } + } + + /** + * @param $attributes + * @return bool + */ + public function hasCompleteSize( $attributes ) { + foreach ( $attributes as $tag => $attribute ) { + if ( $tag == 'completeListSize' ) { + return true; + } + } + return false; + } + + /** + * Gets the current value to build the progress bar and give feedback to the user + * on the background process that is running the importer. + * + * It does so by comparing the "size" attribute with the $in_step_count class attribute + * where size indicates the total size of iterations the step will take and $this->in_step_count + * is the current iteration. + * + * For the step with "process_items" as a callback, this method will look for the the $this->collections array + * and sum the value of all "total_items" attributes of each collection. Then it will look for + * $this->get_current_collection and $this->set_current_collection_item to calculate the progress. + * + * The value must be from 0 to 100 + * + * If a negative value is passed, it is assumed that the progress is unknown + */ + public function get_progress_value() { + $current_step = $this->get_current_step(); + $steps = $this->get_steps(); + $value = -1; + + if ( isset($steps[$current_step]) ) { + $step = $steps[$current_step]; + + if ($step['callback'] == 'process_collections') { + + $totalItems = 0; + $currentItem = $this->get_current_collection_item(); + $current_collection = $this->get_current_collection(); + $collections = $this->get_collections(); + + foreach ($collections as $i => $col) { + if ( isset($col['total_items']) && is_numeric($col['total_items']) ) { + $totalItems += intval($col['total_items']); + if ($i < $current_collection) { + $currentItem += $col['total_items']; + } + } + } + + if ($totalItems > 0) { + $totalItems = ($this->get_transient('change_total')) ? $this->get_transient('change_total') : $totalItems; + $value = round( ($currentItem/$totalItems) * 100 ); + } + + + } else { + + if ( isset($step['total']) && is_numeric($step['total']) && $step['total'] > 0 ) { + $total = ($this->get_transient('change_total')) ? $this->get_transient('change_total') : $step['total']; + + $current = $this->get_in_step_count(); + $value = round( ($current/$total) * 100 ); + } + + } + + + } + return $value; + } + + public function options_form(){ + ob_start(); + ?> +
+
+
+ + + + + + + + +
+
+
+
+

+
+
+
+
+
+ +
+
+
+
+
+ 'Create Taxonomies', 'progress_label' => 'Creating taxonomies', @@ -33,7 +32,6 @@ class Old_Tainacan extends Importer{ 'callback' => 'link_relationships', 'total' => 5 ] - ]; protected $tainacan_api_address, $wordpress_api_address, $actual_collection; @@ -64,8 +62,7 @@ class Old_Tainacan extends Importer{ * */ public function create_taxonomies() { - - if(!$this->get_url()){ + if (!$this->get_url()) { $this->add_error_log('Site url not found'); $this->abort(); return false; @@ -74,57 +71,49 @@ class Old_Tainacan extends Importer{ $this->add_log('Creating taxonomies'); foreach ($this->get_taxonomies() as $taxonomy) { + $tax = new Entities\Taxonomy(); + $tax->set_name( $taxonomy->name ); + $tax->set_description( $taxonomy->description ); + $tax->set_allow_insert('yes'); + $tax->set_status('publish'); - $tax = new Entities\Taxonomy(); - $tax->set_name( $taxonomy->name ); - $tax->set_description( $taxonomy->description ); - $tax->set_allow_insert('yes'); - $tax->set_status('publish'); - - if ($tax->validate()) { - $tax = $this->tax_repo->insert($tax); + if ($tax->validate()) { + $tax = $this->tax_repo->insert($tax); $this->add_log('Taxonomy ' . $tax->get_name() . ' created, id from Old'. $taxonomy->term_id ); + $this->add_transient('tax_' . $taxonomy->term_id . '_id', $tax->get_id()); + $this->add_transient('tax_' . $taxonomy->term_id . '_name', $tax->get_name()); - $this->add_transient('tax_' . $taxonomy->term_id . '_id', $tax->get_id()); - $this->add_transient('tax_' . $taxonomy->term_id . '_name', $tax->get_name()); - - if (isset($taxonomy->children) && $tax) { - $this->add_all_terms($tax, $taxonomy->children); - } - + if (isset($taxonomy->children) && $tax) { + $this->add_all_terms($tax, $taxonomy->children); + } } else { - $this->add_log('Error creating taxonomy ' . $taxonomy->name ); - $this->add_log($tax->get_errors()); - - } + $this->add_log('Error creating taxonomy ' . $taxonomy->name ); + $this->add_log($tax->get_errors()); + } + } - } - return false; - } + } /** * create the repository metadata which each collection inherits by default * */ - public function create_repo_metadata(){ - - $this->add_log('Creating repository metadata'); + public function create_repo_metadata() { + $this->add_log('Creating repository metadata'); foreach ($this->get_repo_metadata() as $metadata) { + if (isset($metadata->slug) && strpos($metadata->slug, 'socialdb_property_fixed') === false) { + $metadatum_id = $this->create_metadata( $metadata ); + } elseif ( strpos($metadata->slug, 'socialdb_property_fixed_tags') !== false ) { + $metadatum_id = $this->create_metadata( $metadata ); + } + } - if (isset($metadata->slug) && strpos($metadata->slug, 'socialdb_property_fixed') === false) { - $metadatum_id = $this->create_metadata( $metadata ); - } elseif ( strpos($metadata->slug, 'socialdb_property_fixed_tags') !== false ){ - $metadatum_id = $this->create_metadata( $metadata ); - } - - } - - $this->add_log('FInished repository metadata'); - return false; - } + $this->add_log('FInished repository metadata'); + return false; + } /** * create all collections and its metadata diff --git a/src/classes/importer/class-tainacan-test-importer.php b/src/classes/importer/class-tainacan-test-importer.php index b129691a5..db47658f3 100644 --- a/src/classes/importer/class-tainacan-test-importer.php +++ b/src/classes/importer/class-tainacan-test-importer.php @@ -316,11 +316,9 @@ class Test_Importer extends Importer { - validate()) { $tax1 = $this->tax_repo->insert($tax1); } else { - /** * In these set up steps, if we have an error adding * a taxonomy, collection or metadatum, there is no point @@ -345,14 +342,12 @@ class Test_Importer extends Importer { $this->add_error_log('Error creating taxonomy Color'); $this->add_error_log($tax1->get_errors()); $this->abort(); - return false; - + return false; } $this->add_transient('tax_1_id', $tax1->get_id()); return false; - } public function create_collections() { @@ -366,11 +361,9 @@ class Test_Importer extends Importer { $this->add_error_log('Error creating Collection 1'); $this->add_error_log($col1->get_errors()); $this->abort(); - return false; - + return false; } - $col1_map = []; // metadata @@ -696,8 +689,7 @@ class Test_Importer extends Importer { * @param $collection * */ - private function create_metadata( $args, $collection ){ - + private function create_metadata( $args, $collection ) { $metadatum = new Entities\Metadatum(); $metadatum->set_name($args['name']); $metadatum->set_collection($collection); @@ -758,7 +750,6 @@ class Test_Importer extends Importer { return $array; } - public function get_col2_item($index) { return [ 'field1' => 'Collection 2 item ' . $index, @@ -766,7 +757,5 @@ class Test_Importer extends Importer { 'field3' => 'Collection 2 whatever ' . $index, ]; } - - - + } \ No newline at end of file diff --git a/src/views/admin/admin.vue b/src/views/admin/admin.vue index d540cdcff..0a504c205 100644 --- a/src/views/admin/admin.vue +++ b/src/views/admin/admin.vue @@ -20,15 +20,16 @@ @click="isMenuCompressed = !isMenuCompressed"> - + diff --git a/src/views/tainacan-admin-i18n.php b/src/views/tainacan-admin-i18n.php index b08d6e29d..87b7034c4 100644 --- a/src/views/tainacan-admin-i18n.php +++ b/src/views/tainacan-admin-i18n.php @@ -474,6 +474,7 @@ return apply_filters( 'tainacan-admin-i18n', [ 'label_page' => __( 'Page', 'tainacan' ), 'label_current_page' => __( 'Current page', 'tainacan' ), 'label_shrink_menu' => __( 'Shrink menu', 'tainacan' ), + 'label_expand_menu' => __( 'Expand menu', 'tainacan' ), 'label_document_uploaded' => __( 'Document uploaded', 'tainacan' ), /* translators: Filter of the repository, not a repository of filter! */ 'label_repository_filter' => __( 'Repository filter', 'tainacan' ), diff --git a/tests/__test-api-exposers.php b/tests/__test-api-exposers.php index fb61f84ca..bfedf2239 100644 --- a/tests/__test-api-exposers.php +++ b/tests/__test-api-exposers.php @@ -536,5 +536,3 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase { } */// TODO automate test this } - -?> \ No newline at end of file diff --git a/tests/test-api-importers.php b/tests/test-api-importers.php index 1e064b01f..0efc9522b 100644 --- a/tests/test-api-importers.php +++ b/tests/test-api-importers.php @@ -7,10 +7,8 @@ namespace Tainacan\Tests; */ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase { - public function test_create(){ - $params = json_encode([ - 'importer_slug' => 'csv' - ]); + public function test_create() { + $params = json_encode([ 'importer_slug' => 'csv' ]); $request = new \WP_REST_Request('POST', $this->namespace . '/importers/session'); $request->set_body($params); @@ -54,6 +52,4 @@ class TAINACAN_REST_Importers_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals(1234, $__importer->get_collections()[0]['total_items']); } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/tests/test-api-logs.php b/tests/test-api-logs.php index 0fd20684c..a31022047 100644 --- a/tests/test-api-logs.php +++ b/tests/test-api-logs.php @@ -64,5 +64,3 @@ class TAINACAN_REST_Logs_Controller extends TAINACAN_UnitApiTestCase { } } - -?> \ No newline at end of file diff --git a/tests/test-api-metadata.php b/tests/test-api-metadata.php index 72a0b47a2..83ff55873 100644 --- a/tests/test-api-metadata.php +++ b/tests/test-api-metadata.php @@ -952,5 +952,3 @@ class TAINACAN_REST_Metadata_Controller extends TAINACAN_UnitApiTestCase { } } - -?> diff --git a/tests/test-api-metadatum-types.php b/tests/test-api-metadatum-types.php index 03334c514..ab06f8c53 100644 --- a/tests/test-api-metadatum-types.php +++ b/tests/test-api-metadatum-types.php @@ -8,23 +8,14 @@ namespace Tainacan\Tests; class TAINACAN_REST_Metadata_Types_Controller extends TAINACAN_UnitApiTestCase { public function test_get_metadata_types(){ - $ftype_request = new \WP_REST_Request('GET', $this->namespace . '/metadata-types'); - $ftype_response = $this->server->dispatch($ftype_request); - $data = $ftype_response->get_data(); - $Tainacan_Metadata = \Tainacan\Repositories\Metadata::get_instance(); - $metadata_types = $Tainacan_Metadata->fetch_metadata_types('NAME'); - $this->assertEquals(count($metadata_types), count($data)); - foreach ($data as $ftype){ $this->assertContains($ftype['name'], $metadata_types); } } } - -?> \ No newline at end of file diff --git a/tests/test-api-private-files.php b/tests/test-api-private-files.php index 3e21024e9..a8b18301b 100644 --- a/tests/test-api-private-files.php +++ b/tests/test-api-private-files.php @@ -7,8 +7,6 @@ namespace Tainacan\Tests; */ class TAINACAN_REST_Private_Files extends TAINACAN_UnitApiTestCase { - - public function test_create_item() { $orig_file = './tests/attachment/codeispoetrywp.jpg'; @@ -62,12 +60,9 @@ class TAINACAN_REST_Private_Files extends TAINACAN_UnitApiTestCase { $folder = 'tainacan-items/' . $this->collection->get_id() . '/' . $this->item->get_id(); $this->assertContains( $folder, $attachment_data['file'] ); - - } function test_internal_api() { - $orig_file = './tests/attachment/codeispoetrywp.jpg'; $this->test_file = '/tmp/codeispoetrywp.jpg'; copy( $orig_file, $this->test_file ); @@ -94,15 +89,10 @@ class TAINACAN_REST_Private_Files extends TAINACAN_UnitApiTestCase { ); $attachment_id = \Tainacan\Media::get_instance()->insert_attachment_from_file($this->test_file, $this->item->get_id()); - $attachment_data = wp_get_attachment_metadata($attachment_id); - $folder = 'tainacan-items/' . $this->collection->get_id() . '/' . $this->item->get_id(); $this->assertContains( $folder, $attachment_data['file'] ); - } } - -?> \ No newline at end of file diff --git a/tests/test-api-queries.php b/tests/test-api-queries.php index 29a47542b..c9040643d 100644 --- a/tests/test-api-queries.php +++ b/tests/test-api-queries.php @@ -8,9 +8,7 @@ namespace Tainacan\Tests; class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { public function test_queries(){ - // Populate the database - $collectionB = $this->tainacan_entity_factory->create_entity( 'collection', [ @@ -57,8 +55,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { ), true ); - // Create Term + // Create Term $termA = $this->tainacan_entity_factory->create_entity( 'term', array( @@ -173,8 +171,6 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { $this->assertCount(1, $data1); $this->assertEquals($collectionB->get_name(), $data1[0]['name']); - - // Search collection with a specific keyword and not other keyword $search_query = ['search' => 'Collection -A']; @@ -190,14 +186,9 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { $names = [$data2[0]['name'], $data2[1]['name']]; $this->assertNotContains('A', $names); - - /* Meta Query: - * * Fetch items from a collection desc ordered by metadatumA1 and its only in range A to F. - * * */ - $meta_query = [ 'metakey' => $metadatumA1->get_id(), 'orderby' => 'meta_value', @@ -212,16 +203,11 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { ]; $meta_query_request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collectionA->get_id() . '/items'); - $meta_query_request->set_query_params($meta_query); - $meta_query_response = $this->server->dispatch($meta_query_request); $data3 = $meta_query_response->get_data()['items']; - $this->assertCount(2, $data3); - $metadatumA1_slug = $metadatumA1->get_slug(); - $values = [$data3[0]['metadata'][$metadatumA1_slug]['value'], $data3[1]['metadata'][$metadatumA1_slug]['value']]; $this->assertNotContains('G', $values); @@ -230,13 +216,9 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { $this->assertEquals('E', $data3[0]['metadata'][$metadatumA1_slug]['value']); $this->assertEquals('D', $data3[1]['metadata'][$metadatumA1_slug]['value']); - /* Date Query: - * * Fetch posts for today - * * */ - $today = getdate(); $date_query = [ @@ -268,11 +250,8 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { $this->assertCount(0, $data5); /* Tax Query - * * Fetch items under a taxonomy with a specific term - * * */ - $tax_query = [ 'taxquery' => [ [ @@ -300,6 +279,4 @@ class TAINACAN_REST_Queries extends TAINACAN_UnitApiTestCase { $this->assertContains('Item A-2', $itemsA1_A2); $this->assertNotContains('Item A-3', $itemsA1_A2); } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/tests/test-api-roles.php b/tests/test-api-roles.php index 09c66eed9..7acd7fa6c 100644 --- a/tests/test-api-roles.php +++ b/tests/test-api-roles.php @@ -7,8 +7,6 @@ namespace Tainacan\Tests; * */ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { - - public function setUp() { parent::setUp(); // reset WP_Roles object. Possible bug was cleaning database between tests, but not the object @@ -35,7 +33,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $data = $name_response->get_data(); $this->assertArrayHasKey('tainacan-new-role', $data); $this->assertEquals('New role', $data['tainacan-new-role']['name']); - } public function test_create_remove_roles() { @@ -58,13 +55,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $name_response = $this->server->dispatch($request); $data = $name_response->get_data(); $this->assertArrayNotHasKey('tainacan-super-role', $data); - - - } public function test_edit_role() { - $request = new \WP_REST_Request('POST', $this->namespace . '/roles'); $request->set_query_params(['name' => 'New role']); @@ -92,29 +85,24 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals('Changed name', $role['name']); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); - $request->set_query_params( [ 'add_cap' => 'manage_options' ] ); - $response = $this->server->dispatch($request); $this->assertEquals( 400, $response->get_status() ); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); - $request->set_query_params( [ 'add_cap' => 'manage_tainacan_collection_234' ] ); - $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status() ); - } public function test_edit_role_validation() { @@ -171,33 +159,27 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status() ); - } public function test_get_role() { $request = new \WP_REST_Request('GET', $this->namespace . '/roles/administrator'); - $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - $this->assertEquals( translate_user_role('Administrator'), $data['name'] ); $this->assertArrayHasKey('manage_tainacan', $data['capabilities']); $this->assertTrue($data['capabilities']['manage_tainacan']); - } public function test_get_roles() { $request = new \WP_REST_Request('GET', $this->namespace . '/roles'); - $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status() ); $data = $response->get_data(); - foreach (\tainacan_roles()->get_tainacan_roles() as $role => $r) { $this->assertArrayHasKey($role, $data); } @@ -210,7 +192,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $request->set_query_params(['name' => 'New role']); $create = $this->server->dispatch($request); - //var_dump($create); $this->assertEquals( 201, $create->get_status() ); $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); @@ -267,7 +248,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { } public function test_get_collection_caps() { - $collection = $this->tainacan_entity_factory->create_entity( 'collection', array( @@ -290,9 +270,7 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $contributor->add_cap( 'tnc_col_all_edit_items' ); $contributor->add_cap( 'tnc_col_all_edit_published_items' ); - $request = new \WP_REST_Request('GET', $this->namespace . '/collection/' . $collection->get_id() . '/capabilities'); - $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status() ); @@ -308,11 +286,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertArrayHasKey('contributor', $caps['tnc_col_' . $collection->get_id() . '_edit_published_items']['roles_inherited']); $this->assertArrayHasKey('administrator', $caps['tnc_col_' . $collection->get_id() . '_delete_published_items']['roles_inherited']); - } function test_get_repo_capabilities() { - $role = add_role('test', 'test', ['tnc_rep_edit_metadata'=>true]); $request = new \WP_REST_Request('GET', $this->namespace . '/capabilities'); @@ -329,12 +305,9 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertArrayNotHasKey('editor', $caps['manage_tainacan']['roles_inherited']); $this->assertArrayNotHasKey('administrator', $caps['manage_tainacan']['roles_inherited']); - - } function test_edit_collection_users_permission() { - global $current_user; $subscriber = $this->factory()->user->create(array( 'role' => 'subscriber' )); @@ -349,7 +322,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { 'add_cap' => 'tnc_col_12_edit_items' ] ); - $response = $this->server->dispatch($request); $this->assertEquals( 403, $response->get_status(), 'should not be permitted'); @@ -358,21 +330,17 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $current_user = $sub_user; $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor'); - $request->set_query_params( [ 'name' => 'Changed name', 'add_cap' => 'tnc_col_12_edit_items' ] ); - $response = $this->server->dispatch($request); $this->assertEquals( 403, $response->get_status(), 'should still not be permitted because edits name'); - $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor'); - $request->set_query_params( [ 'add_cap' => 'tnc_rep_edit_metadata' @@ -383,9 +351,7 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals( 403, $response->get_status(), 'should not be permitted'); - $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/contributor'); - $request->set_query_params( [ 'add_cap' => 'tnc_col_12_edit_items' @@ -395,13 +361,8 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status(), 'should be permitted'); - - } - /** - * @group xis - */ public function test_create_get_roles_with_caps() { $request = new \WP_REST_Request('POST', $this->namespace . '/roles'); @@ -418,7 +379,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals( 201, $create->get_status() ); $request = new \WP_REST_Request('GET', $this->namespace . '/roles'); - //$request->set_query_params($name_query); $name_response = $this->server->dispatch($request); @@ -430,13 +390,10 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertArrayHasKey('tnc_rep_edit_collections', $role['capabilities']); $this->assertTrue($role['capabilities']['tnc_rep_edit_collections']); - } public function test_edit_role_with_caps() { - $request = new \WP_REST_Request('POST', $this->namespace . '/roles'); - $request->set_query_params([ 'name' => 'New role', 'capabilities' => [ @@ -468,8 +425,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertArrayHasKey('manage_tainacan_collection_123', $data['capabilities']); $this->assertTrue($data['capabilities']['manage_tainacan_collection_123']); - - // EDIT $request = new \WP_REST_Request('PATCH', $this->namespace . '/roles/tainacan-new-role'); @@ -491,7 +446,6 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertEquals( 200, $response->get_status() ); $request = new \WP_REST_Request('GET', $this->namespace . '/roles/tainacan-new-role'); - $response = $this->server->dispatch($request); $this->assertEquals( 200, $response->get_status() ); @@ -510,27 +464,18 @@ class TAINACAN_REST_Roles_Controller extends TAINACAN_UnitApiTestCase { $this->assertArrayNotHasKey('tnc_rep_edit_taxonomies', $data['capabilities']); $this->assertArrayNotHasKey('manage_tainacan_collection_123', $data['capabilities']); - - - } function test_new_role_can_read() { $request = new \WP_REST_Request('POST', $this->namespace . '/roles'); - $request->set_query_params(['name' => 'New role']); - $create = $this->server->dispatch($request); $this->assertEquals( 201, $create->get_status() ); - $role = get_role('tainacan-new-role'); $this->assertTrue( $role->has_cap( 'read' ) ); - } -} - -?> +} \ No newline at end of file diff --git a/tests/test-taxonomies.php b/tests/test-taxonomies.php index e02ab29ba..8463d2041 100644 --- a/tests/test-taxonomies.php +++ b/tests/test-taxonomies.php @@ -3,17 +3,16 @@ namespace Tainacan\Tests; /** - * Class TestCollections + * Class Taxonomies * * @package Test_Tainacan */ /** - * Sample test case. + * Taxonomies test case. */ class Taxonomies extends TAINACAN_UnitTestCase { - /** * Teste da insercao de uma taxonomia simples */ @@ -334,7 +333,6 @@ class Taxonomies extends TAINACAN_UnitTestCase { } function test_brackets_2() { - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $collection = $this->tainacan_entity_factory->create_entity( @@ -411,9 +409,6 @@ class Taxonomies extends TAINACAN_UnitTestCase { $itemMeta2_check = new \Tainacan\Entities\Item_Metadata_Entity($i2, $metadatum); $this->assertEquals('[Rock]', $itemMeta2_check->get_value()->get_name()); - - - } function test_metadata_taxonomy_term_count() { @@ -538,7 +533,6 @@ class Taxonomies extends TAINACAN_UnitTestCase { $itemMeta1_repo->validate(); $Tainacan_Item_Metadata->insert($itemMeta1_repo); - $i2 = $this->tainacan_entity_factory->create_entity( 'item', array( @@ -587,8 +581,6 @@ class Taxonomies extends TAINACAN_UnitTestCase { function test_term_taxonomy_filtered_by_collections() { $Tainacan_Taxonomies = \Tainacan\Repositories\Taxonomies::get_instance(); - $Tainacan_Terms = \Tainacan\Repositories\Terms::get_instance(); - $Tainacan_Item_Metadata = \Tainacan\Repositories\Item_Metadata::get_instance(); $tax = $this->tainacan_entity_factory->create_entity( 'taxonomy', @@ -704,7 +696,6 @@ class Taxonomies extends TAINACAN_UnitTestCase { $response = $matches[3][0]; $this->assertEquals($response, $expected_response); - $meta_query = [ 'metaquery' => [ [ diff --git a/tests/test-utilities.php b/tests/test-utilities.php index b4434bf31..94d230e65 100644 --- a/tests/test-utilities.php +++ b/tests/test-utilities.php @@ -9,14 +9,11 @@ namespace Tainacan\Tests; */ /** - * Sample test case. + * TestUtilities */ class TestUtilities extends TAINACAN_UnitTestCase { - function test_initials() { - - $string = 'Roberto Carlos'; $this->assertEquals('RC', tainacan_get_initials($string)); @@ -42,7 +39,6 @@ class TestUtilities extends TAINACAN_UnitTestCase { $string = ''; $this->assertEquals('', tainacan_get_initials($string)); - } function test_get_descendants_ids() {