From 37a2bcf5a888cc0fb5e2390251b1ed4eab4f4ec3 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 4 May 2024 20:42:41 +0200 Subject: [PATCH] NEW Add option MAIN_USE_FULL_TEXT_INDEXATION --- htdocs/core/lib/files.lib.php | 41 +++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php index 37774dd092f..b9d3b628957 100644 --- a/htdocs/core/lib/files.lib.php +++ b/htdocs/core/lib/files.lib.php @@ -2126,6 +2126,47 @@ function addFileIntoDatabaseIndex($dir, $file, $fullpathorig = '', $mode = 'uplo $ecmfile->share = getRandomPassword(true); } + // Use a convertisser Doc to Text + $useFullTextIndexation = getDolGlobalString('MAIN_USE_FULL_TEXT_INDEXATION'); + $useFullTextIndexation = 1; + if ($useFullTextIndexation) { + $ecmfile->filepath = $rel_dir; + $ecmfile->filename = $filename; + + $filetoprocess = $dir.'/'.$ecmfile->filename; + + $textforfulltextindex = ''; + $keywords = ''; + if (preg_match('/\.pdf/i', $filename)) { + // TODO Move this into external submodule files + + // TODO Develop a native PHP parser using sample code in https://github.com/adeel/php-pdf-parser + + include_once DOL_DOCUMENT_ROOT.'/core/class/utils.class.php'; + $utils = new Utils($db); + $outputfile = $conf->admin->dir_temp.'/tmppdttotext.'.$user->id.'.out'; // File used with popen method + + // We also exclude '/temp/' dir and 'documents/admin/documents' + // We make escapement here and call executeCLI without escapement because we don't want to have the '*.log' escaped. + $cmd = getDolGlobalString('MAIN_USE_FULL_TEXT_INDEXATION_PDFTOTEXT', 'pdftotext')." -htmlmeta '".escapeshellcmd($filetoprocess)."' - "; + $result = $utils->executeCLI($cmd, $outputfile, 0, null, 1); + + if (!$result['error']) { + $txt = $result['output']; + $matches = array(); + if (preg_match('//i', $txt, $matches)) { + $keywords = $matches[1]; + } + if (preg_match('/
(.*)<\/pre>/si', $txt, $matches)) {
+						$textforfulltextindex = dol_string_nospecial($matches[1]);
+					}
+				}
+			}
+
+			$ecmfile->description = $textforfulltextindex;
+			$ecmfile->keywords = $keywords;
+		}
+
 		$result = $ecmfile->create($user);
 		if ($result < 0) {
 			dol_syslog($ecmfile->error);