Skip to content

Commit

Permalink
Import resource rights (resolves #47) (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ned Zimmerman authored Oct 3, 2019
1 parent d1cab2c commit 37feee9
Showing 1 changed file with 56 additions and 11 deletions.
67 changes: 56 additions & 11 deletions includes/classes/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
* Class which handles the resource importer.
*/
class Importer {


/**
* An array of column headings from the import source spreadsheet.
*
Expand Down Expand Up @@ -174,7 +176,7 @@ protected function log_error( \WP_Error $error ) {
// Log the data as debug info too.
$data = $error->get_error_data();
if ( ! empty( $data ) ) {
$this->logger->debug( var_export( $data, true ) ); // @codingStandardsIgnoreLine
$this->logger->debug(var_export($data, true)); // @codingStandardsIgnoreLine
}
}

Expand Down Expand Up @@ -415,7 +417,11 @@ protected function parse_row_post( $row ) {
}
break;
case 'Rights':
// TODO: Handle licensing. @see https://github.com/platform-coop-toolkit/learning-commons-framework/issues/5
$rights = $this->map_rights( $this->convert_string_encoding( $val ) );
$meta[] = [
'key' => 'lc_resource_rights',
'value' => $rights,
];
break;
case 'Manual Tags':
case 'Automatic Tags':
Expand Down Expand Up @@ -483,7 +489,7 @@ protected function process_post( $data, $meta, $terms ) {
)
);

do_action( 'resource_importer.process_skipped.resource', $data ); // @codingStandardsIgnoreLine
do_action('resource_importer.process_skipped.resource', $data); // @codingStandardsIgnoreLine

return false;
}
Expand All @@ -500,7 +506,7 @@ protected function process_post( $data, $meta, $terms ) {
)
);

do_action( 'resource_importer.process_already_imported.resource', $data ); // @codingStandardsIgnoreLine
do_action('resource_importer.process_already_imported.resource', $data); // @codingStandardsIgnoreLine

return false;
}
Expand Down Expand Up @@ -535,7 +541,7 @@ protected function process_post( $data, $meta, $terms ) {
);
$this->logger->debug( $post_id->get_error_message() );

do_action( 'resource_importer.process_failed.resource', $post_id, $data, $meta, $terms ); // @codingStandardsIgnoreLine
do_action('resource_importer.process_failed.resource', $post_id, $data, $meta, $terms); // @codingStandardsIgnoreLine
return false;
}

Expand Down Expand Up @@ -581,7 +587,7 @@ protected function process_post( $data, $meta, $terms ) {

$this->process_post_meta( $meta, $post_id, $data );

do_action( 'resource_importer.processed.resource', $post_id, $data, $meta, $terms ); // @codingStandardsIgnoreLine
do_action('resource_importer.processed.resource', $post_id, $data, $meta, $terms); // @codingStandardsIgnoreLine
}

/**
Expand Down Expand Up @@ -654,7 +660,7 @@ protected function process_term( $data ) {
* @param array $data Term data. (Return empty to skip.)
* @param array $meta Meta data.
*/
$data = apply_filters( 'resource_importer.pre_process.term', $data ); // @codingStandardsIgnoreLine
$data = apply_filters('resource_importer.pre_process.term', $data); // @codingStandardsIgnoreLine
if ( empty( $data ) ) {
return false;
}
Expand All @@ -676,7 +682,7 @@ protected function process_term( $data ) {
*
* @param array $data Raw data imported for the term.
*/
do_action( 'resource_importer.process_already_imported.term', $data ); // @codingStandardsIgnoreLine
do_action('resource_importer.process_already_imported.term', $data); // @codingStandardsIgnoreLine

$this->mapping['term'][ $mapping_key ] = $existing;
return false;
Expand Down Expand Up @@ -720,7 +726,7 @@ protected function process_term( $data ) {
* @param array $data Raw data imported for the term.
* @param array $meta Meta data supplied for the term.
*/
do_action( 'resource_importer.process_failed.term', $result, $data ); // @codingStandardsIgnoreLine
do_action('resource_importer.process_failed.term', $result, $data); // @codingStandardsIgnoreLine
return false;
}

Expand All @@ -744,7 +750,7 @@ protected function process_term( $data ) {
* @param int $term_id New term ID.
* @param array $data Raw data imported for the term.
*/
do_action( 'resource_importer.processed.term', $term_id, $data ); // @codingStandardsIgnoreLine
do_action('resource_importer.processed.term', $term_id, $data); // @codingStandardsIgnoreLine
}

/**
Expand Down Expand Up @@ -814,7 +820,7 @@ protected function prefill_existing_terms() {

$query = "SELECT t.term_id, tt.taxonomy, t.slug FROM {$wpdb->terms} AS t";
$query .= " JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id";
$terms = $wpdb->get_results( $query ); // @codingStandardsIgnoreLine
$terms = $wpdb->get_results($query); // @codingStandardsIgnoreLine

foreach ( $terms as $item ) {
$exists_key = sha1( $item->taxonomy . ':' . $item->slug );
Expand Down Expand Up @@ -935,4 +941,43 @@ protected function map_format( $format ) {
return 'articles';
}
}

/**
* Map rights from Excel to resource rights field.
*
* @param string $rights The rights as indicated in the Excel sheet.
*
* @return string The mapped rights value.
*/
protected function map_rights( $rights ) {
$rights = strtolower( $rights );
if ( strpos( $rights, 'attribution-noncommercial-sharealike' ) || strpos( $rights, 'by-nc-sa' ) ) {
return 'cc-by-nc-sa';
}
if ( strpos( $rights, 'attribution-noncommercial-noderivatives' ) || strpos( $rights, 'by-nc-nd' ) ) {
return 'cc-by-nc-nd';
}
if ( strpos( $rights, 'attribution-sharealike' ) || strpos( $rights, 'by-sa' ) ) {
return 'cc-by-sa';
}
if ( strpos( $rights, 'attribution-noderivatives' ) || strpos( $rights, 'by-nd' ) ) {
return 'cc-by-nd';
}
if ( strpos( $rights, 'attribution-noncommercial' ) || strpos( $rights, 'by-nc' ) ) {
return 'cc-by-nc';
}
if ( strpos( $rights, 'attribution' ) || strpos( $rights, 'by' ) ) {
return 'cc-by';
}
if ( strpos( $rights, 'educational community license' ) || strpos( $rights, 'ecl' ) ) {
return 'ecl';
}
if ( strpos( $rights, 'no rights reserved' ) || strpos( $rights, 'zero' ) || strpos( $rights, 'cc0' ) ) {
return 'cc0';
}
if ( strpos( $rights, 'public domain' ) || strpos( $rights, 'no known copyright' ) ) {
return 'public-domain';
}
return 'all-rights-reserved';
}
}

0 comments on commit 37feee9

Please sign in to comment.