Plugin Directory

Changeset 1509894


Ignore:
Timestamp:
10/07/2016 08:55:45 AM (9 years ago)
Author:
swain.tara
Message:

Serialized string replace and backup features added.

Location:
find-and-replace-all/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • find-and-replace-all/trunk/frform.php

    r1150781 r1509894  
    22$findstr = isset($_POST['findstr']) ? esc_attr($_POST['findstr']) : '';
    33$replacestr = isset($_POST['replacestr']) ? esc_attr($_POST['replacestr']) : '';
    4 if ($_SERVER['REQUEST_METHOD'] == 'POST' and $findstr!='') {
     4if ($_SERVER['REQUEST_METHOD'] == 'POST' and $findstr != '') {
     5
     6    $url = backup_database();
     7
     8    echo "<br /><strong>Database backup completed <a href=\"$url\">Click Here to Download</a><br /></strong>";
     9
    510    global $wpdb;
    611    $tables = $wpdb->get_results('SHOW TABLES FROM ' . DB_NAME, ARRAY_N);
     
    813    foreach ($tables as $tablearr) {
    914        $table = $tablearr[0];
    10         $columns = $wpdb->get_results("SHOW COLUMNS FROM $table");
     15
     16        $primary_column = $wpdb->get_var("SHOW COLUMNS FROM $table WHERE `key`='PRI'");
     17        if ($primary_column == '') {
     18            continue;
     19        }
     20        $columns = $wpdb->get_results("SHOW COLUMNS FROM $table WHERE `key`!='PRI'");
     21
    1122        foreach ($columns as $column) {
    12             if (isset($column->Key) and $column->Key != 'PRI') {
     23            if (isset($column->Key)) {
    1324                $field = $column->Field;
    14                 $csql = "UPDATE $table SET `$field` = REPLACE(`$field`, '$findstr', '$replacestr');";
    15                 $wpdb->get_results($csql);
     25                //$csql = "UPDATE $table SET `$field` = REPLACE(`$field`, '$findstr', '$replacestr');";
     26                $csql = "SELECT `$primary_column`, `$field` FROM $table";
     27                $cresults = $wpdb->get_results($csql);
     28                foreach ($cresults as $cresult) {
     29                    $primary_id = (int) $cresult->$primary_column;
     30
     31                    if (is_serialized_string($cresult->$field)) {
     32                        $unserialized = @unserialize($cresult->$field);
     33                        if ($unserialized) {
     34                            $unserialized = esc_sql(replace_recursive($unserialized, $findstr, $replacestr));
     35                            $updatesql = "UPDATE $table SET `$field` = '$unserialized' WHERE `$primary_column`='$primary_id'";
     36                            $wpdb->get_results($updatesql);
     37                        }
     38                    } else {
     39                        $replacedstr = esc_sql(str_replace($findstr, $replacestr, $cresult->$field));
     40                        $updatesql = "UPDATE $table SET `$field` = '$replacedstr' WHERE `$primary_column`='$primary_id'";
     41                        $wpdb->get_results($updatesql);
     42                    }
     43                }
     44
     45                echo "<br />Replacing in table {$table}.{$field}";
    1646            }
    1747        }
    18         echo "<br />Replacing in table $table";
     48        echo "<br />All fields replaced in table $table";
    1949    }
    2050    echo "<br />All done. Okay!!";
     51   
     52    echo "<br /><strong>Database backup: <a href=\"$url\">Click Here to Download</a><br /></strong>";
    2153}
     54
    2255?>
    2356<script>
  • find-and-replace-all/trunk/functions.php

    r1150781 r1509894  
    44  Plugin Name: Find and Replace All
    55  Description: A wordpress plugin to find and replace from all the tables and all the fields
    6   Version: 1.0
     6  Version: 1.1
    77  Author: Taraprasad Swain
    88  Author URI: http://www.taraprasad.com
    99
    10   Copyright 2015 by Taraprasad.com (email : swain.tara@gmail.com)
     10  Copyright 2016 by Taraprasad.com (email : swain.tara@gmail.com)
    1111
    1212  This program is free software; you can redistribute it and/or modify
     
    3737    include('frform.php');
    3838}
     39
     40if (!function_exists('replace_recursive')) {
     41
     42    function replace_recursive(Array $array, $key, $value) {
     43        array_walk_recursive($array, function(&$v, $k) use ($key, $value) {
     44            $k == $key && $v = $value;
     45        });
     46        return $array;
     47    }
     48
     49}
     50
     51if (!function_exists('backup_database')) {
     52
     53    function backup_database($tables = '*') {
     54
     55        $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
     56
     57        mysql_select_db(DB_NAME, $link);
     58
     59        if ($tables == '*') {
     60            $tables = array();
     61            $result = mysql_query('SHOW TABLES');
     62            while ($row = mysql_fetch_row($result)) {
     63                $tables[] = $row[0];
     64            }
     65        } else {
     66            $tables = is_array($tables) ? $tables : explode(',', $tables);
     67        }
     68
     69        foreach ($tables as $table) {
     70            $result = mysql_query('SELECT * FROM ' . $table);
     71            $num_fields = mysql_num_fields($result);
     72
     73            $return .= "DROP TABLE IF EXISTS `{$table}`;";
     74
     75            $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
     76            $return.= "\n\n" . $row2 [1] . ";\n\n";
     77
     78            for ($i = 0; $i < $num_fields; $i++) {
     79                while ($row = mysql_fetch_row($result)) {
     80                    $return.= 'INSERT INTO ' . $table . ' VALUES(';
     81
     82                    for ($j = 0; $j < $num_fields; $j++) {
     83                        $row[$j] = addslashes($row[$j]);
     84                        $row[$j] = ereg_replace("\n", "\\n", $row[$j]);
     85                        if (isset($row[$j])) {
     86                            $return.= '"' . $row[$j] . '"';
     87                        } else {
     88                            $return.= '""';
     89                        }
     90                        if ($j < ($num_fields - 1)) {
     91                            $return.= ',';
     92                        }
     93                    }
     94                    $return.= ");\n";
     95                }
     96            }
     97            $return.="\n\n\n";
     98        }
     99
     100        $filename = '/backups/db-backup-' . date('Y-m-d-His') . '.sql';
     101
     102        $file_path = WP_CONTENT_DIR . $filename;
     103
     104        if (!file_exists(WP_CONTENT_DIR . '/backups')) {
     105            mkdir(WP_CONTENT_DIR . '/backups', 0755, true);
     106            file_put_contents(WP_CONTENT_DIR . '/backups/index.html', '');
     107        }
     108
     109        file_put_contents($file_path, $return);
     110
     111        return WP_CONTENT_URL . $filename;
     112    }
     113
     114}
  • find-and-replace-all/trunk/readme.txt

    r1488267 r1509894  
    55Requires at least: 3.0.1
    66Tested up to: 4.6
    7 Stable tag: 1.0
     7Stable tag: 1.1
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    4040== Changelog ==
    4141
     42= V1.1 - 07.10.2016 =
     43*Release Date - 7th October, 2016*
     44
     45* Plugin supports serialized string replace
     46* Backups before string replace
     47
    4248= V1.0 - 29.04.2015 =
    4349* Plugin created
Note: See TracChangeset for help on using the changeset viewer.