diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php index 0dd8eefb3ac775c5a6114a5e230f5abec32ee0e1..57658a4610384d7971d0e4fa2306c303fa037393 100755 --- a/apps/maarch_entreprise/actions/index_mlb.php +++ b/apps/maarch_entreprise/actions/index_mlb.php @@ -1863,6 +1863,12 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI 'filename' => $storeResult['file_destination_name'], 'fingerprint' => $storeResult['fingerPrint'], ]); + } else { + \Convert\controllers\ConvertPdfController::convert([ + 'resId' => $resId, + 'collId' => 'letterbox_coll', + 'isVersion' => false, + ]); } \History\controllers\HistoryController::add([ 'tableName' => 'res_letterbox', @@ -2056,6 +2062,12 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI return false; } + $customId = \SrcCore\models\CoreConfigModel::getCustomId(); + if (empty($customId)) { + $customId = 'null'; + } + exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$resId} 'letterbox_coll' > /dev/null &"); + if ($attach) { $idDoc = get_value_fields($formValues, 'res_id'); $idDocTab = explode(',', $idDoc); diff --git a/src/app/convert/controllers/FullTextController.php b/src/app/convert/controllers/FullTextController.php new file mode 100644 index 0000000000000000000000000000000000000000..f7f9588a391a09c4931515661267f5f613c62c85 --- /dev/null +++ b/src/app/convert/controllers/FullTextController.php @@ -0,0 +1,109 @@ +<?php + +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. +* +*/ + +/** +* @brief Full Text Controller +* @author dev@maarch.org +*/ + +namespace Convert\controllers; + +use Convert\models\AdrModel; +use Docserver\models\DocserverModel; +use SrcCore\models\CoreConfigModel; +use SrcCore\models\TextFormatModel; +use SrcCore\models\ValidatorModel; + +class FullTextController +{ + public static function indexDocument(array $args) + { + ValidatorModel::notEmpty($args, ['resId', 'collId']); + ValidatorModel::intVal($args, ['resId']); + ValidatorModel::stringType($args, ['collId']); + + $document = AdrModel::getConvertedDocumentById([ + 'select' => ['docserver_id', 'path', 'filename', 'fingerprint'], + 'resId' => $args['resId'], + 'collId' => $args['collId'], + 'type' => 'PDF' + ]); + if (empty($document)) { + return ['errors' => 'Converted document does not exist']; + } + + $docserver = DocserverModel::getByDocserverId(['docserverId' => $document['docserver_id'], 'select' => ['path_template', 'docserver_type_id']]); + if (empty($docserver['path_template']) || !is_dir($docserver['path_template'])) { + return ['errors' => 'Docserver does not exist']; + } + + $pathToDocument = $docserver['path_template'] . str_replace('#', DIRECTORY_SEPARATOR, $document['path']) . $document['filename']; + if (!is_file($pathToDocument)) { + return ['errors' => 'Document not found on docserver']; + } + + $fullTextDocserver = DocserverModel::getCurrentDocserver(['collId' => 'letterbox_coll', 'typeId' => 'FULLTEXT']); + if (empty($fullTextDocserver['path_template']) || !is_dir($fullTextDocserver['path_template'])) { + return ['errors' => 'FullText docserver does not exist']; + } + + if (FullTextController::isDirEmpty($fullTextDocserver['path_template'])) { + $index = \Zend_Search_Lucene::create($fullTextDocserver['path_template']); + } else { + $index = \Zend_Search_Lucene::open($fullTextDocserver['path_template']); + } + + $index->setFormatVersion(\Zend_Search_Lucene::FORMAT_2_3); + \Zend_Search_Lucene_Analysis_Analyzer::setDefault(new \Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8Num_CaseInsensitive()); + $index->setMaxBufferedDocs(1000); + + $tmpFile = CoreConfigModel::getTmpPath() . basename($pathToDocument) . rand() . '.txt'; + exec("pdftotext " . escapeshellarg($pathToDocument) . " " . escapeshellarg($tmpFile)); + + if (!is_file($tmpFile)) { + return ['errors' => 'Command pdftotext did not work']; + } + + $fp = fopen($tmpFile, "r"); + $fileContent = fread($fp, filesize($tmpFile)); + fclose($fp); + + $fileContent = trim($fileContent); + $fileContent = TextFormatModel::normalize(['string' => $fileContent]); + $fileContent = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $fileContent); + + $doc = new \Zend_Search_Lucene_Document(); + + $doc->addField(\Zend_Search_Lucene_Field::UnIndexed('Id', (integer)$args['resId'])); + $doc->addField(\Zend_Search_Lucene_Field::UnStored('contents', $fileContent)); + + $index->addDocument($doc); + $index->commit(); + $index->optimize(); + + unlink($tmpFile); + + return ['success' => 'success']; + } + + public static function isDirEmpty($dir) + { + $dir = opendir($dir); + $isEmpty = true; + while (($entry = readdir($dir)) !== false) { + if ($entry !== '.' && $entry !== '..') { + $isEmpty = false; + break; + } + } + closedir($dir); + + return $isEmpty; + } +} diff --git a/src/app/convert/models/AdrModel.php b/src/app/convert/models/AdrModel.php index f46e02732e41fc06b20685e8edf959c8eca7f4c7..e3829f0f552592cc4f5ff317208d4a3565ce6d56 100755 --- a/src/app/convert/models/AdrModel.php +++ b/src/app/convert/models/AdrModel.php @@ -28,7 +28,7 @@ class AdrModel if ($aArgs['collId'] == 'letterbox_coll') { $table = "adr_letterbox"; - } else if ($aArgs['isVersion']) { + } else if ($aArgs['collId'] == 'version_attachments_coll' || $aArgs['isVersion']) { $table = "adr_attachments_version"; } else { $table = "adr_attachments"; diff --git a/src/app/convert/scripts/FullTextScript.php b/src/app/convert/scripts/FullTextScript.php new file mode 100644 index 0000000000000000000000000000000000000000..dced47cea1cb280f2173f8771008b9f2e2ac3032 --- /dev/null +++ b/src/app/convert/scripts/FullTextScript.php @@ -0,0 +1,64 @@ +<?php + +/** + * Copyright Maarch since 2008 under licence GPLv3. + * See LICENCE.txt file at the root folder for more details. + * This file is part of Maarch software. + * + */ + +/** + * @brief Full Text Script + * @author dev@maarch.org + */ + +namespace Convert\scripts; + +require 'vendor/autoload.php'; + +use Attachment\models\AttachmentModel; +use Convert\controllers\FullTextController; +use Resource\models\ResModel; +use SrcCore\models\DatabasePDO; + +//customId = $argv[1]; +//resId = $argv[2]; +//collId = $argv[3]; + +FullTextScript::index(['customId' => $argv[1], 'resId' => $argv[2], 'collId' => $argv[3]]); + +class FullTextScript +{ + public static function index(array $args) + { + DatabasePDO::reset(); + new DatabasePDO(['customId' => $args['customId']]); + + $isIndexed = FullTextController::indexDocument(['resId' => $args['resId'], 'collId' => $args['collId']]); + if (!empty($isIndexed['success'])) { + if ($args['collId'] == 'letterbox_coll') { + ResModel::update(['set' => ['fulltext_result' => 'SUCCESS'], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]); + } else { + AttachmentModel::update([ + 'set' => ['fulltext_result' => 'SUCCESS'], + 'where' => ['res_id = ?'], + 'data' => [$args['resId']], + 'isVersion' => $args['collId'] == 'version_attachments_coll' + ]); + } + } else { + if ($args['collId'] == 'letterbox_coll') { + ResModel::update(['set' => ['fulltext_result' => 'ERROR'], 'where' => ['res_id = ?'], 'data' => [$args['resId']]]); + } else { + AttachmentModel::update([ + 'set' => ['fulltext_result' => 'ERROR'], + 'where' => ['res_id = ?'], + 'data' => [$args['resId']], + 'isVersion' => $args['collId'] == 'version_attachments_coll' + ]); + } + } + + return $isIndexed; + } +}