PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/importmag/inc/magmi_csvreader.php

https://gitlab.com/inglobe/mgt-clemente-css
PHP | 208 lines | 174 code | 19 blank | 15 comment | 41 complexity | 8206a23d5de188c2aae3d7f5b050a924 MD5 | raw file
  1. <?php
  2. require_once("magmi_mixin.php");
  3. require_once("magmi_utils.php");
  4. class Magmi_CSVException extends Exception
  5. {
  6. }
  7. class Magmi_CSVReader extends Magmi_Mixin
  8. {
  9. protected $_filename;
  10. protected $_fh;
  11. protected $_cols;
  12. protected $_csep;
  13. protected $_dcsep;
  14. protected $_params;
  15. protected $_buffersize;
  16. protected $_curline;
  17. protected $_nhcols;
  18. protected $_cenc;
  19. protected $_ignored = array();
  20. protected $_prefix;
  21. public function initialize($params=null, $prefix='CSV')
  22. {
  23. $this->_prefix = $prefix;
  24. if (isset($params)) {
  25. $this->_params=$params;
  26. }
  27. $this->_filename = $this->getParam($this->_prefix.":filename");
  28. $this->_csep = $this->getParam($this->_prefix.":separator", ",");
  29. $this->_dcsep = $this->_csep;
  30. if ($this->_csep == "\\t") {
  31. $this->_csep = "\t";
  32. }
  33. $this->_cenc = $this->getParam($this->_prefix.":enclosure", '"');
  34. $this->_buffersize = $this->getParam($this->_prefix.":buffer", 0);
  35. $this->_ignored = explode(",", $this->getParam($this->_prefix.":ignore"));
  36. }
  37. public function getParam($paramname, $default='')
  38. {
  39. return (isset($this->_params[$paramname]) && $this->_params[$paramname] != "")?$this->_params[$paramname]:$default;
  40. }
  41. public function getLinesCount()
  42. {
  43. // open csv file
  44. $f = fopen($this->_filename, "rb");
  45. if ($this->getParam($this->_prefix.":noheader", false) == true) {
  46. $count = 0;
  47. } else {
  48. $count = -1;
  49. }
  50. if ($f != false) {
  51. $line = 1;
  52. while ($line < $this->getParam($this->_prefix.":headerline", 1)) {
  53. $line++;
  54. fgetcsv($f, $this->_buffersize, $this->_csep, $this->_cenc);
  55. }
  56. // get records count
  57. while (fgetcsv($f, $this->_buffersize, $this->_csep, $this->_cenc)) {
  58. if (!in_array($line, $this->_ignored)) {
  59. $count++;
  60. }
  61. }
  62. fclose($f);
  63. } else {
  64. $this->log("Could not read $this->_filename , check permissions", "error");
  65. }
  66. return $count;
  67. }
  68. public function checkCSV()
  69. {
  70. $this->_curline = 0;
  71. ini_set("auto_detect_line_endings", true);
  72. if (!isset($this->_filename)) {
  73. throw new Magmi_CSVException("No csv file set");
  74. }
  75. if (!file_exists($this->_filename)) {
  76. throw new Magmi_CSVException("{$this->_filename} not found");
  77. }
  78. $this->log("Importing CSV : $this->_filename using separator [ $this->_dcsep ] enclosing [ $this->_cenc ]",
  79. "startup");
  80. }
  81. public function openCSV()
  82. {
  83. $utf8bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
  84. // open csv file
  85. $this->_fh = fopen($this->_filename, "rb");
  86. // check for UTF8 BOM
  87. $bomtest = fread($this->_fh, 3);
  88. // if 3 first bytes of file are not utf8 bom
  89. if ($bomtest != $utf8bom) {
  90. // rewind to first byte;
  91. $this->log("No BOM detected, assuming File is UTF8 without BOM...", "startup");
  92. fseek($this->_fh, 0, SEEK_SET);
  93. } else {
  94. $this->log("File is UTF8 (BOM Detected)", "startup");
  95. }
  96. }
  97. public function getColumnNames($prescan = false)
  98. {
  99. if ($prescan == true) {
  100. $this->openCSV();
  101. $this->_csep = $this->getParam($this->_prefix.":separator", ",");
  102. $this->_dcsep = $this->_csep;
  103. if ($this->_csep == "\\t") {
  104. $this->_csep = "\t";
  105. }
  106. $this->_cenc = $this->getParam($this->_prefix.":enclosure", '"');
  107. $this->_buffersize = $this->getParam($this->_prefix.":buffer", 0);
  108. }
  109. $line = 1;
  110. while ($line < $this->getParam($this->_prefix.":headerline", 1)) {
  111. $line++;
  112. $dummy = fgetcsv($this->_fh, $this->_buffersize, $this->_csep, $this->_cenc);
  113. $this->log("skip line $line:" . implode($this->_csep, $dummy), "info");
  114. }
  115. $cols = fgetcsv($this->_fh, $this->_buffersize, $this->_csep, $this->_cenc);
  116. // if csv has no headers,use column number as column name
  117. if ($this->getParam($this->_prefix.":noheader", false) == true) {
  118. $kl = array_merge(array("dummy"), $cols);
  119. unset($kl[0]);
  120. $cols = array();
  121. foreach (array_keys($kl) as $c) {
  122. $cols[] = "col" . $c;
  123. }
  124. // reset file pointer
  125. fseek($this->_fh, 0);
  126. }
  127. $this->_cols = $cols;
  128. $this->_nhcols = count($this->_cols);
  129. // trim column names
  130. for ($i = 0; $i < $this->_nhcols; $i++) {
  131. $this->_cols[$i] = trim($this->_cols[$i]);
  132. }
  133. if ($prescan == true) {
  134. fclose($this->_fh);
  135. } else {
  136. $this->log("$this->_nhcols CSV headers columns found", "startup");
  137. }
  138. return $this->_cols;
  139. }
  140. public function closeCSV()
  141. {
  142. fclose($this->_fh);
  143. }
  144. public function isemptyline($row)
  145. {
  146. return (!isset($row[1]) && empty($row[0]));
  147. }
  148. public function getNextRecord()
  149. {
  150. $row = null;
  151. $allowtrunc = $this->getParam($this->_prefix.":allowtrunc", false);
  152. while ($row !== false) {
  153. $row = fgetcsv($this->_fh, $this->_buffersize, $this->_csep, $this->_cenc);
  154. if ($row !== false) {
  155. $this->_curline++;
  156. // skip empty lines
  157. if ($this->isemptyline($row)) {
  158. continue;
  159. }
  160. $rcols = count($row);
  161. if (!$allowtrunc && $rcols != $this->_nhcols) {
  162. // if strict matching, warning & continue
  163. $this->log(
  164. "warning: line $this->_curline , wrong column number : $rcols found over $this->_nhcols, line skipped",
  165. "warning");
  166. continue;
  167. }
  168. break;
  169. }
  170. }
  171. // if we read something
  172. if (is_array($row)) {
  173. // strict mode
  174. if (!$allowtrunc) {
  175. // create product attributes values array indexed by attribute code
  176. $record = array_combine($this->_cols, $row);
  177. } else {
  178. // relax mode, recompose keys from read columns , others will be left unset
  179. $ncols = count($row);
  180. $cols = array_slice($this->_cols, 0, $ncols);
  181. $record = array_combine($cols, $row);
  182. }
  183. } else {
  184. $record = false;
  185. }
  186. unset($row);
  187. return $record;
  188. }
  189. }