Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- ini_set('memory_limit', '2048M');
- $host = "internal-db.s24347.gridserver.com";
- $db = "db24347_siterg";
- $user = "db24347";
- $pass = "new3081graf";
- $sqlfile = "ndp.sql";
- function getQueriesFromSQLFile($sqlfile){
- if(is_readable($sqlfile) === false){
- throw new Exception($sqlfile . 'does not exist or is not readable.');
- }
- # read file into array
- $file = file($sqlfile);
- # import file line by line
- # and filter (remove) those lines, beginning with an sql comment token
- $file = array_filter($file,
- create_function('$line',
- 'return strpos(ltrim($line), "--") !== 0;'));
- # and filter (remove) those lines, beginning with an sql notes token
- $file = array_filter($file,
- create_function('$line',
- 'return strpos(ltrim($line), "/*") !== 0;'));
- # this is a whitelist of SQL commands, which are allowed to follow a semicolon
- $keywords = array(
- 'ALTER', 'CREATE', 'DELETE', 'DROP', 'INSERT',
- 'REPLACE', 'SELECT', 'SET', 'TRUNCATE', 'UPDATE', 'USE'
- );
- # create the regular expression for matching the whitelisted keywords
- $regexp = sprintf('/\s*;\s*(?=(%s)\b)/s', implode('|', $keywords));
- # split there
- $splitter = preg_split($regexp, implode("\r\n", $file));
- # remove trailing semicolon or whitespaces
- $splitter = array_map(create_function('$line',
- 'return preg_replace("/[\s;]*$/", "", $line);'),
- $splitter);
- # replace the default database prefix "your_prefix_"
- $table_prefix = $_POST['config']['database']['prefix'];
- $splitter = preg_replace("/`your_prefix_/", "`$table_prefix", $splitter);
- # remove empty lines
- return array_filter($splitter, create_function('$line', 'return !empty($line);'));
- }
- //-------------------
- // Import database
- //-------------------
- try {
- $pdo = new PDO ("mysql:host=$host; dbname=$db", "$user", "$pass");
- $queries = getQueriesFromSQLFile($sqlfile);
- $countSteps = count($queries);
- $step = 1;
- foreach($queries as $query){
- echo "\n " . (($step*100)/$countSteps) . "% => Query " . $step . " de " . $countSteps . "\n";
- $pdo->exec(utf8_decode($query));
- $step++;
- }
- } catch ( PDOException $e ) {
- echo $e->getMessage ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement