Merge branch 'feature/245' into develop

This commit is contained in:
Leo Germani 2019-09-20 09:11:13 -03:00
commit cb94e68c2a
7 changed files with 397 additions and 90 deletions

View File

@ -3,7 +3,8 @@
"description": "Transforme seu site wordpress em um repositório digital.",
"type": "wordpress-plugin",
"require": {
"respect/validation": "^1.1"
"respect/validation": "^1.1",
"smalot/pdfparser": "*"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.2 || ^3.0.2",

199
composer.lock generated
View File

@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "b5a7a81dd7eed9bc122ee36e472c6430",
"content-hash": "e2a614836d1857e45c4f9be57073c2b2",
"packages": [
{
"name": "respect/validation",
"version": "1.1.16",
"version": "1.1.31",
"source": {
"type": "git",
"url": "https://github.com/Respect/Validation.git",
"reference": "020ea1ebb5dc626bb7f1958ff49f69172ff589cc"
"reference": "45d109fc830644fecc1145200d6351ce4f2769d0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Respect/Validation/zipball/020ea1ebb5dc626bb7f1958ff49f69172ff589cc",
"reference": "020ea1ebb5dc626bb7f1958ff49f69172ff589cc",
"url": "https://api.github.com/repos/Respect/Validation/zipball/45d109fc830644fecc1145200d6351ce4f2769d0",
"reference": "45d109fc830644fecc1145200d6351ce4f2769d0",
"shasum": ""
},
"require": {
@ -25,9 +25,9 @@
"symfony/polyfill-mbstring": "^1.2"
},
"require-dev": {
"egulias/email-validator": "~1.2",
"egulias/email-validator": "~1.2 || ~2.1",
"mikey179/vfsstream": "^1.5",
"phpunit/phpunit": "~4.0",
"phpunit/phpunit": "~4.0 || ~5.0",
"symfony/validator": "~2.6.9",
"zendframework/zend-validator": "~2.3"
},
@ -35,7 +35,7 @@
"egulias/email-validator": "Strict (RFC compliant) email validation",
"ext-bcmath": "Arbitrary Precision Mathematics",
"ext-mbstring": "Multibyte String Functions",
"fabpot/php-cs-fixer": "Fix PSR2 and other coding style issues",
"friendsofphp/php-cs-fixer": "Fix PSR2 and other coding style issues",
"symfony/validator": "Use Symfony validator through Respect\\Validation",
"zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation"
},
@ -52,7 +52,7 @@
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD Style"
"BSD-3-Clause"
],
"authors": [
{
@ -67,20 +67,70 @@
"validation",
"validator"
],
"time": "2018-05-19T14:26:44+00:00"
"time": "2019-05-28T06:10:06+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.8.0",
"name": "smalot/pdfparser",
"version": "v0.14.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "3296adf6a6454a050679cde90f95350ad604b171"
"url": "https://github.com/smalot/pdfparser.git",
"reference": "ec72a99028ba5e21a0acad92047b85e128cbf81f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/3296adf6a6454a050679cde90f95350ad604b171",
"reference": "3296adf6a6454a050679cde90f95350ad604b171",
"url": "https://api.github.com/repos/smalot/pdfparser/zipball/ec72a99028ba5e21a0acad92047b85e128cbf81f",
"reference": "ec72a99028ba5e21a0acad92047b85e128cbf81f",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"ext-zlib": "*",
"php": ">=5.3.0",
"tecnickcom/tcpdf": "~6.0"
},
"require-dev": {
"atoum/atoum": "^2.8 | ^3.0"
},
"type": "library",
"autoload": {
"psr-0": {
"Smalot\\PdfParser\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0"
],
"authors": [
{
"name": "Sebastien Malot",
"email": "sebastien@malot.fr"
}
],
"description": "Pdf parser library. Can read and extract information from pdf file.",
"homepage": "http://www.pdfparser.org",
"keywords": [
"extract",
"parse",
"parser",
"pdf",
"text"
],
"time": "2019-01-23T09:14:37+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.12.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "b42a2f66e8f1b15ccf25652c3424265923eb4f17"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/b42a2f66e8f1b15ccf25652c3424265923eb4f17",
"reference": "b42a2f66e8f1b15ccf25652c3424265923eb4f17",
"shasum": ""
},
"require": {
@ -92,7 +142,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "1.8-dev"
"dev-master": "1.12-dev"
}
},
"autoload": {
@ -126,22 +176,84 @@
"portable",
"shim"
],
"time": "2018-04-26T10:06:28+00:00"
"time": "2019-08-06T08:03:45+00:00"
},
{
"name": "tecnickcom/tcpdf",
"version": "6.2.26",
"source": {
"type": "git",
"url": "https://github.com/tecnickcom/TCPDF.git",
"reference": "367241059ca166e3a76490f4448c284e0a161f15"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/tecnickcom/TCPDF/zipball/367241059ca166e3a76490f4448c284e0a161f15",
"reference": "367241059ca166e3a76490f4448c284e0a161f15",
"shasum": ""
},
"require": {
"php": ">=5.3.0"
},
"type": "library",
"autoload": {
"classmap": [
"config",
"include",
"tcpdf.php",
"tcpdf_parser.php",
"tcpdf_import.php",
"tcpdf_barcodes_1d.php",
"tcpdf_barcodes_2d.php",
"include/tcpdf_colors.php",
"include/tcpdf_filters.php",
"include/tcpdf_font_data.php",
"include/tcpdf_fonts.php",
"include/tcpdf_images.php",
"include/tcpdf_static.php",
"include/barcodes/datamatrix.php",
"include/barcodes/pdf417.php",
"include/barcodes/qrcode.php"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0"
],
"authors": [
{
"name": "Nicola Asuni",
"email": "info@tecnick.com",
"role": "lead"
}
],
"description": "TCPDF is a PHP class for generating PDF documents and barcodes.",
"homepage": "http://www.tcpdf.org/",
"keywords": [
"PDFD32000-2008",
"TCPDF",
"barcodes",
"datamatrix",
"pdf",
"pdf417",
"qrcode"
],
"time": "2018-10-16T17:24:05+00:00"
}
],
"packages-dev": [
{
"name": "squizlabs/php_codesniffer",
"version": "3.2.3",
"version": "3.4.2",
"source": {
"type": "git",
"url": "https://github.com/squizlabs/PHP_CodeSniffer.git",
"reference": "4842476c434e375f9d3182ff7b89059583aa8b27"
"reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/4842476c434e375f9d3182ff7b89059583aa8b27",
"reference": "4842476c434e375f9d3182ff7b89059583aa8b27",
"url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8",
"reference": "b8a7362af1cc1aadb5bd36c3defc4dda2cf5f0a8",
"shasum": ""
},
"require": {
@ -174,64 +286,71 @@
}
],
"description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.",
"homepage": "http://www.squizlabs.com/php-codesniffer",
"homepage": "https://github.com/squizlabs/PHP_CodeSniffer",
"keywords": [
"phpcs",
"standards"
],
"time": "2018-02-20T21:35:23+00:00"
"time": "2019-04-10T23:49:02+00:00"
},
{
"name": "wimg/php-compatibility",
"version": "8.1.0",
"version": "9.3.1",
"source": {
"type": "git",
"url": "https://github.com/wimg/PHPCompatibility.git",
"reference": "4ac01e4fe8faaa4f8d3b3cd06ea92e5418ce472e"
"url": "https://github.com/PHPCompatibility/PHPCompatibility.git",
"reference": "9999344e47e7af6b00e1a898eacc4e4368fb7196"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/wimg/PHPCompatibility/zipball/4ac01e4fe8faaa4f8d3b3cd06ea92e5418ce472e",
"reference": "4ac01e4fe8faaa4f8d3b3cd06ea92e5418ce472e",
"url": "https://api.github.com/repos/PHPCompatibility/PHPCompatibility/zipball/9999344e47e7af6b00e1a898eacc4e4368fb7196",
"reference": "9999344e47e7af6b00e1a898eacc4e4368fb7196",
"shasum": ""
},
"require": {
"php": ">=5.3",
"squizlabs/php_codesniffer": "^2.2 || ^3.0.2"
"squizlabs/php_codesniffer": "^2.3 || ^3.0.2"
},
"conflict": {
"squizlabs/php_codesniffer": "2.6.2"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0"
"phpunit/phpunit": "~4.5 || ^5.0 || ^6.0 || ^7.0"
},
"suggest": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.4.3"
"dealerdirect/phpcodesniffer-composer-installer": "^0.5 || This Composer plugin will sort out the PHPCS 'installed_paths' automatically.",
"roave/security-advisories": "dev-master || Helps prevent installing dependencies with known security issues."
},
"type": "phpcodesniffer-standard",
"autoload": {
"psr-4": {
"PHPCompatibility\\": "PHPCompatibility/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"LGPL-3.0"
"LGPL-3.0-or-later"
],
"authors": [
{
"name": "Wim Godden",
"homepage": "https://github.com/wimg",
"role": "lead"
},
{
"name": "Juliette Reinders Folmer",
"homepage": "https://github.com/jrfnl",
"role": "lead"
},
{
"name": "Contributors",
"homepage": "https://github.com/PHPCompatibility/PHPCompatibility/graphs/contributors"
}
],
"description": "A set of sniffs for PHP_CodeSniffer that checks for PHP version compatibility.",
"description": "A set of sniffs for PHP_CodeSniffer that checks for PHP cross-version compatibility.",
"homepage": "http://techblog.wimgodden.be/tag/codesniffer/",
"keywords": [
"compatibility",
"phpcs",
"standards"
],
"time": "2017-12-27T21:58:38+00:00"
"abandoned": "phpcompatibility/php-compatibility",
"time": "2019-09-05T18:36:49+00:00"
}
],
"aliases": [],

