PluginProbe ʕ •ᴥ•ʔ
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets / 4.1.1
WP All Import – Drag & Drop Import for CSV, XML, Excel & Google Sheets v4.1.1
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 / libraries / XmlImportSQLParse.php
wp-all-import / libraries Last commit date
ast 1 month ago XmlImportConfig.php 1 month ago XmlImportCsvParse.php 1 month ago XmlImportException.php 1 month ago XmlImportParser.php 1 month ago XmlImportReaderInterface.php 1 month ago XmlImportSQLParse.php 1 month ago XmlImportStringReader.php 1 month ago XmlImportTemplate.php 1 month ago XmlImportTemplateCodeGenerator.php 1 month ago XmlImportTemplateParser.php 1 month ago XmlImportTemplateScanner.php 3 days ago XmlImportToken.php 1 month ago XmlImportXLSParse.php 1 month ago wpaipclzip.lib.php 1 month ago
XmlImportSQLParse.php
123 lines
1 <?php
2 // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fopen
3 // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fread
4 // phpcs:disable WordPress.WP.AlternativeFunctions.file_system_operations_fclose
5 // phpcs:disable Squiz.PHP.DiscouragedFunctions.Discouraged
6
7 class PMXI_SQLParser{
8
9 public $xml_path;
10
11 public $_filename;
12
13 public $targetDir;
14
15 public function __construct($path, $targetDir = false){
16
17 $this->_filename = $path;
18
19 $wp_uploads = wp_upload_dir();
20
21 $this->targetDir = ( ! $targetDir ) ? wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY ) : $targetDir;
22 }
23
24 public function parse(){
25
26 $tmpname = wp_unique_filename($this->targetDir, str_replace("sql", "xml", basename($this->_filename)));
27
28 $this->xml_path = $this->targetDir . '/' . wp_all_import_url_title($tmpname);
29
30 $this->toXML();
31
32 return $this->xml_path;
33 }
34
35 protected function toXML(){
36
37 $fp = fopen($this->_filename, 'rb');
38 fseek($fp, 0);
39
40 $xmlWriter = new XMLWriter();
41 $xmlWriter->openURI($this->xml_path);
42 $xmlWriter->setIndent(true);
43 $xmlWriter->setIndentString("\t");
44 $xmlWriter->startDocument('1.0', 'UTF-8');
45 $xmlWriter->startElement('data');
46
47 while( ! feof($fp) )
48 {
49 //reset time limit for big files
50 set_time_limit(0);
51
52 $sql = fread($fp, 1024 * 8);
53
54 $count = preg_match_all("%INSERT INTO .*;%Uis", $sql, $matches);
55
56 if ( $count ){
57
58 foreach ($matches[0] as $key => $insert) {
59
60 $current_table = 'node';
61
62 $table = preg_match_all("%INTO\s*[^\(].*\(%Uis", $insert, $table_matches);
63
64 if ( $table )
65 $current_table = sanitize_key(trim(trim(str_replace('INTO', '', trim($table_matches[0][0],'('))), '`'));
66
67 $rawData = array();
68
69 $headers = preg_match_all("%\(.*\)\s*VALUES%Uis", $insert, $headers_matches);
70
71 if ( $headers ){
72
73 foreach ($headers_matches[0] as $hdr_key => $found_headers) {
74 $hdrs = explode(',', rtrim(ltrim(trim(rtrim(trim($found_headers), 'VALUES')),'('),')'));
75 if ( ! empty($hdrs) ){
76 foreach ($hdrs as $header) {
77 $rawData[ sanitize_key(trim(trim($header), '`')) ] = '';
78 }
79 }
80 }
81
82 $values = preg_match_all("%\([^`].*\)\s*[,|;]{1}%Uis", $insert, $values_matches);
83
84 if ( $values ){
85 foreach ($values_matches[0] as $val_key => $value) {
86 $insertData = array();
87 $vals = explode(',', rtrim(ltrim(trim(rtrim(rtrim(trim($value), ','),';')),'('),')'));
88 if ( ! empty($vals) ){
89 $i = 0;
90 foreach ($rawData as $r_key => $v) {
91 foreach ($vals as $k => $val) {
92 if ($i == $k) $insertData[$r_key] = trim(trim($val),"'");
93 }
94 $i++;
95 }
96 }
97 if ( ! empty($insertData)){
98
99 $xmlWriter->startElement($current_table);
100 foreach ($insertData as $h => $xml_value) {
101 $xmlWriter->startElement($h);
102 $xmlWriter->writeCData($xml_value);
103 $xmlWriter->endElement();
104 }
105 $xmlWriter->endElement();
106
107 }
108 }
109 }
110 }
111 }
112 }
113 }
114 fclose($fp);
115
116 $xmlWriter->endElement();
117
118 $xmlWriter->flush(true);
119
120 return true;
121
122 }
123 }