PageRenderTime 23ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/magmi/inc/magmi_csvreader.php

https://bitbucket.org/jit_bec/shopifine
PHP | 230 lines | 184 code | 34 blank | 12 comment | 18 complexity | f7aef735785bd3d8cb67fdb395ba93f2 MD5 | raw file
Possible License(s): LGPL-3.0
  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 $_ignored=array();
  19. public function initialize()
  20. {
  21. $this->_filename=$this->getParam("CSV:filename");
  22. $this->_csep=$this->getParam("CSV:separator",",");
  23. $this->_dcsep=$this->_csep;
  24. if($this->_csep=="\\t")
  25. {
  26. $this->_csep="\t";
  27. }
  28. $this->_cenc=$this->getParam("CSV:enclosure",'"');
  29. $this->_buffersize=$this->getParam("CSV:buffer",0);
  30. $this->_ignored=explode(",",$this->getParam("CSV:ignore"));
  31. }
  32. public function getLinesCount()
  33. {
  34. //open csv file
  35. $f=fopen($this->_filename,"rb");
  36. if($this->getParam("CSV:noheader",false)==true)
  37. {
  38. $count=0;
  39. }
  40. else
  41. {
  42. $count=-1;
  43. }
  44. $linenum=0;
  45. if($f!=false)
  46. {
  47. $line=1;
  48. while($line<$this->getParam("CSV:headerline",1))
  49. {
  50. $line++;
  51. $dummy=fgetcsv($f,$this->_buffersize,$this->_csep,$this->_cenc);
  52. }
  53. //get records count
  54. while(fgetcsv($f,$this->_buffersize,$this->_csep,$this->_cenc))
  55. {
  56. if(!in_array($line,$this->_ignored))
  57. {
  58. $count++;
  59. }
  60. }
  61. fclose($f);
  62. }
  63. else
  64. {
  65. $this->log("Could not read $this->_filename , check permissions","error");
  66. }
  67. return $count;
  68. }
  69. public function checkCSV()
  70. {
  71. $this->_curline=0;
  72. ini_set("auto_detect_line_endings",true);
  73. if(!isset($this->_filename))
  74. {
  75. throw new Magmi_CSVException("No csv file set");
  76. }
  77. if(!file_exists($this->_filename))
  78. {
  79. throw new Magmi_CSVException("{$this->_filename} not found");
  80. }
  81. $this->log("Importing CSV : $this->_filename using separator [ $this->_dcsep ] enclosing [ $this->_cenc ]","startup");
  82. }
  83. public function openCSV()
  84. {
  85. //open csv file
  86. $this->_fh=fopen($this->_filename,"rb");
  87. }
  88. public function getColumnNames($prescan=false)
  89. {
  90. if($prescan==true)
  91. {
  92. $this->_fh=fopen($this->getParam("CSV:filename"),"rb");
  93. $this->_csep=$this->getParam("CSV:separator",",");
  94. $this->_dcsep=$this->_csep;
  95. if($this->_csep=="\\t")
  96. {
  97. $this->_csep="\t";
  98. }
  99. $this->_cenc=$this->getParam("CSV:enclosure",'"');
  100. $this->_buffersize=$this->getParam("CSV:buffer",0);
  101. }
  102. $line=1;
  103. while($line<$this->getParam("CSV:headerline",1))
  104. {
  105. $line++;
  106. $dummy=fgetcsv($this->_fh,$this->_buffersize,$this->_csep,$this->_cenc);
  107. $this->log("skip line $line:$dummy","info");
  108. }
  109. $cols=fgetcsv($this->_fh,$this->_buffersize,$this->_csep,$this->_cenc);
  110. //if csv has no headers,use column number as column name
  111. if($this->getParam("CSV:noheader",false)==true)
  112. {
  113. $kl=array_merge(array("dummy"),$cols);
  114. unset($kl[0]);
  115. $cols=array();
  116. foreach(array_keys($kl) as $c)
  117. {
  118. $cols[]="col".$c;
  119. }
  120. //reset file pointer
  121. fseek($this->_fh,0);
  122. }
  123. $this->_cols=$cols;
  124. $this->_nhcols=count($this->_cols);
  125. //trim column names
  126. for($i=0;$i<$this->_nhcols;$i++)
  127. {
  128. $this->_cols[$i]=trim($this->_cols[$i]);
  129. }
  130. if($prescan==true)
  131. {
  132. fclose($this->_fh);
  133. }
  134. else
  135. {
  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. return ( !isset($row[1]) && empty($row[0]) );
  146. }
  147. public function getNextRecord()
  148. {
  149. $row=null;
  150. $allowtrunc=$this->getParam("CSV:allowtrunc",false);
  151. while($row!==false)
  152. {
  153. $row=fgetcsv($this->_fh,$this->_buffersize,$this->_csep,$this->_cenc);
  154. if($row !==false)
  155. {
  156. $this->_curline++;
  157. //skip empty lines
  158. if($this->isemptyline($row))
  159. {
  160. continue;
  161. }
  162. $rcols=count($row);
  163. if(!$allowtrunc && $rcols!=$this->_nhcols)
  164. {
  165. //if strict matching, warning & continue
  166. $this->log("warning: line $this->_curline , wrong column number : $rcols found over $this->_nhcols, line skipped","warning");
  167. continue;
  168. }
  169. break;
  170. }
  171. }
  172. //if we read something
  173. if(is_array($row))
  174. {
  175. //strict mode
  176. if(!$allowtrunc)
  177. {
  178. //create product attributes values array indexed by attribute code
  179. $record=array_combine($this->_cols,$row);
  180. }
  181. else
  182. {
  183. //relax mode, recompose keys from read columns , others will be left unset
  184. $ncols=count($row);
  185. $cols=array_slice($this->_cols,0,$ncols);
  186. $record=array_combine($cols,$row);
  187. }
  188. }
  189. else
  190. {
  191. $record=false;
  192. }
  193. unset($row);
  194. return $record;
  195. }
  196. }