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

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\SearchDisplace\Ingest\SendDocument;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Storage;
  6. class RunSearchDisplace extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'sd:run
  14. {path : The document path}
  15. {filters* : The filters which will be applied to the search}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Run search and displace on document with filters.';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. */
  35. public function handle()
  36. {
  37. $documentPath = $this->argument('path');
  38. $searchers = $this->argument('filters');
  39. $id = md5(uniqid(rand(), true));
  40. $pathDetails = pathinfo($documentPath);
  41. $resultedDocumentPath = $pathDetails['dirname'] . '/' . $pathDetails['filename'] . '-displaced.md';
  42. try {
  43. $this->storeSearchers($id, $searchers, $resultedDocumentPath);
  44. $sendToIngest = new SendDocument();
  45. $sendToIngest->execute($id, [
  46. 'path' => $documentPath,
  47. 'name' => $pathDetails['basename'],
  48. 'type' => $this->getFileMimeType($documentPath),
  49. ]);
  50. $this->info('Processing document..');
  51. $this->info('After the processing will be done the result will show up at the same path as the input.');
  52. } catch (\Exception $exception) {
  53. $this->error('Something went wrong. (' . $exception->getMessage() . ')');
  54. }
  55. }
  56. protected function storeSearchers($id, $searchers, $storeResultPath)
  57. {
  58. $data = [
  59. 'searchers' => $this->getSearchers($searchers),
  60. 'document_path' => $storeResultPath,
  61. ];
  62. $storage = Storage::disk('local');
  63. $storage->put("searchers/$id.json", json_encode($data));
  64. }
  65. protected function getSearchers($searchers)
  66. {
  67. if (count($searchers) === 1 && str_contains($searchers[0], '.json')) {
  68. return $this->getSearchersFromFile($searchers[0]);
  69. }
  70. return $this->getSearchersFromList($searchers);
  71. }
  72. protected function getSearchersFromList($searchers)
  73. {
  74. $storage = Storage::disk('local');
  75. $list = [];
  76. foreach ($searchers as $searcher) {
  77. $result = explode(':', $searcher);
  78. $searcherPath = 'searchers/' . $result[0] . '.json';
  79. if ( ! $storage->exists($searcherPath)) {
  80. throw new \Exception('Searcher does not exist');
  81. }
  82. $list[] = [
  83. 'content' => json_decode($storage->get($searcherPath), true),
  84. 'replace_with' => count($result) > 1 ? $result[1] : '',
  85. ];
  86. }
  87. return $list;
  88. }
  89. protected function getSearchersFromFile($argument)
  90. {
  91. $result = explode(':', $argument);
  92. if (count($result) > 1) {
  93. $path = $result[0];
  94. $replaceWith = $result[1];
  95. } else {
  96. $path = $argument;
  97. $replaceWith = '';
  98. }
  99. $contents = file_get_contents($path);
  100. if ( ! $contents) {
  101. throw new \Exception('There is no data in the searcher JSON file.');
  102. }
  103. return [
  104. [
  105. 'content' => json_decode($contents),
  106. 'replace_with' => $replaceWith,
  107. ],
  108. ];
  109. }
  110. protected function getFileMimeType($file) {
  111. if (function_exists('finfo_file')) {
  112. $finfo = finfo_open(FILEINFO_MIME_TYPE);
  113. $type = finfo_file($finfo, $file);
  114. finfo_close($finfo);
  115. } else {
  116. require_once 'upgradephp/ext/mime.php';
  117. $type = mime_content_type($file);
  118. }
  119. if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
  120. $secondOpinion = exec('file -b --mime-type ' . escapeshellarg($file), $foo, $returnCode);
  121. if ($returnCode === 0 && $secondOpinion) {
  122. $type = $secondOpinion;
  123. }
  124. }
  125. if (!$type || in_array($type, array('application/octet-stream', 'text/plain'))) {
  126. require_once 'upgradephp/ext/mime.php';
  127. $exifImageType = exif_imagetype($file);
  128. if ($exifImageType !== false) {
  129. $type = image_type_to_mime_type($exifImageType);
  130. }
  131. }
  132. return $type;
  133. }
  134. }