Repo for the search and displace core module including the interface to select files and search and displace operations to run on them.
https://searchanddisplace.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
168 lines
4.7 KiB
168 lines
4.7 KiB
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\SearchDisplace\Ingest\SendDocument;
|
|
use Illuminate\Console\Command;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class RunSearchDisplace extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'sd:run
|
|
{path : The document path}
|
|
{filters* : The filters which will be applied to the search}';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'Run search and displace on document with filters.';
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
*/
|
|
public function handle()
|
|
{
|
|
$documentPath = $this->argument('path');
|
|
$searchers = $this->argument('filters');
|
|
|
|
$id = md5(uniqid(rand(), true));
|
|
$pathDetails = pathinfo($documentPath);
|
|
|
|
$resultedDocumentPath = $pathDetails['dirname'] . '/' . $pathDetails['filename'] . '-displaced.md';
|
|
|
|
try {
|
|
$this->storeSearchers($id, $searchers, $resultedDocumentPath);
|
|
|
|
$sendToIngest = new SendDocument();
|
|
|
|
$sendToIngest->execute($id, [
|
|
'path' => $documentPath,
|
|
'name' => $pathDetails['basename'],
|
|
'type' => $this->getFileMimeType($documentPath),
|
|
]);
|
|
|
|
$this->info('Processing document..');
|
|
$this->info('After the processing will be done the result will show up at the same path as the input.');
|
|
} catch (\Exception $exception) {
|
|
$this->error('Something went wrong. (' . $exception->getMessage() . ')');
|
|
}
|
|
}
|
|
|
|
protected function storeSearchers($id, $searchers, $storeResultPath)
|
|
{
|
|
$data = [
|
|
'searchers' => $this->getSearchers($searchers),
|
|
|
|
'document_path' => $storeResultPath,
|
|
];
|
|
|
|
$storage = Storage::disk('local');
|
|
$storage->put("searchers/$id.json", json_encode($data));
|
|
}
|
|
|
|
protected function getSearchers($searchers)
|
|
{
|
|
if (count($searchers) === 1 && str_contains($searchers[0], '.json')) {
|
|
return $this->getSearchersFromFile($searchers[0]);
|
|
}
|
|
|
|
return $this->getSearchersFromList($searchers);
|
|
}
|
|
|
|
protected function getSearchersFromList($searchers)
|
|
{
|
|
$storage = Storage::disk('local');
|
|
|
|
$list = [];
|
|
|
|
foreach ($searchers as $searcher) {
|
|
$result = explode(':', $searcher);
|
|
|
|
$searcherPath = 'searchers/' . $result[0] . '.json';
|
|
|
|
if ( ! $storage->exists($searcherPath)) {
|
|
throw new \Exception('Searcher does not exist');
|
|
}
|
|
|
|
$list[] = [
|
|
'content' => json_decode($storage->get($searcherPath), true),
|
|
'replace_with' => count($result) > 1 ? $result[1] : '',
|
|
];
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected function getSearchersFromFile($argument)
|
|
{
|
|
$result = explode(':', $argument);
|
|
|
|
if (count($result) > 1) {
|
|
$path = $result[0];
|
|
|
|
$replaceWith = $result[1];
|
|
} else {
|
|
$path = $argument;
|
|
$replaceWith = '';
|
|
}
|
|
|
|
$contents = file_get_contents($path);
|
|
|
|
if ( ! $contents) {
|
|
throw new \Exception('There is no data in the searcher JSON file.');
|
|
}
|
|
|
|
return [
|
|
[
|
|
'content' => json_decode($contents),
|
|
'replace_with' => $replaceWith,
|
|
],
|
|
];
|
|
}
|
|
|
|
protected function getFileMimeType($file) {
|
|
if (function_exists('finfo_file')) {
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
|
$type = finfo_file($finfo, $file);
|
|
finfo_close($finfo);
|
|
} else {
|
|
require_once 'upgradephp/ext/mime.php';
|
|
$type = mime_content_type($file);
|
|
}
|
|
|
|
if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
|
|
$secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode);
|
|
if ($returnCode === 0 && $secondOpinion) {
|
|
$type = $secondOpinion;
|
|
}
|
|
}
|
|
|
|
if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
|
|
require_once 'upgradephp/ext/mime.php';
|
|
$exifImageType = exif_imagetype($file);
|
|
if ($exifImageType !== false) {
|
|
$type = image_type_to_mime_type($exifImageType);
|
|
}
|
|
}
|
|
|
|
return $type;
|
|
}
|
|
}
|