ParserCompleto.php 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. eliminarArchivosProcesoCompleto($this->rutaDiariaFecha);
  53. }
  54. $this->tablasCalculadas($idUltimaImportacion);
  55. $this->load->library('exportXls');
  56. //$this->exportxls->exportDatosCompletos();
  57. $this->Parser_model->stopParser();
  58. }
  59. private function downloadProcess($urlXML)
  60. {
  61. $fileName = basename($urlXML);
  62. $this->downloadFile($fileName, $urlXML, $this->rutaDiariaFecha);
  63. $archivoAtom = $this->getFile($this->rutaDiariaFecha . $fileName, $fileName);
  64. $nuevoAtomAttrNext = $this->readNextAttribute($archivoAtom);
  65. while ($nuevoAtomAttrNext !== "") {
  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. }
  71. }
  72. private function readNextAttribute($archivo)
  73. {
  74. $enlace = "";
  75. foreach ($archivo['link'] as $row) {
  76. $attrNext = $row['@attributes']['rel'];
  77. $attrHref = $row['@attributes']['href'];
  78. if ($attrNext === "next") {
  79. $enlace = $attrHref;
  80. } else {
  81. $enlace = "";
  82. }
  83. }
  84. return $enlace;
  85. }
  86. private function getFile($url, $fileName)
  87. {
  88. $feed = implode(file($url));
  89. $xml = simplexml_load_string($feed);
  90. $json = json_encode($xml);
  91. $array = json_decode($json, true);
  92. $this->formatXml($feed, $fileName);
  93. return $array;
  94. }
  95. private function newFolder($rutaFinal)
  96. {
  97. $result = false;
  98. if (!is_dir($rutaFinal)) {
  99. $result = mkdir($rutaFinal);
  100. mkdir($rutaFinal . "/format");
  101. }
  102. return $result;
  103. }
  104. private function formatXml($xml, $fileName)
  105. {
  106. $openingFormat = $this->regExIniciosEsquema($xml);
  107. $closureFormat = $this->regExFinalesEsquema($openingFormat);
  108. unlink($this->rutaDiariaFecha . "/" . $fileName);
  109. file_put_contents($this->rutaDiariaFecha . "/format/" . $fileName, $closureFormat);
  110. }
  111. private function downloadFile($fileName, $url, $ruta)
  112. {
  113. if (file_put_contents($ruta . $fileName, file_get_contents($url))) {
  114. return true;
  115. } else {
  116. return false;
  117. }
  118. }
  119. private function regExIniciosEsquema($xml)
  120. {
  121. $pattern = "/(<cac:)|(<cbc:)|(<cac-place-ext:)|(<cbc-place-ext:)/";
  122. $result = preg_replace($pattern, "<", $xml);
  123. return $result;
  124. }
  125. private function regExFinalesEsquema($xml)
  126. {
  127. $pattern = "/(<\/cac:)|(<\/cbc:)|(<\/cac-place-ext:)|(<\/cbc-place-ext:)/";
  128. $result = preg_replace($pattern, "</", $xml);
  129. return $result;
  130. }
  131. private function tablasCalculadas($idUltimaImportacion)
  132. {
  133. $this->Parser_model->truncateTables("vista_datos_licitacion");
  134. $licitaciones = $this->Xlsexport_model->getLicitaciones($idUltimaImportacion);
  135. insertBucleDuplicateOnKey($licitaciones['datos'], "vista_datos_licitacion", "");
  136. }
  137. private function eliminarArchivos() {}
  138. }
  139. /* End of file ParserCompleto.php */
  140. /* Location: ./application/controllers/ParserCompleto.php */