| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Config; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\ConfigInterface; |
| 6 |
use ImportWP\Common\Importer\Exception\FileException; |
| 7 |
|
| 8 |
class Config implements ConfigInterface |
| 9 |
{ |
| 10 |
|
| 11 |
private $config; |
| 12 |
private $file; |
| 13 |
private $index_buffer = []; |
| 14 |
private $index_cache_size = 1; |
| 15 |
|
| 16 |
public function __construct($file) |
| 17 |
{ |
| 18 |
$this->file = $file; |
| 19 |
if (file_exists($this->file)) { |
| 20 |
$this->read(); |
| 21 |
} else { |
| 22 |
$this->config = array(); |
| 23 |
$this->write(); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Read from file |
| 29 |
*/ |
| 30 |
private function read() |
| 31 |
{ |
| 32 |
$this->config = json_decode(file_get_contents($this->file), true); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Write to file |
| 37 |
*/ |
| 38 |
private function write() |
| 39 |
{ |
| 40 |
file_put_contents($this->file, json_encode($this->config)); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get setting |
| 45 |
* |
| 46 |
* @param $key |
| 47 |
* |
| 48 |
* @return mixed |
| 49 |
*/ |
| 50 |
public function get($key) |
| 51 |
{ |
| 52 |
return isset($this->config[$key]) ? $this->config[$key] : false; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Set setting |
| 57 |
* |
| 58 |
* @param $key |
| 59 |
* @param $data |
| 60 |
* |
| 61 |
* @return mixed |
| 62 |
*/ |
| 63 |
public function set($key, $data) |
| 64 |
{ |
| 65 |
$this->config[$key] = $data; |
| 66 |
$this->write(); |
| 67 |
} |
| 68 |
|
| 69 |
public function getData() |
| 70 |
{ |
| 71 |
$data = $this->get('data'); |
| 72 |
|
| 73 |
return $data; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* TODO: Refactor function, basic implementation to test stream file indexing |
| 78 |
* @param $key |
| 79 |
* @param $record |
| 80 |
* @return array |
| 81 |
* @throws FileException |
| 82 |
*/ |
| 83 |
public function getIndex($key, $record) |
| 84 |
{ |
| 85 |
$recordIndex = $record * 2; |
| 86 |
|
| 87 |
$cache = $this->getCachedIndex($key, $record); |
| 88 |
if ($cache) { |
| 89 |
return $cache; |
| 90 |
} |
| 91 |
|
| 92 |
$file = $this->getIndexFile($key); |
| 93 |
if (!file_exists($file)) { |
| 94 |
throw new FileException(sprintf(__("Config Index File Not Found: %s", 'jc-importer'), $file)); |
| 95 |
} |
| 96 |
|
| 97 |
$fh = fopen($file, 'r'); |
| 98 |
$counter = 0; |
| 99 |
|
| 100 |
|
| 101 |
$max_chunk = 8192; |
| 102 |
$prev_chunk = false; |
| 103 |
|
| 104 |
$found = $recordIndex === 0 ? true : false; |
| 105 |
$cache_max = $this->getIndexCacheSize(); |
| 106 |
$cache = []; |
| 107 |
|
| 108 |
while (!feof($fh)) { |
| 109 |
|
| 110 |
// get buffer / append end of previous buffer |
| 111 |
$buffer = fread($fh, $max_chunk); |
| 112 |
if ($prev_chunk !== false) { |
| 113 |
$buffer = $prev_chunk . $buffer; |
| 114 |
} |
| 115 |
|
| 116 |
// reset $prev_chunk |
| 117 |
$prev_chunk = false; |
| 118 |
|
| 119 |
$buffer_len = strlen($buffer); |
| 120 |
|
| 121 |
$separator_line_count = substr_count($buffer, "\n"); |
| 122 |
|
| 123 |
if ($counter + $separator_line_count >= $recordIndex) { |
| 124 |
|
| 125 |
// loop through each separator |
| 126 |
$separator_offset = 0; |
| 127 |
$separator_counter = $counter; |
| 128 |
while ($separator_offset < strlen($buffer)) { |
| 129 |
|
| 130 |
$separator_pos = strpos($buffer, "\n", $separator_offset); |
| 131 |
if ($separator_pos !== false) { |
| 132 |
|
| 133 |
if ($found) { |
| 134 |
$cache_data = substr($buffer, $separator_offset, $separator_pos - $separator_offset); |
| 135 |
$cache[] = $cache_data; |
| 136 |
if (count($cache) >= $cache_max * 2) { |
| 137 |
break; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
$separator_counter++; |
| 142 |
$separator_offset = $separator_pos + 1; |
| 143 |
if ($separator_counter === $recordIndex) { |
| 144 |
$found = true; |
| 145 |
} |
| 146 |
} elseif (strlen($buffer) > $separator_offset) { |
| 147 |
$prev_chunk = substr($buffer, $separator_offset); |
| 148 |
break; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (count($cache) >= $cache_max * 2) { |
| 154 |
break; |
| 155 |
} |
| 156 |
|
| 157 |
$counter += $separator_line_count; |
| 158 |
} |
| 159 |
|
| 160 |
if (count($cache) < 2) { |
| 161 |
throw new FileException(sprintf(__("Unable to located record: %s.", 'jc-importer'), ($record + 1))); |
| 162 |
} |
| 163 |
|
| 164 |
// store cache |
| 165 |
$this->config['cached_indices'] = [ |
| 166 |
'key' => $key, |
| 167 |
'start' => $record, |
| 168 |
'cache' => $cache |
| 169 |
]; |
| 170 |
$this->write(); |
| 171 |
|
| 172 |
return [ |
| 173 |
$cache[0], |
| 174 |
$cache[1] |
| 175 |
]; |
| 176 |
} |
| 177 |
|
| 178 |
public function setIndex($key, $record, $start, $end) |
| 179 |
{ |
| 180 |
|
| 181 |
$this->index_buffer[] = $start; |
| 182 |
$this->index_buffer[] = $end - $start; |
| 183 |
|
| 184 |
if (count($this->index_buffer) >= 200000) { |
| 185 |
$this->storeIndexes($key, true); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
public function readIndexes($key) |
| 190 |
{ |
| 191 |
if (!$this->hasIndexed($key)) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
|
| 195 |
// Treat a zero-record index as missing so it can be rebuilt |
| 196 |
if (!isset($this->config['count'][$key]) || intval($this->config['count'][$key]) < 1) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
public function storeIndexes($key, $buffer = false) |
| 204 |
{ |
| 205 |
|
| 206 |
if (count($this->index_buffer) > 0) { |
| 207 |
|
| 208 |
if (!isset($this->config['count'])) { |
| 209 |
$this->config['count'] = array(); |
| 210 |
} |
| 211 |
|
| 212 |
if (!isset($this->config['count'][$key])) { |
| 213 |
$this->config['count'][$key] = 0; |
| 214 |
} |
| 215 |
|
| 216 |
$this->config['count'][$key] += count($this->index_buffer) / 2; |
| 217 |
|
| 218 |
$fh = fopen($this->getIndexFile($key), 'a'); |
| 219 |
foreach ($this->index_buffer as $value) { |
| 220 |
fputs($fh, $value . "\n"); |
| 221 |
} |
| 222 |
fclose($fh); |
| 223 |
$this->index_buffer = []; |
| 224 |
} |
| 225 |
|
| 226 |
// hit end of indexer, write config to file |
| 227 |
if (false === $buffer) { |
| 228 |
if (!isset($this->config['indexed'])) { |
| 229 |
$this->config['indexed'] = array(); |
| 230 |
} |
| 231 |
|
| 232 |
$this->config['indexed'][$key] = true; |
| 233 |
$this->write(); |
| 234 |
} |
| 235 |
|
| 236 |
return true; |
| 237 |
} |
| 238 |
|
| 239 |
private function hasIndexed($key) |
| 240 |
{ |
| 241 |
return isset($this->config['indexed']) && isset($this->config['indexed'][$key]) && true === $this->config['indexed'][$key]; |
| 242 |
} |
| 243 |
|
| 244 |
public function getRecordCount($key) |
| 245 |
{ |
| 246 |
if (!isset($this->config['count'][$key])) { |
| 247 |
return 0; |
| 248 |
} |
| 249 |
|
| 250 |
return intval($this->config['count'][$key]); |
| 251 |
} |
| 252 |
|
| 253 |
public function getIndexFile($key) |
| 254 |
{ |
| 255 |
return $this->file . '.' . $key; |
| 256 |
} |
| 257 |
|
| 258 |
private function getCachedIndex($key, $record) |
| 259 |
{ |
| 260 |
if ( |
| 261 |
isset($this->config['cached_indices']['key']) |
| 262 |
&& $this->config['cached_indices']['key'] === $key |
| 263 |
&& isset($this->config['cached_indices']['start']) |
| 264 |
&& isset($this->config['cached_indices']['cache']) |
| 265 |
&& $this->config['cached_indices']['start'] <= $record |
| 266 |
&& ($this->config['cached_indices']['start'] + (count($this->config['cached_indices']['cache']) / 2)) - 1 >= $record |
| 267 |
) { |
| 268 |
|
| 269 |
$index = ($record - $this->config['cached_indices']['start']) * 2; |
| 270 |
|
| 271 |
return [ |
| 272 |
$this->config['cached_indices']['cache'][$index], |
| 273 |
$this->config['cached_indices']['cache'][$index + 1] |
| 274 |
]; |
| 275 |
} |
| 276 |
|
| 277 |
return false; |
| 278 |
} |
| 279 |
|
| 280 |
public function setIndexCacheSize($length) |
| 281 |
{ |
| 282 |
$this->index_cache_size = $length; |
| 283 |
} |
| 284 |
|
| 285 |
public function getIndexCacheSize() |
| 286 |
{ |
| 287 |
return $this->index_cache_size; |
| 288 |
} |
| 289 |
} |
| 290 |
|