| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\File; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\FileInterface; |
| 6 |
use ImportWP\Common\Importer\State\ImporterState; |
| 7 |
|
| 8 |
class CSVFile extends AbstractIndexedFile implements FileInterface |
| 9 |
{ |
| 10 |
|
| 11 |
protected $enclosure = '"'; |
| 12 |
protected $delimiter = ','; |
| 13 |
protected $escape = '\\'; |
| 14 |
|
| 15 |
|
| 16 |
/** |
| 17 |
* Set Enclosure |
| 18 |
* |
| 19 |
* @param string $enclosure |
| 20 |
*/ |
| 21 |
public function setEnclosure($enclosure) |
| 22 |
{ |
| 23 |
$this->enclosure = $enclosure; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Set Delimiter |
| 28 |
* |
| 29 |
* @param string $delimiter |
| 30 |
*/ |
| 31 |
public function setDelimiter($delimiter) |
| 32 |
{ |
| 33 |
$this->delimiter = $delimiter; |
| 34 |
} |
| 35 |
|
| 36 |
public function setEscape($escape) |
| 37 |
{ |
| 38 |
$this->escape = $escape; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get Delimiter |
| 43 |
* |
| 44 |
* @return string |
| 45 |
*/ |
| 46 |
public function getDelimiter() |
| 47 |
{ |
| 48 |
return $this->delimiter; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get enclosure |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
public function getEnclosure() |
| 57 |
{ |
| 58 |
return $this->enclosure; |
| 59 |
} |
| 60 |
|
| 61 |
public function getEscape() |
| 62 |
{ |
| 63 |
return $this->enclosure === $this->escape ? '' : $this->escape; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Generate record file positions |
| 68 |
* |
| 69 |
* Loop through each record and save each position |
| 70 |
*/ |
| 71 |
public function generateIndex() |
| 72 |
{ |
| 73 |
$record = 0; |
| 74 |
|
| 75 |
$last_percent = 0; |
| 76 |
$size = intval($this->get_file_size($this->getFileHandle())); |
| 77 |
$this->config->set('process_max', $size); |
| 78 |
|
| 79 |
rewind($this->getFileHandle()); |
| 80 |
while (!feof($this->getFileHandle())) { |
| 81 |
|
| 82 |
$startIndex = ftell($this->getFileHandle()); |
| 83 |
$row = fgetcsv($this->getFileHandle(), 0, $this->getDelimiter(), $this->getEnclosure(), $this->getEscape()); |
| 84 |
|
| 85 |
if (!empty($row)) { |
| 86 |
$this->setIndex($record, $startIndex, ftell($this->getFileHandle())); |
| 87 |
$record++; |
| 88 |
} |
| 89 |
|
| 90 |
if ($this->is_processing && ($record >= 2 || $startIndex > $this->process_max_size)) { |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
$current_pos = intval(ftell($this->getFileHandle())); |
| 95 |
if ($size > 0 && $current_pos > 0) { |
| 96 |
$percent = round($current_pos / $size, 2); |
| 97 |
if ($percent > $last_percent) { |
| 98 |
|
| 99 |
do_action('iwp/importer/process', $last_percent * 100); |
| 100 |
$this->config->set('process', $last_percent * 100); |
| 101 |
$last_percent = $percent; |
| 102 |
|
| 103 |
if (!is_null(iwp()->importer)) { |
| 104 |
$state = ImporterState::get_state(iwp()->importer->getId()); |
| 105 |
if (isset($state['status']) && $state['status'] === 'cancelled') { |
| 106 |
return; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|