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 | } |