From dbec21d502a8e06ab9041d2fe642a85381511e7e Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Wed, 11 Sep 2019 23:53:47 -0300 Subject: [PATCH] improvements on detect encode and WP-Cli #245 --- src/classes/class-tainacan-media.php | 6 +++++- src/cli/class-tainacan-cli-document.php | 18 +++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/classes/class-tainacan-media.php b/src/classes/class-tainacan-media.php index 2412b4ede..730ca9e7f 100644 --- a/src/classes/class-tainacan-media.php +++ b/src/classes/class-tainacan-media.php @@ -260,7 +260,11 @@ class Media { try { $PDF2Text->decodePDF(); $content = preg_replace('~[[:cntrl:]]~', '', $PDF2Text->output()); - $content = mb_convert_encoding($content, 'UTF-8', 'ISO-8859-1'); + + $wp_charset = get_bloginfo('charset'); + $content_charset = mb_detect_encoding($content); + $content = mb_convert_encoding($content, $wp_charset, $content_charset); + $meta_id = update_post_meta( $item_id, $content_index_meta, $content ); } catch(Exception $e) { error_log('Caught exception: ' . $e->getMessage() . "\n"); diff --git a/src/cli/class-tainacan-cli-document.php b/src/cli/class-tainacan-cli-document.php index 0e930446e..fe7256040 100644 --- a/src/cli/class-tainacan-cli-document.php +++ b/src/cli/class-tainacan-cli-document.php @@ -22,8 +22,8 @@ class Cli_Document { * index content of documents * * ## OPTIONS - * [--collection-id=] - * : Specific ID of the collection into which the document content of the items will be indexed, if not informed all collections will be index. + * [--collection=] + * : Specific ID of the collection into which the document content of the items will be indexed, or 'all' to all collections. * * * [--dry-run] @@ -31,13 +31,13 @@ class Cli_Document { * * ## EXAMPLES * - * wp tainacan index-content --collection-id=416 + * wp tainacan index-content --collection=416 * indexing documents of items to collection 416: 100% [====================================================] 0:00 / 0:00 * Success: * 7 items indexed * * - * wp tainacan index-content + * wp tainacan index-content --collection=all * indexing documents of items to collection 416: 100% [====================================================] 0:00 / 0:00 * Success: * 7 items indexed @@ -52,11 +52,15 @@ class Cli_Document { $this->dry_run = true; } - if( empty($assoc_args['collection-id']) ) { + if( empty($assoc_args['collection']) ) { + \WP_CLI::error( 'Wrong parameters', true ); + } + + $collection = $assoc_args['collection']; + if ($collection == 'all') { $this->index_item_all_collections(); } else { - $collection_id = $assoc_args['collection-id']; - $this->index_item($collection_id); + $this->index_item($collection); } }