/wp-content/plugins/wp-all-import-pro/libraries/XmlImportSQLParse.php

https://gitlab.com/najomie/fit-hippie · PHP · 119 lines · 88 code · 30 blank · 1 comment · 8 complexity · 289299e1b98a8eb7fdce54b4085fc355 MD5 · raw file

  1. <?php
  2. class PMXI_SQLParser{
  3. public $xml_path;
  4. public $_filename;
  5. public $targetDir;
  6. public function __construct($path, $targetDir = false){
  7. $this->_filename = $path;
  8. $wp_uploads = wp_upload_dir();
  9. $this->targetDir = ( ! $targetDir ) ? wp_all_import_secure_file($wp_uploads['basedir'] . DIRECTORY_SEPARATOR . PMXI_Plugin::UPLOADS_DIRECTORY ) : $targetDir;
  10. }
  11. public function parse(){
  12. $tmpname = wp_unique_filename($this->targetDir, str_replace("sql", "xml", basename($this->_filename)));
  13. $this->xml_path = $this->targetDir . '/' . wp_all_import_url_title($tmpname);
  14. $this->toXML();
  15. return $this->xml_path;
  16. }
  17. protected function toXML(){
  18. $fp = fopen($this->_filename, 'rb');
  19. fseek($fp, 0);
  20. $xmlWriter = new XMLWriter();
  21. $xmlWriter->openURI($this->xml_path);
  22. $xmlWriter->setIndent(true);
  23. $xmlWriter->setIndentString("\t");
  24. $xmlWriter->startDocument('1.0', 'UTF-8');
  25. $xmlWriter->startElement('data');
  26. while( ! feof($fp) )
  27. {
  28. //reset time limit for big files
  29. set_time_limit(0);
  30. $sql = fread($fp, 1024 * 8);
  31. $count = preg_match_all("%INSERT INTO .*;%Uis", $sql, $matches);
  32. if ( $count ){
  33. foreach ($matches[0] as $key => $insert) {
  34. $current_table = 'node';
  35. $table = preg_match_all("%INTO\s*[^\(].*\(%Uis", $insert, $table_matches);
  36. if ( $table )
  37. $current_table = sanitize_key(trim(trim(str_replace('INTO', '', trim($table_matches[0][0],'('))), '`'));
  38. $rawData = array();
  39. $headers = preg_match_all("%\(.*\)\s*VALUES%Uis", $insert, $headers_matches);
  40. if ( $headers ){
  41. foreach ($headers_matches[0] as $key => $found_headers) {
  42. $hdrs = explode(',', rtrim(ltrim(trim(rtrim(trim($found_headers), 'VALUES')),'('),')'));
  43. if ( ! empty($hdrs) ){
  44. foreach ($hdrs as $header) {
  45. $rawData[ sanitize_key(trim(trim($header), '`')) ] = '';
  46. }
  47. }
  48. }
  49. $values = preg_match_all("%\([^`].*\)\s*[,|;]{1}%Uis", $insert, $values_matches);
  50. if ( $values ){
  51. foreach ($values_matches[0] as $key => $value) {
  52. $insertData = array();
  53. $vals = explode(',', rtrim(ltrim(trim(rtrim(rtrim(trim($value), ','),';')),'('),')'));
  54. if ( ! empty($vals) ){
  55. $i = 0;
  56. foreach ($rawData as $r_key => $v) {
  57. foreach ($vals as $k => $val) {
  58. if ($i == $k) $insertData[$r_key] = trim(trim($val),"'");
  59. }
  60. $i++;
  61. }
  62. }
  63. if ( ! empty($insertData)){
  64. $xmlWriter->startElement($current_table);
  65. foreach ($insertData as $h => $xml_value) {
  66. $xmlWriter->startElement($h);
  67. $xmlWriter->writeCData($xml_value);
  68. $xmlWriter->endElement();
  69. }
  70. $xmlWriter->endElement();
  71. }
  72. }
  73. }
  74. }
  75. }
  76. }
  77. }
  78. fclose($fp);
  79. $xmlWriter->endElement();
  80. $xmlWriter->flush(true);
  81. return true;
  82. }
  83. }