Advertisement
Guest User

Untitled

a guest
Dec 21st, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. ini_set('memory_limit', '2048M');
  4. $host = "internal-db.s24347.gridserver.com";
  5. $db = "db24347_siterg";
  6. $user = "db24347";
  7. $pass = "new3081graf";
  8. $sqlfile = "ndp.sql";
  9. function getQueriesFromSQLFile($sqlfile){
  10. if(is_readable($sqlfile) === false){
  11. throw new Exception($sqlfile . 'does not exist or is not readable.');
  12. }
  13. # read file into array
  14. $file = file($sqlfile);
  15. # import file line by line
  16. # and filter (remove) those lines, beginning with an sql comment token
  17. $file = array_filter($file,
  18. create_function('$line',
  19. 'return strpos(ltrim($line), "--") !== 0;'));
  20. # and filter (remove) those lines, beginning with an sql notes token
  21. $file = array_filter($file,
  22. create_function('$line',
  23. 'return strpos(ltrim($line), "/*") !== 0;'));
  24. # this is a whitelist of SQL commands, which are allowed to follow a semicolon
  25. $keywords = array(
  26. 'ALTER', 'CREATE', 'DELETE', 'DROP', 'INSERT',
  27. 'REPLACE', 'SELECT', 'SET', 'TRUNCATE', 'UPDATE', 'USE'
  28. );
  29. # create the regular expression for matching the whitelisted keywords
  30. $regexp = sprintf('/\s*;\s*(?=(%s)\b)/s', implode('|', $keywords));
  31. # split there
  32. $splitter = preg_split($regexp, implode("\r\n", $file));
  33. # remove trailing semicolon or whitespaces
  34. $splitter = array_map(create_function('$line',
  35. 'return preg_replace("/[\s;]*$/", "", $line);'),
  36. $splitter);
  37. # replace the default database prefix "your_prefix_"
  38. $table_prefix = $_POST['config']['database']['prefix'];
  39. $splitter = preg_replace("/`your_prefix_/", "`$table_prefix", $splitter);
  40. # remove empty lines
  41. return array_filter($splitter, create_function('$line', 'return !empty($line);'));
  42. }
  43. //-------------------
  44. // Import database
  45. //-------------------
  46. try {
  47. $pdo = new PDO ("mysql:host=$host; dbname=$db", "$user", "$pass");
  48. $queries = getQueriesFromSQLFile($sqlfile);
  49. $countSteps = count($queries);
  50. $step = 1;
  51. foreach($queries as $query){
  52. echo "\n " . (($step*100)/$countSteps) . "% => Query " . $step . " de " . $countSteps . "\n";
  53. $pdo->exec(utf8_decode($query));
  54. $step++;
  55. }
  56.  
  57. } catch ( PDOException $e ) {
  58. echo $e->getMessage ();
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement