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.

66 lines
1.5 KiB

  1. <?php
  2. namespace App\SearchDisplace;
  3. use Symfony\Component\Process\Process;
  4. class SearchAndDisplaceXML
  5. {
  6. protected $file;
  7. protected $searchers;
  8. public function __construct($file, $searchers)
  9. {
  10. $this->file = $file;
  11. $this->searchers = $searchers;
  12. }
  13. public function execute()
  14. {
  15. $this->applySD();
  16. $this->convertToOdt();
  17. }
  18. protected function convertToOdt()
  19. {
  20. (new Process(['soffice', '--convert-to', 'odt', $this->file, '--outdir', storage_path('app/tmp/')]))->run();
  21. }
  22. protected function applySD()
  23. {
  24. $dom = new \DOMDocument();
  25. $dom->load($this->file);
  26. foreach($dom->getElementsByTagName('p') as $p) {
  27. $is_image = false;
  28. if($p->childNodes) {
  29. foreach($p->childNodes as $child) {
  30. if(isset($child->tagName) && $child->tagName == 'draw:frame') {
  31. $is_image = true;
  32. }
  33. }
  34. }
  35. if(!$is_image) {
  36. $search = new SearchAndDisplace(
  37. stripslashes($p->textContent),
  38. [
  39. 'searchers' => $this->searchers,
  40. ],
  41. false,
  42. true
  43. );
  44. $changed = $search->execute();
  45. if(!$changed) {
  46. continue;
  47. }
  48. $p->textContent = $changed['content'];
  49. }
  50. }
  51. $dom->save($this->file);
  52. }
  53. }