ParserCompleto.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. protected $rutaDiaria = FCPATH . "DOWNLOADS/DAILY/";
  21. protected $rutaDiariaFecha = "";
  22. protected $rutaMensual = FCPATH . "DOWNLOADS/MONTH/";
  23. protected $enlaceAtom = "";
  24. protected $fechaCreacionLog = "";
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->load->helper('file');
  29. $this->load->helper('xml');
  30. $this->load->library('Parserfile');
  31. $this->load->model("Parser_model");
  32. }
  33. public function index()
  34. {
  35. $fechaCarpeta = "";
  36. $this->Parser_model->inicioParser();
  37. $resultSetPerfiles = $this->Parser_model->getUrlArchivos();
  38. $fechaCarpeta = date("Y-m-d_H-i-s");
  39. $fechaImportacionLog = date("Y-m-d H:i:s");
  40. foreach ($resultSetPerfiles['datos'] as $row) {
  41. $idUltimaImportacion = $this->Parser_model->setFechaNuevaImportacion($fechaImportacionLog);
  42. $this->fechaCreacionLog = $fechaCarpeta;
  43. $rutaPerfil = $this->rutaDiaria . "/" . $row->prefijos_tablas;
  44. $this->newFolder($rutaPerfil);
  45. if ($this->newFolder($rutaPerfil . "/" . $fechaCarpeta)) {
  46. $this->rutaDiariaFecha = $rutaPerfil . "/" . $fechaCarpeta . "/";
  47. $this->downloadProcess($row->url_descarga . ".atom");
  48. }
  49. $files = get_filenames($this->rutaDiariaFecha . "/format");
  50. emailInicioProcesoDescarga($fechaImportacionLog, $files);
  51. $this->parserfile->index($this->rutaDiariaFecha, $fechaCarpeta, $row, $idUltimaImportacion);
  52. $this->eliminarArchivosProcesoCompleto($this->rutaDiariaFecha);
  53. }
  54. $this->tablasCalculadas($idUltimaImportacion);
  55. $this->load->library('exportXls');
  56. $this->exportxls->exportDatosCompletos();
  57. }
  58. private function downloadProcess($urlXML)
  59. {
  60. $fileName = basename($urlXML);
  61. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  62. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  63. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  64. while ($nuevoAtomAttrNext !== "") {
  65. $fileName = basename($nuevoAtomAttrNext);
  66. $this->downloadFile($fileName, $nuevoAtomAttrNext, $this->rutaDiariaFecha);
  67. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  68. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  69. }
  70. }
  71. private function readNextAttribute($archivo)
  72. {
  73. $enlace = "";
  74. foreach ($archivo['link'] as $row) {
  75. $attrNext = $row['@attributes']['rel'];
  76. $attrHref = $row['@attributes']['href'];
  77. if ($attrNext === "next") {
  78. if (!strpos($attrHref, "licitacionesPerfilesContratanteCompleto3_20220517_133815_1")) { //"20220127_")) {
  79. $enlace = $attrHref;
  80. } else {
  81. $enlace = "";
  82. }
  83. /* $enlace = $attrHref;
  84. } else {
  85. $enlace = ""; */
  86. }
  87. }
  88. return $enlace;
  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. unlink($this->rutaDiariaFecha . "/" . $fileName);
  113. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  114. }
  115. private function downloadFile($fileName, $url, $ruta)
  116. {
  117. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  118. return true;
  119. } else {
  120. return false;
  121. }
  122. }
  123. private function regExIniciosEsquema($xml)
  124. {
  125. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  126. $result = preg_replace($pattern, "<", $xml);
  127. return $result;
  128. }
  129. private function regExFinalesEsquema($xml)
  130. {
  131. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  132. $result = preg_replace($pattern, "</", $xml);
  133. return $result;
  134. }
  135. private function tablasCalculadas($idUltimaImportacion)
  136. {
  137. $this->Parser_model->truncateTables("vista_datos_licitacion");
  138. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  139. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  140. }
  141. private function eliminarArchivosProcesoCompleto($ruta)
  142. {
  143. $files = glob($ruta . "format/*");
  144. foreach ($files as $file) { // iterate files
  145. if (is_file($file)) {
  146. unlink($file); // delete file
  147. }
  148. }
  149. rmdir($ruta . "format");
  150. rmdir($ruta);
  151. }
  152. }
  153. /* End of file ParserCompleto.php */
  154. /* Location: ./application/controllers/ParserCompleto.php */