PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Importer / Config / Config.php

Config.php in Import WP – CSV & XML Import Export for WordPress 2.14.23, at class/Common/Importer/Config/Config.php

277 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 return $this->hasIndexed($key);
192 }
193
194 public function storeIndexes($key, $buffer = false)
195 {
196
197 if (count($this->index_buffer) > 0) {
198
199 if (!isset($this->config['count'])) {
200 $this->config['count'] = array();
201 }
202
203 if (!isset($this->config['count'][$key])) {
204 $this->config['count'][$key] = 0;
205 }
206
207 $this->config['count'][$key] += count($this->index_buffer) / 2;
208
209 $fh = fopen($this->getIndexFile($key), 'a');
210 foreach ($this->index_buffer as $value) {
211 fputs($fh, $value . "\n");
212 }
213 fclose($fh);
214 $this->index_buffer = [];
215 }
216
217 // hit end of indexer, write config to file
218 if (false === $buffer) {
219 if (!isset($this->config['indexed'])) {
220 $this->config['indexed'] = array();
221 }
222
223 $this->config['indexed'][$key] = true;
224 $this->write();
225 }
226
227 return true;
228 }
229
230 private function hasIndexed($key)
231 {
232 return isset($this->config['indexed']) && isset($this->config['indexed'][$key]) && true === $this->config['indexed'][$key];
233 }
234
235 public function getRecordCount($key)
236 {
237 return $this->config['count'][$key];
238 }
239
240 public function getIndexFile($key)
241 {
242 return $this->file . '.' . $key;
243 }
244
245 private function getCachedIndex($key, $record)
246 {
247 if (
248 isset($this->config['cached_indices']['key'])
249 && $this->config['cached_indices']['key'] === $key
250 && isset($this->config['cached_indices']['start'])
251 && isset($this->config['cached_indices']['cache'])
252 && $this->config['cached_indices']['start'] <= $record
253 && ($this->config['cached_indices']['start'] + (count($this->config['cached_indices']['cache']) / 2)) - 1 >= $record
254 ) {
255
256 $index = ($record - $this->config['cached_indices']['start']) * 2;
257
258 return [
259 $this->config['cached_indices']['cache'][$index],
260 $this->config['cached_indices']['cache'][$index + 1]
261 ];
262 }
263
264 return false;
265 }
266
267 public function setIndexCacheSize($length)
268 {
269 $this->index_cache_size = $length;
270 }
271
272 public function getIndexCacheSize()
273 {
274 return $this->index_cache_size;
275 }
276 }
277