| @@ -2,8 +2,9 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace ImportWP\Common\Importer\File; |
| 4 | 4 | |
| 5 | 5 | use ImportWP\Common\Importer\FileInterface; |
| 6 | +use ImportWP\Common\Importer\State\ImporterState; | |
| 6 | 7 | |
| 7 | 8 | class CSVFile extends AbstractIndexedFile implements FileInterface |
| 8 | 9 | { |
| 9 | 10 | |
| @@ -8,8 +9,9 @@ | ||
| 8 | 9 | { |
| 9 | 10 | |
| 10 | 11 | protected $enclosure = '"'; |
| 11 | 12 | protected $delimiter = ','; |
| 13 | + protected $escape = '\\'; | |
| 12 | 14 | |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | 17 | * Set Enclosure |
| @@ -30,8 +32,13 @@ | ||
| 30 | 32 | { |
| 31 | 33 | $this->delimiter = $delimiter; |
| 32 | 34 | } |
| 33 | 35 | |
| 36 | + public function setEscape($escape) | |
| 37 | + { | |
| 38 | + $this->escape = $escape; | |
| 39 | + } | |
| 40 | + | |
| 34 | 41 | /** |
| 35 | 42 | * Get Delimiter |
| 36 | 43 | * |
| 37 | 44 | * @return string |
| @@ -50,21 +57,31 @@ | ||
| 50 | 57 | { |
| 51 | 58 | return $this->enclosure; |
| 52 | 59 | } |
| 53 | 60 | |
| 61 | + public function getEscape() | |
| 62 | + { | |
| 63 | + return $this->enclosure === $this->escape ? '' : $this->escape; | |
| 64 | + } | |
| 65 | + | |
| 54 | 66 | /** |
| 55 | 67 | * Generate record file positions |
| 56 | 68 | * |
| 57 | 69 | * Loop through each record and save each position |
| 58 | 70 | */ |
| 59 | - protected function generateIndex() | |
| 71 | + public function generateIndex() | |
| 60 | 72 | { |
| 61 | 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 | + | |
| 62 | 79 | rewind($this->getFileHandle()); |
| 63 | 80 | while (!feof($this->getFileHandle())) { |
| 64 | 81 | |
| 65 | 82 | $startIndex = ftell($this->getFileHandle()); |
| 66 | - $row = fgetcsv($this->getFileHandle(), 0, $this->getDelimiter(), $this->getEnclosure()); | |
| 83 | + $row = fgetcsv($this->getFileHandle(), 0, $this->getDelimiter(), $this->getEnclosure(), $this->getEscape()); | |
| 67 | 84 | |
| 68 | 85 | if (!empty($row)) { |
| 69 | 86 | $this->setIndex($record, $startIndex, ftell($this->getFileHandle())); |
| 70 | 87 | $record++; |
| @@ -69,10 +86,28 @@ | ||
| 69 | 86 | $this->setIndex($record, $startIndex, ftell($this->getFileHandle())); |
| 70 | 87 | $record++; |
| 71 | 88 | } |
| 72 | 89 | |
| 73 | - if ($this->is_processing && ($record >= 2 || $startIndex > $this->process_max_size)) { | |
| 90 | + if ($this->is_processing && $startIndex > $this->process_max_size) { | |
| 74 | 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 | + } | |
| 75 | 110 | } |
| 76 | 111 | } |
| 77 | 112 | } |
| 78 | 113 | } |