forked from ttodua/useful-php-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database-modifier-when-migrating-wordpress
64 lines (54 loc) · 3.85 KB
/
database-modifier-when-migrating-wordpress
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
// LIVE DEMO: https://puvox.software/tools/wordpress-migrator
if (empty($_FILES['dbfilee'])) {
echo '<!DOCTYPE html><head><title>Replace strings in DB</title></head><body>';
?>
<h1>Intelligently replace strings and domains with new values</h1>
<b>DESCRIPTION:</b> this script will help you MUCH!! sometimes, when migrating site (or in SQL file, you want to replace strings), dont do it manually! because the strings may be present in SERIALIZED ARRAYS too, and that needs special replacement, not just the direct replacement... so, this script will do that... ( But also, read some notes <a href="https://puvox.software/blog/migrating-normal-wp-site-as-multisite" target="_blank">HERE</a>; You can download <a href="https://github.com/ttodua/useful-php-scripts/">SOURCE CODE</a> of this script)
<br/><br/>
<form style="background:rgb(184, 231, 133);padding:10px;" action="" method="POST" enctype="multipart/form-data">
drag database file here:<input type="file" name="dbfilee" /><br/>
Old string <input type="text" value="" placeholder="i.e. word1 or http://domain1.com" name="oldstr" style="width:300px;" /><br/>
new string<input type="text" value="" placeholder="i.e. word2 or http://domain2.com" style="width:300px;" name="newstr" /><br/>
(Case sensitive replacement : <input type="hidden" name="case_sensitive" value="0" /> <input type="checkbox" name="case_sensitive" value="1" /> )<br/>
<input type="submit" value="GENERATE NEW DB" /> (then wait some seconds ,according to DB size ...)
</form>
<div style=" padding:10px;margin:30px 0 0;">
<br/>p.s. at this moment, i can also advise to use more advanced scripts, like :
<br/>1) http://wptavern.com/3-efficient-tools-for-wordpress-database-migration
<br/>2) https://wordpress.org/plugins/better-search-replace/
</div>
<?php
}
else {
if(empty($_FILES["dbfilee"]["tmp_name"])) {
exit ("Increase PHP upload & post max size");
}
function DOMAIN_or_STRING_modifier_in_DB($old_string, $new_string, $sql_content, $download = false, $case_sensitive=false){
set_time_limit(1000);
$replace_type = $case_sensitive ? 'str_replace' : 'str_ireplace';
$length_difference= strlen($old_string)- strlen($new_string); //i.e. FROM site.com TO example.com
$old_string_slashed=str_replace('/','\/',$old_string);
// Replace every occurence of Serialized arrays, i.e. {s:32:"blablabla"}
preg_match_all('/(|\}|\{|\;\|)s\:(\d{1,5})\:\"(.*?)\";/si', $sql_content, $n1, PREG_SET_ORDER);
preg_match_all('/(|\}|\{|\;\|)s\:(\d{1,5})\:\\\"(.*?)\";/si', $sql_content, $n2, PREG_SET_ORDER); $n=array_merge($n1,$n2);
foreach($n as $Each_Serialized_String){ $e=$Each_Serialized_String; //var_dump($e); 0=>{s:12:"blablablabla" 1=>; 2=>17 3=>"blablablabla"
if(stripos($e[3],$old_string)!==false){ //if string found in line, then go to next
if(!is_numeric($e[2])) { continue; } else { $found_char_length= intval($e[2]);}
$before_s = $e[1]; //i.e. } or { or ; or |
$found_line = $e[0];
$found_line_changed = str_replace(
array($before_s.'s:'.$found_char_length, $old_string),
array($before_s.'s:'.($found_char_length - $length_difference), $new_string),
$found_line);
$sql_content = str_replace($found_line, call_user_func($replace_type, $old_string,$new_string,$found_line_changed), $sql_content);
}
}
// Now, we can freely replace typical occurences
$sql_content=call_user_func($replace_type, $old_string,$new_string,$sql_content);
if ($download) { header('Content-Type: application/octet-stream'); header("Content-Transfer-Encoding: Binary"); header('Content-disposition: attachment; filename="db_'.rand(1,99999).'.sql"'); echo $sql_content; exit; } else {return $sql_content;}
}
// EXECUTE !!!
DOMAIN_or_STRING_modifier_in_DB($_POST['oldstr'], $_POST['newstr'], file_get_contents($_FILES["dbfilee"]["tmp_name"]), true, $_POST['case_sensitive']);
}
?>