Parser.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. $fechaCarpeta = "";
  22. $this->Parser_model->inicioParser();
  23. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  24. $fechaCarpeta = date("Y-m-d_H-i-s");
  25. $fechaImportacionLog = date("Y-m-d H:i:s");
  26. foreach ($resultSetPerfiles['datos'] as $row) {
  27. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  28. $this->Xlsexport_model->getLastDateLog($idUltimaImportacion);
  29. $this->fechaCreacionLog = $fechaCarpeta;
  30. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  31. $this->removeFoldersFromThursdayToMonday($rutaPerfil);
  32. $this->newFolder($rutaPerfil);
  33. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  34. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  35. $this->downloadZip($row->url_descarga);
  36. }
  37. $files = get_filenames($this->rutaDiariaFecha . "/format");
  38. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  39. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  40. }
  41. $this->tablasCalculadas($idUltimaImportacion);
  42. $this->load->library('exportXls');
  43. $this->exportxls->exportDatosCompletos();
  44. $this->Parser_model->stopParser();
  45. }
  46. private function downloadZip($urlXML)
  47. {
  48. $fechaDescarga = $this->getFechaForDownloadZip();
  49. $fileName = basename($urlXML);
  50. $downladedZip = $this->downloadFile($fileName, $urlXML . $fechaDescarga, $this->rutaDiariaFecha);
  51. if ($downladedZip) {
  52. $zip = new ZipArchive;
  53. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  54. $zip->extractTo($this->rutaDiariaFecha);
  55. $zip->close();
  56. unlink($this->rutaDiariaFecha . $fileName);
  57. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  58. foreach ($archivosTotales as $file) {
  59. $this->getFile($this->rutaDiariaFecha . $file, $file);
  60. }
  61. }
  62. }
  63. }
  64. private function getFechaForDownloadZip(): String
  65. {
  66. $result = "_";
  67. $anyo = date("Y");
  68. $mes = date("m");
  69. $mesPrevious = date("m", strtotime("-1 months"));
  70. $yearPrevious = date("Y", strtotime("-1 year"));
  71. $dia = date("d");
  72. if (date("m") == "01") {
  73. $result .= $yearPrevious . $mesPrevious . ".zip";
  74. } else {
  75. if ($dia == "01") {
  76. $result .= $anyo . $mesPrevious . ".zip";
  77. } else {
  78. $result .= $anyo . $mes . ".zip";
  79. }
  80. }
  81. return $result;
  82. }
  83. private function getFile($url, $fileName)
  84. {
  85. $feed = implode(file($url));
  86. $xml = simplexml_load_string($feed);
  87. $json = json_encode($xml);
  88. $array = json_decode($json, true);
  89. $this->formatXml($feed, $fileName);
  90. return $array;
  91. }
  92. private function newFolder($rutaFinal)
  93. {
  94. $result = false;
  95. if (!is_dir($rutaFinal)) {
  96. $result = mkdir($rutaFinal);
  97. mkdir($rutaFinal . "/format");
  98. }
  99. return $result;
  100. }
  101. private function formatXml($xml, $fileName)
  102. {
  103. $openingFormat = $this->regExIniciosEsquema($xml);
  104. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  105. unlink($this->rutaDiariaFecha . "/" . $fileName);
  106. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  107. }
  108. private function downloadFile($fileName, $url, $ruta)
  109. {
  110. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  111. return true;
  112. } else {
  113. return false;
  114. }
  115. }
  116. private function regExIniciosEsquema($xml)
  117. {
  118. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)|(<at:)/";
  119. $result = preg_replace($pattern, "<", $xml);
  120. return $result;
  121. }
  122. private function regExFinalesEsquema($xml)
  123. {
  124. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)|(<\/at:)/";
  125. $result = preg_replace($pattern, "</", $xml);
  126. return $result;
  127. }
  128. private function tablasCalculadas($idUltimaImportacion)
  129. {
  130. $this->Parser_model->truncateTables("vista_datos_licitacion");
  131. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  132. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  133. }
  134. private function removeFoldersFromThursdayToMonday($ruta)
  135. {
  136. $dayWeek = date('l');
  137. if ($dayWeek == "Friday") {
  138. $folders = glob($ruta . "/*");
  139. $thursday = date('Y-m-d', strtotime(date("Y-m-d") . ' -1 days'));
  140. $minusSixDays = date('Y-m-d', strtotime($thursday . ' -6 days'));
  141. foreach ($folders as $folder) {
  142. $folderDate = substr($folder, -19, 10);
  143. if ($folderDate <= $thursday && $folderDate >= $minusSixDays) {
  144. eliminarArchivosProcesoCompleto($folder);
  145. }
  146. }
  147. }
  148. }
  149. }