|
|
@ -2,6 +2,8 @@ |
|
|
|
|
|
|
|
namespace App\Console\Commands; |
|
|
|
|
|
|
|
use App\Events\SDFailedToApply; |
|
|
|
use App\Listeners\AnalyzeOperationPerformance; |
|
|
|
use App\SearchDisplace\Ingest\SendDocument; |
|
|
|
use App\SearchDisplace\SearchAndDisplaceOriginalDocument; |
|
|
|
use Illuminate\Console\Command; |
|
|
@ -36,8 +38,6 @@ class RunSearchDisplace extends Command |
|
|
|
public function __construct() |
|
|
|
{ |
|
|
|
parent::__construct(); |
|
|
|
|
|
|
|
// @TODO Add way to handle 'displace'.. Right now we are doing the 'replace' with ':'.
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -51,21 +51,73 @@ class RunSearchDisplace extends Command |
|
|
|
$original = $this->option('original'); |
|
|
|
|
|
|
|
try { |
|
|
|
$this->bootAnalyzer($documentPath); |
|
|
|
$isDirectory = is_dir($documentPath); |
|
|
|
|
|
|
|
if ( ! $original) { |
|
|
|
$this->runMarkdownOperation($documentPath, $searchers); |
|
|
|
if ($isDirectory) { |
|
|
|
$this->handleDirectory($documentPath, $original, $searchers); |
|
|
|
} else { |
|
|
|
$this->runOriginalDocumentOperation($documentPath, $searchers); |
|
|
|
$this->handleDocument($documentPath, $original, $searchers); |
|
|
|
} |
|
|
|
|
|
|
|
$this->info('Processing document..'); |
|
|
|
$this->info('Processing ' . $isDirectory ? 'directory' : 'document' . '..'); |
|
|
|
$this->info('After the processing will be done the result will show up at the same path as the input.'); |
|
|
|
} catch (\Exception $exception) { |
|
|
|
\Illuminate\Support\Facades\Log::info($exception->getTraceAsString()); |
|
|
|
|
|
|
|
$this->error('Something went wrong. (' . $exception->getMessage() . ')'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $path |
|
|
|
* @param $original |
|
|
|
* @param $searchers |
|
|
|
* @throws \Exception |
|
|
|
*/ |
|
|
|
protected function handleDirectory($path, $original, $searchers) |
|
|
|
{ |
|
|
|
$allFiles = $this->getDirContents($path); |
|
|
|
|
|
|
|
$filesCount = count($allFiles); |
|
|
|
|
|
|
|
$this->bootAnalyzer($path, $filesCount); |
|
|
|
|
|
|
|
foreach ($allFiles as $file) { |
|
|
|
try { |
|
|
|
if ( ! $original) { |
|
|
|
$this->runMarkdownOperation($file, $searchers); |
|
|
|
} else { |
|
|
|
$this->runOriginalDocumentOperation($file, $searchers); |
|
|
|
} |
|
|
|
} catch (\Exception $exception) { |
|
|
|
SDFailedToApply::dispatch(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $documentPath |
|
|
|
* @param $original |
|
|
|
* @param $searchers |
|
|
|
* @throws \Exception |
|
|
|
*/ |
|
|
|
protected function handleDocument($documentPath, $original, $searchers) |
|
|
|
{ |
|
|
|
$path = pathinfo($documentPath,PATHINFO_DIRNAME); |
|
|
|
|
|
|
|
$this->bootAnalyzer($path, 1); |
|
|
|
|
|
|
|
try { |
|
|
|
if ( ! $original) { |
|
|
|
$this->runMarkdownOperation($documentPath, $searchers); |
|
|
|
} else { |
|
|
|
$this->runOriginalDocumentOperation($documentPath, $searchers); |
|
|
|
} |
|
|
|
} catch (\Exception $exception) { |
|
|
|
SDFailedToApply::dispatch(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param $documentPath |
|
|
|
* @param $searchers |
|
|
@ -113,12 +165,14 @@ class RunSearchDisplace extends Command |
|
|
|
$storage->put("searchers/$id.json", json_encode($data)); |
|
|
|
} |
|
|
|
|
|
|
|
protected function bootAnalyzer($filePath) |
|
|
|
protected function bootAnalyzer($path, $filesCount) |
|
|
|
{ |
|
|
|
$redis = Redis::connection(); |
|
|
|
|
|
|
|
$redis->set('analyze_performance_time', Carbon::now()->format('U')); |
|
|
|
$redis->set('analyze_performance_path', pathinfo($filePath,PATHINFO_DIRNAME)); |
|
|
|
$redis->set('analyze_performance_path', $path); |
|
|
|
|
|
|
|
$redis->set('analyze_performance_remaining_files', $filesCount); |
|
|
|
} |
|
|
|
|
|
|
|
protected function getSearchers($searchers) |
|
|
@ -206,7 +260,8 @@ class RunSearchDisplace extends Command |
|
|
|
return $searchersList; |
|
|
|
} |
|
|
|
|
|
|
|
protected function getFileMimeType($file) { |
|
|
|
protected function getFileMimeType($file) |
|
|
|
{ |
|
|
|
if (function_exists('finfo_file')) { |
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
|
|
|
$type = finfo_file($finfo, $file); |
|
|
@ -233,4 +288,21 @@ class RunSearchDisplace extends Command |
|
|
|
|
|
|
|
return $type; |
|
|
|
} |
|
|
|
|
|
|
|
protected function getDirContents($dir, &$results = array()) |
|
|
|
{ |
|
|
|
$files = scandir($dir); |
|
|
|
|
|
|
|
foreach ($files as $key => $value) { |
|
|
|
$path = realpath($dir . DIRECTORY_SEPARATOR . $value); |
|
|
|
|
|
|
|
if (!is_dir($path)) { |
|
|
|
$results[] = $path; |
|
|
|
} else if ($value != "." && $value != "..") { |
|
|
|
$this->getDirContents($path, $results); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $results; |
|
|
|
} |
|
|
|
} |