PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.0
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 / File / AbstractIndexedFile.php

AbstractIndexedFile.php in Import WP – CSV & XML Import Export for WordPress 2.15.0, at class/Common/Importer/File/AbstractIndexedFile.php

252 lines 5.5 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\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 * Force the next index access to reload/regenerate for the current key.
150 */
151 public function resetIndexState()
152 {
153 $this->loaded = false;
154 }
155
156 /**
157 * Generate file position index.
158 *
159 * @return mixed
160 */
161 abstract protected function generateIndex();
162
163 /**
164 * Is there record next
165 *
166 * @return bool
167 */
168 public function hasNextRecord()
169 {
170 if ($this->getRecordCount() - 1 > $this->current_record) {
171 return true;
172 }
173
174 return false;
175 }
176
177 /**
178 * Get record count
179 *
180 * @return int
181 */
182 public function getRecordCount()
183 {
184 if (!$this->loadIndex()) {
185 $this->generateIndex();
186 $this->storeIndexes();
187 }
188
189 return $this->config->getRecordCount($this->getFileIndexKey());
190 }
191
192 /**
193 * Is there record previous
194 *
195 * @return bool
196 */
197 public function hasPrevRecord()
198 {
199 if ($this->current_record > 0) {
200 return true;
201 }
202
203 return false;
204 }
205
206 public function storeIndexes()
207 {
208 if (null === $this->config) {
209 return false;
210 }
211
212 return $this->config->storeIndexes($this->getFileIndexKey());
213 }
214
215 public function readIndexes()
216 {
217 if (null === $this->config) {
218 return false;
219 }
220
221 return $this->config->readIndexes($this->getFileIndexKey());
222
223 // return $this->index = $this->config->get( $this->getFileIndexKey() );
224 }
225
226 public function getFileIndexKey()
227 {
228 return 'file_index';
229 }
230
231 public function processing($processing = false)
232 {
233 $this->is_processing = true;
234 }
235
236 /**
237 * Read file size from handle and reset pointer back to current position
238 *
239 * @param resource $handle
240 * @return void
241 */
242 public function get_file_size($handle)
243 {
244 $current = ftell($handle);
245 fseek($handle, 0, SEEK_END);
246 $size = ftell($handle);
247 fseek($handle, $current);
248
249 return $size;
250 }
251 }
252