Parser.php 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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->downloadProcess($row->url_descarga);
  37. $this->downloadZip($row->url_descarga);
  38. }
  39. $files = get_filenames($this->rutaDiariaFecha . "/format");
  40. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  41. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  42. }
  43. $this->tablasCalculadas($idUltimaImportacion);
  44. emailFinProcesoDescarga($fechaImportacionLog);
  45. } else {
  46. echo "EXISTE UN CRON EN MARCHA.";
  47. }
  48. } else {
  49. echo "No existe parametro que iniciar.";
  50. }
  51. $this->Parser_model->stopParser();
  52. /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  53. foreach ($resultSetPerfiles['datos'] as $row) {
  54. $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
  55. } */
  56. }
  57. private function downloadProcess($urlXML)
  58. {
  59. $fileName = basename($urlXML);
  60. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  61. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  62. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  63. while ($nuevoAtomAttrNext !== "") {
  64. //$aux = 0;
  65. //while ($aux <= 3) {
  66. $fileName = basename($nuevoAtomAttrNext);
  67. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  68. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  69. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  70. //$aux++;
  71. }
  72. }
  73. private function downloadZip($urlXML)
  74. {
  75. $fileName = basename($urlXML);
  76. $downladedZip = $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  77. if ($downladedZip) {
  78. $zip = new ZipArchive;
  79. if ($zip->open($this->rutaDiariaFecha . $fileName)) {
  80. $zip->extractTo($this->rutaDiariaFecha);
  81. $zip->close();
  82. unlink($this->rutaDiariaFecha . $fileName);
  83. $archivosTotales = get_filenames($this->rutaDiariaFecha);
  84. foreach ($archivosTotales as $file) {
  85. $this->getFile($this->rutaDiariaFecha . $file, $file);
  86. }
  87. }
  88. }
  89. }
  90. private function getFile($url, $fileName)
  91. {
  92. $feed = implode(file($url));
  93. $xml = simplexml_load_string($feed);
  94. $json = json_encode($xml);
  95. $array = json_decode($json, true);
  96. $this->formatXml($feed, $fileName);
  97. return $array;
  98. }
  99. private function newFolder($rutaFinal)
  100. {
  101. $result = false;
  102. if (!is_dir($rutaFinal)) {
  103. $result = mkdir($rutaFinal);
  104. mkdir($rutaFinal . "/format");
  105. }
  106. return $result;
  107. }
  108. private function formatXml($xml, $fileName)
  109. {
  110. $openingFormat = $this->regExIniciosEsquema($xml);
  111. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  112. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  113. }
  114. private function readNextAttribute($archivo)
  115. {
  116. $enlace = "";
  117. foreach ($archivo['link'] as $row) {
  118. $attrNext = $row['@attributes']['rel'];
  119. $attrHref = $row['@attributes']['href'];
  120. if ($attrNext === "next") {
  121. if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_2021")) { //"20220127_")) {
  122. $enlace = $attrHref;
  123. } else {
  124. $enlace = "";
  125. }
  126. //$enlace = $attrHref;
  127. }
  128. }
  129. return $enlace;
  130. }
  131. private function downloadFile($fileName, $url, $ruta)
  132. {
  133. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  134. return true;
  135. } else {
  136. return false;
  137. }
  138. }
  139. private function regExIniciosEsquema($xml)
  140. {
  141. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  142. $result = preg_replace($pattern, "<", $xml);
  143. return $result;
  144. }
  145. private function regExFinalesEsquema($xml)
  146. {
  147. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  148. $result = preg_replace($pattern, "</", $xml);
  149. return $result;
  150. }
  151. private function tablasCalculadas($idUltimaImportacion)
  152. {
  153. $this->Parser_model->truncateTables("import_licitaciones_temp");
  154. $this->Parser_model->truncateTables("vista_datos_licitacion");
  155. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  156. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  157. }
  158. }