PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / 3.2.2
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets v3.2.2
4.1.1 3.9.5 3.9.6 4.0.0 4.0.1 4.1.0 trunk 2.12 2.13 2.14 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.4.7 3.4.8 3.4.9 3.5.0 3.5.1 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.6.9 3.7.0 3.7.1 3.7.2 3.7.3 3.7.3-beta-1.0 3.7.4 3.7.4-beta-1.0 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8.0 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4
wp-all-import / classes / chunk.php
wp-all-import / classes Last commit date
chunk.php 11 years ago config.php 11 years ago download.php 11 years ago handler.php 11 years ago helper.php 11 years ago input.php 11 years ago nested.php 11 years ago render.php 11 years ago session.php 11 years ago upload.php 11 years ago
chunk.php
252 lines
1 <?php
2 /**
3 * Chunk
4 *
5 * Reads a large file in as chunks for easier parsing.
6 *
7 *
8 * @package default
9 * @author Max Tsiplyakov
10 */
11 class PMXI_Chunk {
12 /**
13 * options
14 *
15 * @var array Contains all major options
16 * @access public
17 */
18 public $options = array(
19 'path' => './', // string The path to check for $file in
20 'element' => '', // string The XML element to return
21 'type' => 'upload',
22 'encoding' => 'UTF-8',
23 'pointer' => 1,
24 'chunkSize' => 1024
25 );
26
27 /**
28 * file
29 *
30 * @var string The filename being read
31 * @access public
32 */
33 public $file = '';
34 /**
35 * pointer
36 *
37 * @var integer The current position the file is being read from
38 * @access public
39 */
40 public $reader;
41 public $cloud = array();
42 public $loop = 1;
43
44 /**
45 * handle
46 *
47 * @var resource The fopen() resource
48 * @access private
49 */
50 private $handle = null;
51 /**
52 * reading
53 *
54 * @var boolean Whether the script is currently reading the file
55 * @access private
56 */
57
58 /**
59 * __construct
60 *
61 * Builds the Chunk object
62 *
63 * @param string $file The filename to work with
64 * @param array $options The options with which to parse the file
65 * @author Dom Hastings
66 * @access public
67 */
68 public function __construct($file, $options = array()) {
69
70 // merge the options together
71 $this->options = array_merge($this->options, (is_array($options) ? $options : array()));
72
73 $this->options['chunkSize'] *= PMXI_Plugin::getInstance()->getOption('chunk_size');
74
75 // set the filename
76 $this->file = $file;
77
78 if (empty($this->options['element'])){
79 //$founded_tags = array();
80
81 if (function_exists('stream_filter_register')){
82 stream_filter_register('preprocessxml', 'preprocessXml_filter');
83 $path = 'php://filter/read=preprocessxml/resource=' . $this->file;
84 }
85 else $path = $this->file;
86
87 $reader = new XMLReader();
88 $reader->open($path);
89 $reader->setParserProperty(XMLReader::VALIDATE, false);
90 while ( @$reader->read()) {
91 switch ($reader->nodeType) {
92 case (XMLREADER::ELEMENT):
93 if (array_key_exists(str_replace(":", "_", $reader->localName), $this->cloud))
94 $this->cloud[str_replace(":", "_", $reader->localName)]++;
95 else
96 $this->cloud[str_replace(":", "_", $reader->localName)] = 1;
97 //array_push($founded_tags, str_replace(":", "_", $reader->localName));
98
99 break;
100 default:
101
102 break;
103 }
104 }
105 unset($reader);
106
107 /*if (!empty($founded_tags)) {
108 $element_counts = array_count_values($founded_tags);
109 if (!empty($element_counts)){
110 foreach ($element_counts as $tag => $count)
111 if (strpos($tag, ":") === false)
112 $this->cloud[$tag] = $count;
113
114 arsort($element_counts);
115 }
116 } */
117
118 if (!empty($this->cloud)){
119
120 arsort($this->cloud);
121
122 $main_elements = array('node', 'product', 'job', 'deal', 'entry', 'item', 'property', 'listing', 'hotel', 'record', 'article', 'post', 'book');
123
124 foreach ($this->cloud as $element_name => $value) {
125 if ( in_array(strtolower($element_name), $main_elements) ){
126 $this->options['element'] = $element_name;
127 break;
128 }
129 }
130 if (empty($this->options['element'])){
131 foreach ($this->cloud as $el => $count) {
132 $this->options['element'] = $el;
133 break;
134 }
135 }
136 }
137 }
138
139 if (function_exists('stream_filter_register')){
140 stream_filter_register('preprocessxml', 'preprocessXml_filter');
141 $path = 'php://filter/read=preprocessxml/resource=' . $this->file;
142 }
143 else $path = $this->file;
144
145 $this->reader = new XMLReader();
146 @$this->reader->open($path);
147 @$this->reader->setParserProperty(XMLReader::VALIDATE, false);
148
149
150 }
151
152 /**
153 * __destruct
154 *
155 * Cleans up
156 *
157 * @return void
158 * @author Dom Hastings
159 * @access public
160 */
161 public function __destruct() {
162 // close the file resource
163 unset($this->reader);
164 }
165
166 /**
167 * read
168 *
169 * Reads the first available occurence of the XML element $this->options['element']
170 *
171 * @return string The XML string from $this->file
172 * @author Dom Hastings
173 * @access public
174 */
175 public function read($debug = false) {
176
177 // trim it
178 $element = trim($this->options['element']);
179
180 $xml = '';
181
182 try {
183 while ( @$this->reader->read() ) {
184 switch ($this->reader->nodeType) {
185 case (XMLREADER::ELEMENT):
186 if ( strtolower(str_replace(":", "_", $this->reader->localName)) == strtolower($element) ) {
187
188 if ($this->loop < $this->options['pointer']){
189 $this->loop++;
190 continue;
191 }
192
193 $xml = @$this->reader->readOuterXML();
194
195 break(2);
196 }
197 break;
198 default:
199 // code ...
200 break;
201 }
202 }
203 } catch (XmlImportException $e) {
204 $xml = false;
205 }
206
207 return ( ! empty($xml) ) ? $this->removeColonsFromRSS(preg_replace('%xmlns.*=\s*([\'"]).*\1%sU', '', $xml)) : false;
208
209 }
210
211 function removeColonsFromRSS($feed) {
212
213 // pull out colons from start tags
214 // (<\w+):(\w+>)
215 $pattern = '/(<\w+):(\w+[ |>]{1})/i';
216 $replacement = '<$2';
217 $feed = preg_replace($pattern, $replacement, $feed);
218 // pull out colons from end tags
219 // (<\/\w+):(\w+>)
220 $pattern = '/(<\/\w+):(\w+>)/i';
221 $replacement = '</$2';
222 $feed = preg_replace($pattern, $replacement, $feed);
223 // pull out colons from attributes
224 $pattern = '/(\s+\w+):(\w+[=]{1})/i';
225 $replacement = '$1_$2';
226 $feed = preg_replace($pattern, $replacement, $feed);
227 // pull colons from single element
228 // (<\w+):(\w+\/>)
229 $pattern = '/(<\w+):(\w+\/>)/i';
230 $replacement = '<$2';
231 $feed = preg_replace($pattern, $replacement, $feed);
232
233 return $feed;
234
235 }
236
237 }
238
239 class preprocessXml_filter extends php_user_filter {
240
241 function filter($in, $out, &$consumed, $closing)
242 {
243 while ($bucket = stream_bucket_make_writeable($in)) {
244 PMXI_Import_Record::preprocessXml($bucket->data);
245 $consumed += $bucket->datalen;
246 stream_bucket_append($out, $bucket);
247 }
248 return PSFS_PASS_ON;
249 }
250
251 }
252