Guest User

Untitled

a guest
Nov 18th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.53 KB | None | 0 0
  1. <?php
  2. /*
  3. * Wordpress postmeta find&replace code
  4. *
  5. * This file should be run on your source database to echo out update statements to run on your migrated database.
  6. * It is to address the issue of when doing a find/replace on an sql file during a wordpress migration, that breaks
  7. * any content changed within wordpress serialized meta values, particularly if you have custom post types etc.
  8. *
  9. * Do your normal find/replace over the exported sql, import it into your new database, then run this code and copy the resulting
  10. * update SQL into your new database to fix it.
  11. *
  12. * See line 80 ish for where to actually define your find/replace.
  13. *
  14. */
  15.  
  16.  
  17.  
  18. error_reporting(-1);
  19.  
  20. $dbhost = '';
  21. $dbname = '';
  22. $dbuser = '';
  23. $dbpass = '';
  24.  
  25. $postmeta_table = 'wp_postmeta';
  26.  
  27.  
  28. $dbconn = mysql_connect($dbhost, $dbuser, $dbpass);
  29. mysql_select_db($dbname, $dbconn);
  30.  
  31. $result = selectarray('SELECT * FROM wp_postmeta');
  32.  
  33.  
  34. if (is_array($result)){
  35.  
  36. foreach($result as $row){
  37.  
  38. $meta_id = $row['meta_id'];
  39. $meta_value = $row['meta_value'];
  40. if (is_serialized($meta_value)){
  41.  
  42. $new_meta_value = make_new_value($meta_value);
  43.  
  44. if (strlen($new_meta_value) != strlen($meta_value)) {
  45.  
  46. ob_start();
  47. echo($meta_value."<br>".strlen($meta_value)."<br><br>".$new_meta_value."<br>".strlen($new_meta_value)."<br>");
  48. echo("<br><hr><hr><br>");
  49. @$debug .= ob_get_clean();
  50.  
  51.  
  52. $sql .= "
  53. UPDATE wp_postmeta SET meta_value = '".mysql_real_escape_string($new_meta_value)."' WHERE meta_id = '$meta_id';
  54. ";
  55.  
  56.  
  57.  
  58. }
  59. }
  60. }
  61.  
  62. }
  63.  
  64. echo("<pre>$sql</pre>");
  65.  
  66.  
  67.  
  68.  
  69. function is_serialized($v){
  70.  
  71. return @unserialize($v)?true:false;
  72.  
  73. }
  74.  
  75.  
  76. function make_new_value($oldvalue){
  77. $val_a = unserialize($oldvalue);
  78.  
  79. if (is_array($val_a) && count($val_a)) {
  80. foreach($val_a as &$val){
  81.  
  82. //run your find & replace code here
  83. //e.g:
  84. $val = str_replace('findme', 'replacemewith', $val);
  85.  
  86. }
  87.  
  88. $new_val_ser = serialize($val_a);
  89.  
  90.  
  91. return $new_val_ser;
  92.  
  93. } else {
  94.  
  95. return $oldvalue;
  96. }
  97.  
  98. }
  99.  
  100. function selectarray($sql) {
  101.  
  102.  
  103.  
  104. $result = mysql_query($sql) or die(mysql_error());
  105.  
  106. if (!$result) {
  107. debug(mysql_error());
  108. }
  109.  
  110. $table_result=array();
  111. $r=0;
  112. while($row = mysql_fetch_assoc($result)){
  113. $arr_row=array();
  114. $c=0;
  115. while ($c < mysql_num_fields($result)) {
  116. $col = mysql_fetch_field($result, $c);
  117. $arr_row[$col -> name] = $row[$col -> name];
  118. $c++;
  119. }
  120. $table_result[$r] = $arr_row;
  121. $r++;
  122. }
  123.  
  124.  
  125. return $table_result;
  126.  
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133. ?>
Add Comment
Please, Sign In to add comment