From 25e1e3f6e1fbfda240955042a2f469a7c0c0991a Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 17 May 2019 11:21:18 +0200
Subject: [PATCH] FEAT #10525 TIME 1:30 Search with docservers + fix search
 versions + log

---
 apps/maarch_entreprise/actions/index_mlb.php  |  3 ++-
 .../indexing_searching/search_adv_result.php  | 20 ++++++++++---------
 modules/attachments/attachments_content.php   |  6 ++++--
 .../controllers/FullTextController.php        |  9 ++++++---
 src/app/convert/scripts/FullTextScript.php    | 17 +++++++++++++++-
 5 files changed, 39 insertions(+), 16 deletions(-)

diff --git a/apps/maarch_entreprise/actions/index_mlb.php b/apps/maarch_entreprise/actions/index_mlb.php
index 57658a46103..8c3d56d79b5 100755
--- a/apps/maarch_entreprise/actions/index_mlb.php
+++ b/apps/maarch_entreprise/actions/index_mlb.php
@@ -2066,7 +2066,8 @@ function manage_form($arrId, $history, $actionId, $label_action, $status, $collI
     if (empty($customId)) {
         $customId = 'null';
     }
-    exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$resId} 'letterbox_coll' > /dev/null &");
+    $user = \User\models\UserModel::getByLogin(['select' => ['id'], 'login' => $_SESSION['user']['UserId']]);
+    exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$resId} 'letterbox_coll' {$user['id']} > /dev/null &");
 
     if ($attach) {
         $idDoc = get_value_fields($formValues, 'res_id');
diff --git a/apps/maarch_entreprise/indexing_searching/search_adv_result.php b/apps/maarch_entreprise/indexing_searching/search_adv_result.php
index f8a3c669842..cd4d5e0419b 100755
--- a/apps/maarch_entreprise/indexing_searching/search_adv_result.php
+++ b/apps/maarch_entreprise/indexing_searching/search_adv_result.php
@@ -368,8 +368,10 @@ if (count($_REQUEST['meta']) > 0) {
                     
                     $_SESSION['search']['plain_text'] = $_REQUEST['fulltext'];
 
-                    foreach ($_SESSION['collections'] as $key => $tmpCollection) {
-                        $path_to_lucene_index = $tmpCollection['path_to_lucene_index'];
+                    foreach (['letterbox_coll', 'attachments_coll', 'attachments_version_coll'] as $key => $tmpCollection) {
+                        $fullTextDocserver = \Docserver\models\DocserverModel::getCurrentDocserver(['collId' => $tmpCollection, 'typeId' => 'FULLTEXT']);
+
+                        $path_to_lucene_index = $fullTextDocserver['path_template'];
 
                         if (is_dir($path_to_lucene_index)) {
                             if (!$func->isDirEmpty($path_to_lucene_index)) {
@@ -386,10 +388,10 @@ if (count($_REQUEST['meta']) > 0) {
                                     $cptIds ++;
                                 }
 
-                                if ($tmpCollection['table'] == 'res_attachments') {
+                                if ($tmpCollection == 'attachments_coll') {
                                     $tmpArray = preg_split("/[,' ]/", $Liste_Ids);
                                     array_splice($tmpArray, 0, 1);
-                                    $_SESSION['fullTextAttachments']['attachments'] = $tmpArray;
+                                    $_SESSION['fullTextAttachments']['attachments'] = array_filter($tmpArray);
                                     $db = new Database();
                                     $stmt = $db->query("SELECT DISTINCT res_id_master FROM res_attachments WHERE res_id IN ($Liste_Ids) AND status NOT IN ('DEL','OBS','TMP') AND attachment_type NOT IN ('print_folder')");
                                     $idMasterDatas = [];
@@ -402,10 +404,10 @@ if (count($_REQUEST['meta']) > 0) {
                                         $Liste_Ids .= ", '{$tmpIdMaster->res_id_master}'";
                                         $_SESSION['fullTextAttachments']['letterbox'][] = $tmpIdMaster->res_id_master;
                                     }
-                                } elseif ($tmpCollection['table'] == 'res_version_attachments') {
+                                } elseif ($tmpCollection == 'attachments_version_coll') {
                                     $tmpArray = preg_split("/[,' ]/", $Liste_Ids);
                                     array_splice($tmpArray, 0, 1);
-                                    $_SESSION['fullTextAttachments']['versionAttachments'] = $tmpArray;
+                                    $_SESSION['fullTextAttachments']['versionAttachments'] = array_filter($tmpArray);
                                     $db = new Database();
                                     $stmt = $db->query("SELECT DISTINCT res_id_master FROM res_version_attachments WHERE res_id IN ($Liste_Ids) AND status NOT IN ('DEL','OBS','TMP') AND attachment_type NOT IN ('print_folder')");
                                     $idMasterDatas = [];
@@ -426,7 +428,7 @@ if (count($_REQUEST['meta']) > 0) {
 
                                 $where_request .= " res_id IN ($Liste_Ids) ";
 
-                                if (empty($_SESSION['collections'][$key + 1])) {
+                                if ($key == 2) {
                                     $where_request .= ') and ';
                                 } else {
                                     $where_request .= ' or ';
@@ -438,7 +440,7 @@ if (count($_REQUEST['meta']) > 0) {
 
                                 $where_request .= " 1=-1 ";
 
-                                if (empty($_SESSION['collections'][$key + 1])) {
+                                if ($key == 2) {
                                     $where_request .= ') and ';
                                 } else {
                                     $where_request .= ' or ';
@@ -451,7 +453,7 @@ if (count($_REQUEST['meta']) > 0) {
 
                             $where_request .= " 1=-1 ";
 
-                            if (empty($_SESSION['collections'][$key + 1])) {
+                            if ($key == 2) {
                                 $where_request .= ') and ';
                             } else {
                                 $where_request .= ' or ';
diff --git a/modules/attachments/attachments_content.php b/modules/attachments/attachments_content.php
index 32fce1944f3..29f51a57428 100755
--- a/modules/attachments/attachments_content.php
+++ b/modules/attachments/attachments_content.php
@@ -544,7 +544,8 @@ if (isset($_POST['add']) && $_POST['add']) {
                                 if (empty($customId)) {
                                     $customId = 'null';
                                 }
-                                exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$id} 'attachments_coll' > /dev/null &");
+                                $user = \User\models\UserModel::getByLogin(['select' => ['id'], 'login' => $_SESSION['user']['UserId']]);
+                                exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$id} 'attachments_coll' {$user['id']} > /dev/null &");
 
                                 if ($id == false) {
                                     $error = $resAttach->get_error();
@@ -1232,7 +1233,8 @@ if (isset($_POST['add']) && $_POST['add']) {
     if (empty($customId)) {
         $customId = 'null';
     }
-    exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$id} {$targetCollId} > /dev/null &");
+    $user = \User\models\UserModel::getByLogin(['select' => ['id'], 'login' => $_SESSION['user']['UserId']]);
+    exec("php src/app/convert/scripts/FullTextScript.php {$customId} {$id} {$targetCollId} {$user['id']} > /dev/null &");
 
     if (empty($error)) {
         //DELETE TEMPORARY BACKUP
diff --git a/src/app/convert/controllers/FullTextController.php b/src/app/convert/controllers/FullTextController.php
index aebf1b6a0c9..71bc737a4b5 100644
--- a/src/app/convert/controllers/FullTextController.php
+++ b/src/app/convert/controllers/FullTextController.php
@@ -51,6 +51,8 @@ class FullTextController
         $fullTextDocserver = DocserverModel::getCurrentDocserver(['collId' => $args['collId'], 'typeId' => 'FULLTEXT']);
         if (empty($fullTextDocserver['path_template']) || !is_dir($fullTextDocserver['path_template'])) {
             return ['errors' => 'FullText docserver does not exist'];
+        } elseif (!is_writable($fullTextDocserver['path_template'])) {
+            return ['errors' => 'FullText docserver is not writable'];
         }
 
         if (FullTextController::isDirEmpty($fullTextDocserver['path_template'])) {
@@ -64,19 +66,20 @@ class FullTextController
         $index->setMaxBufferedDocs(1000);
 
         $tmpFile = CoreConfigModel::getTmpPath() . basename($pathToDocument) . rand() . '.txt';
-        exec("pdftotext " . escapeshellarg($pathToDocument) . " " . escapeshellarg($tmpFile));
+        $pdfToText = exec("pdftotext " . escapeshellarg($pathToDocument) . " " . escapeshellarg($tmpFile));
 
         if (!is_file($tmpFile)) {
-            return ['errors' => 'Command pdftotext did not work'];
+            return ['errors' => 'Command pdftotext did not work : ' . $pdfToText];
         }
 
         $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);
+        $fileContent = preg_replace('/[[:punct:]]/', ' ', $fileContent);
+        $fileContent = trim($fileContent);
 
         $doc = new \Zend_Search_Lucene_Document();
 
diff --git a/src/app/convert/scripts/FullTextScript.php b/src/app/convert/scripts/FullTextScript.php
index eb2b4e5d67b..f61ca1d9b88 100644
--- a/src/app/convert/scripts/FullTextScript.php
+++ b/src/app/convert/scripts/FullTextScript.php
@@ -19,13 +19,16 @@ require 'vendor/autoload.php';
 use Attachment\models\AttachmentModel;
 use Convert\controllers\FullTextController;
 use Resource\models\ResModel;
+use SrcCore\controllers\LogsController;
 use SrcCore\models\DatabasePDO;
+use User\models\UserModel;
 
 //customId  = $argv[1];
 //resId     = $argv[2];
 //collId    = $argv[3];
+//userId    = $argv[4];
 
-FullTextScript::index(['customId' => $argv[1], 'resId' => $argv[2], 'collId' => $argv[3]]);
+FullTextScript::index(['customId' => $argv[1], 'resId' => $argv[2], 'collId' => $argv[3], 'userId' => $argv[4]]);
 
 class FullTextScript
 {
@@ -34,6 +37,9 @@ class FullTextScript
         DatabasePDO::reset();
         new DatabasePDO(['customId' => $args['customId']]);
 
+        $currentUser = UserModel::getById(['id' => $args['userId'], 'select' => ['user_id']]);
+        $GLOBALS['userId'] = $currentUser['user_id'];
+
         $isIndexed = FullTextController::indexDocument(['resId' => $args['resId'], 'collId' => $args['collId']]);
         if (!empty($isIndexed['success'])) {
             if ($args['collId'] == 'letterbox_coll') {
@@ -57,6 +63,15 @@ class FullTextScript
                     'isVersion' => $args['collId'] == 'attachments_version_coll'
                 ]);
             }
+            LogsController::add([
+                'isTech'    => true,
+                'moduleId'  => 'fullText',
+                'level'     => 'ERROR',
+                'tableName' => $args['collId'],
+                'recordId'  => $args['resId'],
+                'eventType' => "Full Text failed : {$isIndexed['errors']}",
+                'eventId'   => "resId : {$args['resId']} || collId : {$args['collId']}"
+            ]);
         }
 
         return $isIndexed;
-- 
GitLab