Parser.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. class Parser extends CI_Controller
  4. {
  5. //TODO: PARAMETRIZAR BBDD -> SI EXISTE UN PROCESO EN MARCHA, NO INICIAR SIGUIENTE DESCARGA, AVISAR
  6. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  7. protected $rutaDiariaFecha = "";
  8. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  9. protected $enlaceAtom = "";
  10. protected $fechaCreacionLog = "";
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $this->load->helper('file');
  15. $this->load->helper('xml');
  16. $this->load->library('parserfile');
  17. $this->load->model("Parser_model");
  18. }
  19. public function index()
  20. {
  21. $existCronRun = $this->Parser_model->existCronRun();
  22. $fechaCarpeta = "";
  23. if ($existCronRun['num_rows'] > 0) {
  24. if ($existCronRun['datos']->cron_ejecucion == 0) {
  25. $this->Parser_model->inicioParser();
  26. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  27. $fechaCarpeta = date("Y-m-d_H-i-s");
  28. $fechaImportacionLog = date("Y-m-d H:i:s");
  29. foreach ($resultSetPerfiles['datos'] as $row) {
  30. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  31. $this->fechaCreacionLog = $fechaCarpeta;
  32. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  33. $this->newFolder($rutaPerfil);
  34. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  35. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  36. $this->downloadZip($row->url_descarga);
  37. }
  38. $files = get_filenames($this->rutaDiariaFecha . "/format");
  39. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  40. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  41. }
  42. $this->tablasCalculadas($idUltimaImportacion);
  43. emailFinProcesoDescarga($fechaImportacionLog);
  44. } else {
  45. echo "EXISTE UN CRON EN MARCHA.";
  46. }
  47. } else {
  48. echo "No existe parametro que iniciar.";
  49. }
  50. $this->Parser_model->stopParser();
  51. /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  52. foreach ($resultSetPerfiles['datos'] as $row) {
  53. $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
  54. } */
  55. }
  56. private function downloadZip($urlXML)
  57. {
  58. $fechaDescarga = $this->getFechaForDownloadZip();
  59. $fileName = basename($urlXML);
  60. $downladedZip = $this->downloadFile($fileName, $urlXML . $fechaDescarga, $this->rutaDiariaFecha);
  61. if ($downladedZip) {
  62. $zip = new ZipArchive;
  63. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  64. $zip->extractTo($this->rutaDiariaFecha);
  65. $zip->close();
  66. unlink($this->rutaDiariaFecha . $fileName);
  67. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  68. foreach ($archivosTotales as $file) {
  69. $this->getFile($this->rutaDiariaFecha . $file, $file);
  70. }
  71. }
  72. }
  73. }
  74. private function getFechaForDownloadZip(): String
  75. {
  76. $result = "_";
  77. $anyo = date("Y");
  78. $mes = date("m");
  79. $mesPrevious = date("m", strtotime("-1 months"));
  80. $yearPrevious = date("Y", strtotime("-1 year"));
  81. $dia = date("d");
  82. if (date("m") == "01") {
  83. $result .= $yearPrevious . $mesPrevious . ".zip";
  84. } else {
  85. if ($dia == "01") {
  86. $result .= $anyo . $mesPrevious . ".zip";
  87. } else {
  88. $result .= $anyo . $mes . ".zip";
  89. }
  90. }
  91. return $result;
  92. }
  93. private function getFile($url, $fileName)
  94. {
  95. $feed = implode(file($url));
  96. $xml = simplexml_load_string($feed);
  97. $json = json_encode($xml);
  98. $array = json_decode($json, true);
  99. $this->formatXml($feed, $fileName);
  100. return $array;
  101. }
  102. private function newFolder($rutaFinal)
  103. {
  104. $result = false;
  105. if (!is_dir($rutaFinal)) {
  106. $result = mkdir($rutaFinal);
  107. mkdir($rutaFinal . "/format");
  108. }
  109. return $result;
  110. }
  111. private function formatXml($xml, $fileName)
  112. {
  113. $openingFormat = $this->regExIniciosEsquema($xml);
  114. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  115. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  116. }
  117. private function downloadFile($fileName, $url, $ruta)
  118. {
  119. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  120. return true;
  121. } else {
  122. return false;
  123. }
  124. }
  125. private function regExIniciosEsquema($xml)
  126. {
  127. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)|(<at:)/";
  128. $result = preg_replace($pattern, "<", $xml);
  129. return $result;
  130. }
  131. private function regExFinalesEsquema($xml)
  132. {
  133. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)|(<\/at:)/";
  134. $result = preg_replace($pattern, "</", $xml);
  135. return $result;
  136. }
  137. private function tablasCalculadas($idUltimaImportacion)
  138. {
  139. $this->Parser_model->truncateTables("vista_datos_licitacion");
  140. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  141. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  142. }
  143. }