ParserCompleto.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. defined('BASEPATH') or exit('No direct script access allowed');
  3. /**
  4. *
  5. * Controller ParserCompleto
  6. *
  7. * This controller for ...
  8. *
  9. * @package CodeIgniter
  10. * @category Controller CI
  11. * @author Setiawan Jodi <jodisetiawan@fisip-untirta.ac.id>
  12. * @author Raul Guerrero <r.g.c@me.com>
  13. * @link https://github.com/setdjod/myci-extension/
  14. * @param ...
  15. * @return ...
  16. *
  17. */
  18. class ParserCompleto extends CI_Controller
  19. {
  20. //TODO: PARAMETRIZAR BBDD -> SI EXISTE UN PROCESO EN MARCHA, NO INICIAR SIGUIENTE DESCARGA, AVISAR
  21. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  22. protected $rutaDiariaFecha = "";
  23. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  24. protected $enlaceAtom = "";
  25. protected $fechaCreacionLog = "";
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->load->helper('file');
  30. $this->load->helper('xml');
  31. $this->load->library('parserfile');
  32. $this->load->model("Parser_model");
  33. }
  34. public function index()
  35. {
  36. $existCronRun = $this->Parser_model->existCronRun();
  37. $fechaCarpeta = "";
  38. if ($existCronRun['num_rows'] > 0) {
  39. if ($existCronRun['datos']->cron_ejecucion == 0) {
  40. $this->Parser_model->inicioParser();
  41. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  42. $fechaCarpeta = date("Y-m-d_H-i-s");
  43. $fechaImportacionLog = date("Y-m-d H:i:s");
  44. foreach ($resultSetPerfiles['datos'] as $row) {
  45. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  46. $this->fechaCreacionLog = $fechaCarpeta;
  47. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  48. $this->newFolder($rutaPerfil);
  49. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  50. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  51. $this->downloadProcess($row->url_descarga . ".atom");
  52. }
  53. $files = get_filenames($this->rutaDiariaFecha . "/format");
  54. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  55. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  56. }
  57. $this->tablasCalculadas($idUltimaImportacion);
  58. emailFinProcesoDescarga($fechaImportacionLog);
  59. } else {
  60. echo "EXISTE UN CRON EN MARCHA.";
  61. }
  62. } else {
  63. echo "No existe parametro que iniciar.";
  64. }
  65. $this->Parser_model->stopParser();
  66. /* $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  67. foreach ($resultSetPerfiles['datos'] as $row) {
  68. $this->parserfile->index($this->rutaDiaria . "PER_PCSP_EX/2022-02-17_20-38-16", date("Y-m-d"), $row);
  69. } */
  70. }
  71. private function downloadProcess($urlXML)
  72. {
  73. $fileName = basename($urlXML);
  74. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  75. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  76. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  77. while ($nuevoAtomAttrNext !== "") {
  78. $fileName = basename($nuevoAtomAttrNext);
  79. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  80. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  81. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  82. }
  83. }
  84. private function readNextAttribute($archivo)
  85. {
  86. $enlace = "";
  87. foreach ($archivo['link'] as $row) {
  88. $attrNext = $row['@attributes']['rel'];
  89. $attrHref = $row['@attributes']['href'];
  90. if ($attrNext === "next") {
  91. if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_2021")) { //"20220127_")) {
  92. $enlace = $attrHref;
  93. } else {
  94. $enlace = "";
  95. }
  96. //$enlace = $attrHref;
  97. }
  98. }
  99. return $enlace;
  100. }
  101. private function getFile($url, $fileName)
  102. {
  103. $feed = implode(file($url));
  104. $xml = simplexml_load_string($feed);
  105. $json = json_encode($xml);
  106. $array = json_decode($json, true);
  107. $this->formatXml($feed, $fileName);
  108. return $array;
  109. }
  110. private function newFolder($rutaFinal)
  111. {
  112. $result = false;
  113. if (!is_dir($rutaFinal)) {
  114. $result = mkdir($rutaFinal);
  115. mkdir($rutaFinal . "/format");
  116. }
  117. return $result;
  118. }
  119. private function formatXml($xml, $fileName)
  120. {
  121. $openingFormat = $this->regExIniciosEsquema($xml);
  122. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  123. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  124. }
  125. private function downloadFile($fileName, $url, $ruta)
  126. {
  127. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  128. return true;
  129. } else {
  130. return false;
  131. }
  132. }
  133. private function regExIniciosEsquema($xml)
  134. {
  135. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  136. $result = preg_replace($pattern, "<", $xml);
  137. return $result;
  138. }
  139. private function regExFinalesEsquema($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 tablasCalculadas($idUltimaImportacion)
  146. {
  147. $this->Parser_model->truncateTables("vista_datos_licitacion");
  148. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  149. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  150. }
  151. }
  152. /* End of file ParserCompleto.php */
  153. /* Location: ./application/controllers/ParserCompleto.php */