Created
February 14, 2016 23:18
-
-
Save dallasread/b3adab4e39d381f7074e to your computer and use it in GitHub Desktop.
WordPress Search and Replace for changing domains
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/** | |
* Search and Replace on a MySQL DB that has PHP serialized fields. | |
* By Dallas Read, 2015. | |
*/ | |
$host = isset($argv[1]) ? $argv[1] : false; | |
$user = isset($argv[2]) ? $argv[2] : false; | |
$password = isset($argv[3]) ? $argv[3] : false; | |
$db = isset($argv[4]) ? $argv[4] : false; | |
$search = isset($argv[5]) ? $argv[5] : false; | |
$replace = isset($argv[6]) ? $argv[6] : false; | |
if ($db == 'dev') { | |
ini_set("display_errors",1); | |
ini_set("display_startup_errors",1); | |
error_reporting(-1); | |
$results = search_and_replace($password, $host, $user); | |
die(); | |
} else { | |
// error_reporting(0); | |
// @ini_set('display_errors', 0); | |
} | |
if (!$host || !$user || !$password || !$db || !$search || !$replace) { | |
echo 'One or more arguments is missing.'; | |
die(); | |
} | |
$cid = mysql_connect($host, $user, $password); | |
$tables_list = mysql_db_query($db, "SHOW TABLES", $cid); | |
$tables = array(); | |
while ($table = mysql_fetch_array($tables_list)) { | |
$table = $table[0]; | |
$cols = array(); | |
$columns = mysql_db_query($db, "DESCRIBE " . $table, $cid); | |
while ($column = mysql_fetch_assoc($columns)) { | |
array_push($cols, $column["Field"]); | |
} | |
$tables[$table] = $cols; | |
} | |
foreach ($tables as $table_name => $columns) { | |
$rows = mysql_db_query($db, "SELECT * FROM " . $table_name, $cid); | |
while ($row = mysql_fetch_array($rows)) { | |
foreach ($columns as $col) { | |
$original_data = $row[$col]; | |
$new_data = $original_data; | |
$new_data = search_and_replace($new_data, $search, $replace); | |
if ($new_data != $original_data) { | |
$sql = 'UPDATE ' . $table_name . ' SET ' | |
. $col . ' = "' . mysql_real_escape_string($new_data) . '"' | |
. ' WHERE ' . $col .' = "' . mysql_real_escape_string($original_data) . '";'; | |
mysql_db_query($db, $sql, $cid); | |
} | |
} | |
} | |
} | |
function search_and_replace($data, $search, $replace) { | |
$new_data = @unserialize($data); | |
if ($new_data) { | |
$new_data = recursive_array_replace($search, $replace, $new_data); | |
$new_data = serialize($new_data); | |
print_r($new_data . "\n\n\n"); | |
return $new_data; | |
} else { | |
return recursive_array_replace($search, $replace, $data); | |
} | |
} | |
function recursive_array_replace($find, $replace, &$data) { | |
if (is_array($data)) { | |
foreach ($data as $key => $value) { | |
if (is_array($value)) { | |
$new_key = $key; | |
if (!is_numeric($new_key)) { | |
$new_key = str_replace($find, $replace, $new_key); | |
if ($new_key !== $key) { | |
unset($data[$key]); | |
} | |
} | |
$data[$new_key] = $value; | |
recursive_array_replace($find, $replace, $data[$new_key]); | |
} else if (is_string($value)) { | |
$data[$key] = str_replace($find, $replace, $value); | |
} | |
} | |
} else if (is_string($data)) { | |
$data = str_replace($find, $replace, $data); | |
} | |
return $data; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment