| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\File; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\ConfigInterface; |
| 6 |
|
| 7 |
abstract class AbstractIndexedFile extends AbstractFile |
| 8 |
{ |
| 9 |
|
| 10 |
private $loaded = false; |
| 11 |
protected $config; |
| 12 |
|
| 13 |
/** |
| 14 |
* Are we temp processing a file |
| 15 |
* |
| 16 |
* @var boolean |
| 17 |
*/ |
| 18 |
protected $is_processing = false; |
| 19 |
/** |
| 20 |
* When temp processing a file we do not need all of it. |
| 21 |
* |
| 22 |
* @var integer Max byte of file to read |
| 23 |
*/ |
| 24 |
protected $process_max_size = 1000000; |
| 25 |
|
| 26 |
/** |
| 27 |
* AbstractIndexedFile constructor. |
| 28 |
* |
| 29 |
* TODO: Config might not be right here, as we are just storing record index's |
| 30 |
* |
| 31 |
* @param string $file_path |
| 32 |
* @param ConfigInterface $config |
| 33 |
*/ |
| 34 |
public function __construct($file_path, $config = null) |
| 35 |
{ |
| 36 |
parent::__construct($file_path); |
| 37 |
|
| 38 |
$this->config = $config; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get record |
| 43 |
* |
| 44 |
* @param int $index |
| 45 |
* |
| 46 |
* @return string |
| 47 |
* |
| 48 |
* @throws \ImportWP\Common\Importer\Exception\FileException |
| 49 |
*/ |
| 50 |
public function getRecord($index = 0) |
| 51 |
{ |
| 52 |
|
| 53 |
$position = $this->getIndex($index); |
| 54 |
fseek($this->getFileHandle(), $position[0]); |
| 55 |
$this->setCurrentRecord($index); |
| 56 |
|
| 57 |
// loop till length has been met or end of file |
| 58 |
|
| 59 |
$contents = ''; |
| 60 |
$max_chunk = 8192; |
| 61 |
$bytes_to_read = $position[1]; |
| 62 |
while (!feof($this->getFileHandle()) && $bytes_to_read > 0) { |
| 63 |
|
| 64 |
$temp_length = $bytes_to_read; |
| 65 |
if ($bytes_to_read > $max_chunk) { |
| 66 |
$temp_length = $max_chunk; |
| 67 |
} |
| 68 |
|
| 69 |
$contents .= fread($this->getFileHandle(), $temp_length); |
| 70 |
$bytes_to_read -= $temp_length; |
| 71 |
} |
| 72 |
|
| 73 |
// Convert to utf-8 if not already |
| 74 |
if (function_exists('mb_detect_encoding') && function_exists('mb_convert_encoding')) { |
| 75 |
$encoding = mb_detect_encoding($contents, array('UTF-8'), true); |
| 76 |
if (false === $encoding) { |
| 77 |
$from_encoding = $this->config->get('file_encoding'); |
| 78 |
if (!empty($from_encoding)) { |
| 79 |
$contents = mb_convert_encoding($contents, 'UTF-8', $from_encoding); |
| 80 |
} else { |
| 81 |
$contents = mb_convert_encoding($contents, 'UTF-8'); |
| 82 |
} |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $contents; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get record file index. |
| 91 |
* |
| 92 |
* @param int $record |
| 93 |
* |
| 94 |
* @return array |
| 95 |
*/ |
| 96 |
public function getIndex($record) |
| 97 |
{ |
| 98 |
if (!$this->loadIndex()) { |
| 99 |
$this->generateIndex(); |
| 100 |
$this->storeIndexes(); |
| 101 |
} |
| 102 |
|
| 103 |
return $this->config->getIndex($this->getFileIndexKey(), $record); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Add record and file position to index |
| 108 |
* |
| 109 |
* @param int $record |
| 110 |
* @param int $start |
| 111 |
* @param int $end |
| 112 |
* |
| 113 |
* @internal param $index |
| 114 |
*/ |
| 115 |
public function setIndex($record, $start, $end) |
| 116 |
{ |
| 117 |
if (!$this->loadIndex()) { |
| 118 |
$this->generateIndex(); |
| 119 |
$this->storeIndexes(); |
| 120 |
} |
| 121 |
|
| 122 |
$this->config->setIndex($this->getFileIndexKey(), $record, $start, $end); |
| 123 |
|
| 124 |
|
| 125 |
// $this->index[$record] = array($start, $end); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Load index from cache |
| 130 |
* |
| 131 |
* @return bool |
| 132 |
*/ |
| 133 |
public function loadIndex() |
| 134 |
{ |
| 135 |
if ($this->loaded) { |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
// Load index from cache |
| 140 |
$this->loaded = true; |
| 141 |
if ($this->readIndexes()) { |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Generate file position index. |
| 150 |
* |
| 151 |
* @return mixed |
| 152 |
*/ |
| 153 |
abstract protected function generateIndex(); |
| 154 |
|
| 155 |
/** |
| 156 |
* Is there record next |
| 157 |
* |
| 158 |
* @return bool |
| 159 |
*/ |
| 160 |
public function hasNextRecord() |
| 161 |
{ |
| 162 |
if ($this->getRecordCount() - 1 > $this->current_record) { |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Get record count |
| 171 |
* |
| 172 |
* @return int |
| 173 |
*/ |
| 174 |
public function getRecordCount() |
| 175 |
{ |
| 176 |
if (!$this->loadIndex()) { |
| 177 |
$this->generateIndex(); |
| 178 |
$this->storeIndexes(); |
| 179 |
} |
| 180 |
|
| 181 |
return $this->config->getRecordCount($this->getFileIndexKey()); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Is there record previous |
| 186 |
* |
| 187 |
* @return bool |
| 188 |
*/ |
| 189 |
public function hasPrevRecord() |
| 190 |
{ |
| 191 |
if ($this->current_record > 0) { |
| 192 |
return true; |
| 193 |
} |
| 194 |
|
| 195 |
return false; |
| 196 |
} |
| 197 |
|
| 198 |
public function storeIndexes() |
| 199 |
{ |
| 200 |
if (null === $this->config) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
return $this->config->storeIndexes($this->getFileIndexKey()); |
| 205 |
} |
| 206 |
|
| 207 |
public function readIndexes() |
| 208 |
{ |
| 209 |
if (null === $this->config) { |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
return $this->config->readIndexes($this->getFileIndexKey()); |
| 214 |
|
| 215 |
// return $this->index = $this->config->get( $this->getFileIndexKey() ); |
| 216 |
} |
| 217 |
|
| 218 |
public function getFileIndexKey() |
| 219 |
{ |
| 220 |
return 'file_index'; |
| 221 |
} |
| 222 |
|
| 223 |
public function processing($processing = false) |
| 224 |
{ |
| 225 |
$this->is_processing = true; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Read file size from handle and reset pointer back to current position |
| 230 |
* |
| 231 |
* @param resource $handle |
| 232 |
* @return void |
| 233 |
*/ |
| 234 |
public function get_file_size($handle) |
| 235 |
{ |
| 236 |
$current = ftell($handle); |
| 237 |
fseek($handle, 0, SEEK_END); |
| 238 |
$size = ftell($handle); |
| 239 |
fseek($handle, $current); |
| 240 |
|
| 241 |
return $size; |
| 242 |
} |
| 243 |
} |
| 244 |
|