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.
108 lines
2.7 KiB
108 lines
2.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));
|
|
|
|
$this->storeSearchers($id, $searchers, $documentPath);
|
|
|
|
try {
|
|
$sendToIngest = new SendDocument();
|
|
|
|
$sendToIngest->execute($documentPath, $id);
|
|
|
|
$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, $documentPath)
|
|
{
|
|
$data = [
|
|
'searchers' => $this->getSearchers($searchers),
|
|
'document_path' => $documentPath,
|
|
];
|
|
|
|
$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)
|
|
{
|
|
$list = [];
|
|
|
|
foreach ($searchers as $searcher) {
|
|
$result = explode(':', $searcher);
|
|
|
|
$list[] = [
|
|
'key' => $result[0],
|
|
'replace_with' => $result[1],
|
|
];
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
protected function getSearchersFromFile($path)
|
|
{
|
|
$contents = file_get_contents($path);
|
|
|
|
if ( ! $contents) {
|
|
throw new \Exception('Something went wrong when tried reading from file.');
|
|
}
|
|
|
|
return json_decode($contents);
|
|
}
|
|
}
|