PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
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 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Importer / File / JSONFile.php

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

126 lines 3.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\FileInterface;
6
7 class JSONFile extends AbstractIndexedFile implements FileInterface
8 {
9 private $base_path;
10
11 private $chunk;
12 private $chunk_size = 8192;
13 private $chunk_offset = 0;
14
15 private $record_counter = 0;
16 private $record_start_index = 0;
17
18 public function setRecordPath($path)
19 {
20 $this->base_path = "\"{$path}\"";
21 }
22
23 /**
24 * Generate record file positions
25 *
26 * Loop through each record and save each position
27 */
28 public function generateIndex()
29 {
30 $this->record_counter = 0;
31 $this->record_start_index = 0;
32
33 rewind($this->getFileHandle());
34 while (!feof($this->getFileHandle())) {
35
36 $this->chunk .= $this->getChunk();
37 $this->processChunk();
38 }
39 }
40
41 /**
42 * Read chunk from file
43 *
44 * @return bool|string
45 */
46 public function getChunk()
47 {
48 return fread($this->getFileHandle(), $this->chunk_size);
49 }
50
51 public function processChunk()
52 {
53
54 $regex_parts = [
55 '"[^"\\\\]*(?:\\\\.[^"\\\\]*)*"', // skip escaped \"
56 "'[^'\\\\]*(?:\\\\.[^'\\\\]*)*'", // skip escaped \'
57 '([^,"\'{}\[\]:\s]+)', // values not in quotes or double quotes
58 '({)',
59 '(})',
60 '(\[)',
61 '(\])',
62 '(:)',
63 '(,)',
64 ];
65
66 $open_tags = ['{', '['];
67 $close_tags = ['}', ']'];
68
69 $regex = '/' . implode('|', $regex_parts) . '/s';
70 $track_depth = false;
71 $track_depth_init = false;
72 $depth = 0;
73 $depth_to_capture = 1;
74
75 while (preg_match($regex, $this->chunk, $matches, PREG_OFFSET_CAPTURE) !== 0) {
76 list($captured, $offset) = $matches[0];
77
78 $this->record_offset = $offset;
79 $depth_changed = false;
80
81 if (true === $track_depth) {
82
83 if (in_array($captured, $open_tags)) {
84 $depth++;
85 $depth_changed = true;
86 } elseif (in_array($captured, $close_tags)) {
87 $depth--;
88 $depth_changed = true;
89 }
90
91 if ($depth_changed) {
92 if ($depth === 0) {
93 $this->record_start_index = $this->chunk_offset + $this->record_offset;
94 } elseif ($depth < -1) {
95 $track_depth = false;
96 } elseif ($depth === -1) {
97 $this->setIndex(
98 $this->record_counter,
99 $this->record_start_index,
100 $this->chunk_offset + $this->record_offset + strlen($captured)
101 );
102 $this->record_counter++;
103 }
104 }
105 } elseif ($track_depth_init && $depth_to_capture >= 0) {
106 if (in_array($captured, $open_tags)) {
107 $depth_to_capture--;
108 if ($depth_to_capture < 0) {
109 $track_depth = true;
110 $this->record_start_index = $this->chunk_offset + $this->record_offset;
111 }
112 }
113 }
114
115
116 if ($captured === $this->base_path) {
117 $track_depth_init = true;
118 }
119
120 $string_offset = $offset + strlen($captured);
121 $this->chunk_offset += $string_offset;
122 $this->chunk = substr($this->chunk, $string_offset);
123 }
124 }
125 }
126