View File

@ -54,6 +54,7 @@
:value="`${metadatum.id}-${metadatum.metadata_type_options.taxonomy}-${metadatum.metadata_type_object.primitive_type}`"
:key="metadatum.id"
>{{ metadatum.name }}</option>
<option value="_document_content_index-undefined-string">Documento</option>
</b-select>
</b-field>
@ -549,7 +550,7 @@
if(criteriaKey[2] != 'date' && criteriaKey[2] != 'int' && criteriaKey[2] != 'float'){
this.advancedSearchQuery.metaquery = Object.assign({}, this.advancedSearchQuery.metaquery, {
[`${searchCriterion}`]: {
key: Number(criteriaKey[0]),
key: criteriaKey[0],
compare: 'LIKE',
originalMeta: value,
}

View File

@ -231,4 +231,46 @@ class Media {
throw new \Exception("fatal error");
}
public function index_pdf_content($file, $item_id) {
if ( ! defined('TAINACAN_INDEX_PDF_CONTENT') || true !== TAINACAN_INDEX_PDF_CONTENT ) {
return;
}
$content_index_meta = '_document_content_index';
if ($file == null) {
$meta_id = update_post_meta( $item_id, $content_index_meta, null );
return true;
}
if ( ! \file_exists($file) ) {
return false;
}
if ( $this->get_mime_content_type($file) != 'application/pdf') {
return null;
}
// Allow plugins to implement other approach to index pdf contents
$alternate = apply_filters('tainacan-index-pdf', null, $file, $item_id);
if ( ! \is_null($alternate) ) {
return $alternate;
}
try {
$parser = new \Smalot\PdfParser\Parser();
$content = $parser->parseFile($file)->getText();
$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");
return false;
}
}
}

View File

@ -376,6 +376,25 @@ class Items extends Repository {
return $where;
}
/**
* generate a content of document to index.
*
* @param Entities\Item $item The item
*
* @return boolean
*/
public function generate_index_content(Entities\Item $item) {
$TainacanMedia = \Tainacan\Media::get_instance();
if ( empty( $item->get_document() ) ) {
$TainacanMedia->index_pdf_content( null, $item->get_ID() );
} elseif ( $item->get_document_type() == 'attachment' ) {
if (! wp_attachment_is_image( $item->get_document() ) ) {
$filepath = get_attached_file( $item->get_document() );
$TainacanMedia->index_pdf_content( $filepath, $item->get_ID() );
}
}
return true;
}
/**
* Get a default thumbnail ID from the item document.
@ -452,13 +471,14 @@ class Items extends Repository {
) {
$thumb_id = $this->get_thumbnail_id_from_document( $updated_item );
if ( ! is_null( $thumb_id ) ) {
set_post_thumbnail( $updated_item->get_id(), (int) $thumb_id );
}
}
if (defined('TAINACAN_CONTENT_PDF_INDEX_ACTIVED') && TAINACAN_CONTENT_PDF_INDEX_ACTIVED === true) {
$this->generate_index_content( $updated_item );
}
}
/**

View File

@ -0,0 +1,123 @@
<?php
namespace Tainacan;
use WP_CLI;
use Tainacan\Repositories;
class Cli_Document {
private $collection_repository;
private $items_repository;
private $result_count;
private $dry_run = false;
public function __construct() {
$this->items_repository = Repositories\Items::get_instance();
$this->collection_repository = Repositories\Collections::get_instance();
$this->result_count = ['indexed_documents' => 0];
}
/**
* index content of documents
*
* ## OPTIONS
* [--collection=<value>]
* : <value> Specific ID of the collection into which the document content of the items will be indexed, or 'all' to all collections.
*
*
* [--dry-run]
* : only count the total of item which will index, just output a report
*
* ## EXAMPLES
*
* 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 --collection=all
* indexing documents of items to collection 416: 100% [====================================================] 0:00 / 0:00
* Success:
* 7 items indexed
* indexing documents of items to collection 301: 100% [====================================================] 0:00 / 0:00
* Success:
* 10 items indexed
*
*/
public function __invoke($args, $assoc_args) {
$this->dry_run = false;
if ( !empty($assoc_args['dry-run']) ) {
$this->dry_run = true;
}
if( empty($assoc_args['collection']) ) {
\WP_CLI::error( 'Wrong parameters', true );
}
$collection = $assoc_args['collection'];
if ($collection == 'all') {
$this->index_item_all_collections();
} else {
$this->index_item($collection);
}
}
private function index_item_all_collections() {
$collections = $this->collection_repository->fetch(['posts_per_page'=>-1], 'OBJECT');
foreach ($collections as $collection) {
$this->result_count['indexed_documents'] = 0;
$this->index_item($collection->get_id());
}
}
private function index_item($collection_id) {
$per_page = 50; $page = 1;
$args = [
'posts_per_page'=> $per_page,
'paged' => $page,
'post_status' => get_post_stati()
];
$collection_items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
$total = $collection_items->found_posts;
$last_page = ceil($total/$per_page);
$progress = \WP_CLI\Utils\make_progress_bar( "indexing documents of items to collection $collection_id:", $total );
while ($page++ <= $last_page) {
if ($collection_items->have_posts()) {
while ( $collection_items->have_posts() ) {
$collection_items->the_post();
$item = new Entities\Item($collection_items->post);
$this->index_content_document_item($item);
$progress->tick();
}
}
$args['paged'] = $page;
$collection_items = $this->items_repository->fetch($args, $collection_id, 'WP_Query');
}
$progress->finish();
$msg = "\n" . $this->result_count['indexed_documents'] . " items indexed";
\WP_CLI::success( $msg );
}
private function index_content_document_item($item) {
if (! $item instanceof Entities\Item) {
\WP_CLI::error( 'An item with this ID was not found', true );
}
if ( empty( $item->get_document() ) ) {
return null;
}
$this->result_count['indexed_documents']++;
if ($this->dry_run)
return true;
return $this->items_repository->generate_index_content($item);
}
}
?>

View File

@ -29,6 +29,7 @@ class Cli {
\WP_CLI::add_command('tainacan garbage-collector', 'Tainacan\Cli_Garbage_Collector');
\WP_CLI::add_command('tainacan move-attachments-to-items-folder', 'Tainacan\Cli_Move_Attachments');
\WP_CLI::add_command('tainacan collection', 'Tainacan\Cli_Collection');
\WP_CLI::add_command('tainacan index-content', 'Tainacan\Cli_Document');
}