/library/Search-Replace-DB-master/searchreplacedb2cli.php

https://github.com/herewithme/wordpress-cli-tools · PHP · 170 lines · 115 code · 30 blank · 25 comment · 18 complexity · 605a6ea3a8dd65d1cee404398297a0f6 MD5 · raw file

  1. #!/usr/bin/php -q
  2. <?php
  3. /**
  4. * To run this script, execute something like this:
  5. * `./searchreplacedb2cli.php -h localhost -u root -d test -c utf\-8 -s "findMe" -r "replaceMe"`
  6. * use the --dry-run flag to do a dry run without searching/replacing.
  7. * this script currently affects all tables in a db there are @TODOs below...
  8. */
  9. require_once('searchreplacedb2.php'); // include the proper srdb script
  10. echo "########################### Ignore Above ###############################\n\n";
  11. // source: https://github.com/interconnectit/Search-Replace-DB/blob/master/searchreplacedb2.php
  12. /* Flags for options, all values required */
  13. $shortopts = "";
  14. $shortopts .= "h:"; // host name // $host
  15. $shortopts .= "d:"; // database name // $data
  16. $shortopts .= "u:"; // user name // $user
  17. $shortopts .= "p:"; // password // $pass
  18. $shortopts .= "c:"; // character set // $char
  19. $shortopts .= "s:"; // search // $srch
  20. $shortopts .= "r:"; // replace // $rplc
  21. $shortopts .= ""; // These options do not accept values
  22. // All long options require values
  23. $longopts = array(
  24. "host:", // $host
  25. "database:", // $data
  26. "user:", // $user
  27. "pass:", // $pass
  28. "charset:", // $char
  29. "search:", // $srch
  30. "replace:", // $rplc
  31. "help", // $help_text
  32. //@TODO write dry-run to also do a search without a replace.
  33. "dry-run", // engage in a dry run, print options, show results
  34. );
  35. /* Store arg values */
  36. $arg_count = $_SERVER["argc"];
  37. $args_array = $_SERVER["argv"];
  38. $options = getopt($shortopts, $longopts); // Store array of options and values
  39. // var_dump($options); // return all the values
  40. /* Map options to correct vars from srdb script */
  41. if (isset($options["h"])){
  42. $host = $options["h"];}
  43. elseif(isset($options["host"])){
  44. $host = $options["host"];}
  45. else{
  46. echo "Abort! Host name required, use --host or -h\n";
  47. exit;}
  48. if (isset($options["d"])){
  49. $data = $options["d"];}
  50. elseif(isset($options["database"])){
  51. $data = $options["database"];}
  52. else{
  53. echo "Abort! Database name required, use --database or -d\n";
  54. exit;}
  55. if (isset($options["u"])){
  56. $user = $options["u"];}
  57. elseif(isset($options["user"])){
  58. $user = $options["user"];}
  59. if (isset($options["p"])){
  60. $pass = $options["p"];}
  61. elseif(isset($options["pass"])){
  62. $pass = $options["pass"];}
  63. if (isset($options["c"])){
  64. $char = $options["c"];}
  65. elseif(isset($options["charset"])){
  66. $char = $options["charset"];}
  67. if (isset($options["s"])){
  68. $srch = $options["s"];}
  69. elseif(isset($options["search"])){
  70. $srch = $options["search"];}
  71. if (isset($options["r"])){
  72. $rplc = $options["r"];}
  73. elseif(isset($options["replace"])){
  74. $rplc = $options["replace"];}
  75. /* Show values if this is a dry-run */
  76. if (isset($options["dry-run"])){
  77. echo "Are you sure these are correct?\n";
  78. }
  79. echo "host: ".$host."\n";
  80. echo "database: ".$data."\n";
  81. echo "user: ".$user."\n";
  82. echo "pass: ".$pass."\n";
  83. echo "charset: ".$char."\n";
  84. echo "search: ".$srch."\n";
  85. echo "replace: ".$rplc."\n\n";
  86. /* Reproduce what's done in Case 3 to test the server before proceeding */
  87. $connection = @mysql_connect( $host, $user, $pass );
  88. if ( ! $connection ) {
  89. $errors[] = mysql_error( );
  90. echo "MySQL Connection Error: ";
  91. print_r($errors);
  92. }
  93. if ( ! empty( $char ) ) {
  94. if ( function_exists( 'mysql_set_charset' ) )
  95. mysql_set_charset( $char, $connection );
  96. else
  97. mysql_query( 'SET NAMES ' . $char, $connection ); // Shouldn't really use this, but there for backwards compatibility
  98. }
  99. // Do we have any tables and if so build the all tables array
  100. $all_tables = array( );
  101. @mysql_select_db( $data, $connection );
  102. $all_tables_mysql = @mysql_query( 'SHOW TABLES', $connection );
  103. if ( ! $all_tables_mysql ) {
  104. $errors[] = mysql_error( );
  105. echo "MySQL Table Error: ";
  106. print_r($errors);
  107. } else {
  108. while ( $table = mysql_fetch_array( $all_tables_mysql ) ) {
  109. $all_tables[] = $table[ 0 ];
  110. }
  111. echo "Tables: ";
  112. foreach($all_tables as $a_table){
  113. echo $a_table . ", ";
  114. }
  115. }
  116. /**
  117. * @TODO allow selection of one or more tables. For now, use all.
  118. */
  119. $tables = $all_tables;
  120. /* Execute Case 5 with the actual search + replace */
  121. if(!isset($options["dry-run"])){ // check if dry-run
  122. echo "\n\nWorking...";
  123. @ set_time_limit( 60 * 10 );
  124. // Try to push the allowed memory up, while we're at it
  125. @ ini_set( 'memory_limit', '1024M' );
  126. // Process the tables
  127. if ( isset( $connection ) )
  128. $report = icit_srdb_replacer( $connection, $srch, $rplc, $tables );
  129. // Output any errors encountered during the db work.
  130. if ( ! empty( $report[ 'errors' ] ) && is_array( $report[ 'errors' ] ) ) {
  131. echo "Find/Replace Errors: \n";
  132. foreach( $report[ 'errors' ] as $error )
  133. echo $error . '\n';
  134. }
  135. // Calc the time taken.
  136. $time = array_sum( explode( ' ', $report[ 'end' ] ) ) - array_sum( explode( ' ', $report[ 'start' ] ) );
  137. echo "Done. Report:\n\n";
  138. printf( 'In the process of replacing "%s" with "%s" we scanned %d tables with a total of %d rows, %d cells were changed and %d db update performed and it all took %f seconds.', $srch, $rplc, $report[ 'tables' ], $report[ 'rows' ], $report[ 'change' ], $report[ 'updates' ], $time );
  139. }
  140. ?>