Skip to content

Commit

Permalink
Merge pull request #82 from gerbilOFdoom/master
Browse files Browse the repository at this point in the history
Add CSV Format search/replace to Search & Replace Tab
  • Loading branch information
bueltge committed Jun 15, 2017
2 parents e7470e3 + c589a3f commit afc7553
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 20 deletions.
22 changes: 17 additions & 5 deletions inc/Database/Exporter.php
Expand Up @@ -77,7 +77,7 @@ public function __construct( Replace $replace, Manager $dbm ) {
* $report[ 'errors'] : WP_Error_object,
* $report ['changes'] : Array with replacements in tables
*/
public function db_backup( $search = '', $replace = '', $tables = array(), $domain_replace = FALSE, $new_table_prefix = '' ) {
public function db_backup( $search = '', $replace = '', $tables = array(), $domain_replace = FALSE, $new_table_prefix = '', $csv = null ) {

if ( count( $tables ) < 1 ) {
$tables = $this->dbm->get_tables();
Expand Down Expand Up @@ -158,7 +158,7 @@ public function db_backup( $search = '', $replace = '', $tables = array(), $doma
);

} else {
$table_report = $this->backup_table( $search, $replace, $table, $new_table_prefix );
$table_report = $this->backup_table( $search, $replace, $table, $new_table_prefix, $csv );
}
//log changes if any

Expand Down Expand Up @@ -195,7 +195,7 @@ public function db_backup( $search = '', $replace = '', $tables = array(), $doma
* @return array $table_report Reports the changes made to the db.
*/

public function backup_table( $search = '', $replace = '', $table, $new_table_prefix = '' ) {
public function backup_table( $search = '', $replace = '', $table, $new_table_prefix = '', $csv = null ) {

$table_report = array(
'table_name' => $table,
Expand Down Expand Up @@ -310,7 +310,15 @@ public function backup_table( $search = '', $replace = '', $table, $new_table_pr

$page_size = $this->page_size;
$pages = ceil( $row_count / $page_size );

//Prepare CSV data
if($csv != null) {
$csv_lines = explode("\n", $csv);
$csv_head = str_getcsv("search,replace");
$csv_array = array();
foreach ($csv_lines as $line) {
$csv_array[] = array_combine($csv_head, str_getcsv($line));
}
}
for ( $page = 0; $page < $pages; $page ++ ) {
$start = $page * $page_size;

Expand Down Expand Up @@ -344,7 +352,11 @@ public function backup_table( $search = '', $replace = '', $table, $new_table_pr
$search, $replace,
$value
);

if($csv != null) {
foreach($csv_array as $entry) {
$edited_data = $this->recursive_unserialize_replace( $entry['search'], $entry['replace'], $edited_data );
}
}
// Something was changed
if ( $edited_data !== $value ) {

Expand Down
29 changes: 21 additions & 8 deletions inc/Database/Replace.php
Expand Up @@ -77,9 +77,9 @@ public function __construct( Manager $dbm, Service\MaxExecutionTime $max_executi
* @return array|\WP_Error Collection of information gathered during the run.
*/

public function run_search_replace( $search, $replace, $tables ) {
public function run_search_replace( $search, $replace, $tables, $csv = null) {

if ( $search === $replace ){
if ( $search === $replace && $search != '' ){
return new \WP_Error( 'error', __( "Search and replace pattern can't be the same!" ) );
}

Expand All @@ -95,7 +95,7 @@ public function run_search_replace( $search, $replace, $tables ) {
foreach ( (array) $tables as $table ) {
//count tables
$report [ 'tables' ] ++;
$table_report = $this->replace_values( $search, $replace, $table );
$table_report = $this->replace_values( $search, $replace, $table, $csv );
//log changes if any

if ( 0 !== $table_report[ 'change' ] ) {
Expand All @@ -113,7 +113,7 @@ public function run_search_replace( $search, $replace, $tables ) {
return $report;
}

public function replace_values( $search = '', $replace = '', $table ) {
public function replace_values( $search = '', $replace = '', $table, $csv = null ) {

$table_report = array(
'table_name' => $table,
Expand All @@ -127,7 +127,7 @@ public function replace_values( $search = '', $replace = '', $table ) {
);

// check we have a search string, bail if not
if ( empty( $search ) ) {
if ( empty( $search ) && empty($csv) ) {
$table_report[ 'errors' ][] = 'Search string is empty';

return $table_report;
Expand All @@ -154,7 +154,15 @@ public function replace_values( $search = '', $replace = '', $table ) {

$page_size = $this->page_size;
$pages = ceil( $row_count / $page_size );

//Prepare CSV data
if($csv != null) {
$csv_lines = explode("\n", $csv);
$csv_head = str_getcsv("search,replace");
$csv_array = array();
foreach ($csv_lines as $line) {
$csv_array[] = array_combine($csv_head, str_getcsv($line));
}
}
for ( $page = 0; $page < $pages; $page ++ ) {

$start = $page * $page_size;
Expand Down Expand Up @@ -195,7 +203,12 @@ public function replace_values( $search = '', $replace = '', $table ) {

// Run a search replace on the data that'll respect the serialisation.
$edited_data = $this->recursive_unserialize_replace( $search, $replace, $data_to_fix );

// Run a search replace by CSV parameters if CSV input present
if($csv != null) {
foreach($csv_array as $entry) {
$edited_data = $this->recursive_unserialize_replace( $entry['search'], $entry['replace'], $edited_data );
}
}
// Something was changed
if ( $edited_data !== $data_to_fix ) {

Expand All @@ -214,7 +227,7 @@ public function replace_values( $search = '', $replace = '', $table ) {
$update = TRUE;

}

}

// Determine what to do with updates.
Expand Down
29 changes: 22 additions & 7 deletions inc/Page/SearchReplace.php
Expand Up @@ -140,10 +140,10 @@ public function save() {
//remove wp_magic_quotes
$search = stripslashes( filter_input( INPUT_POST, 'search' ) );
$replace = stripslashes( filter_input( INPUT_POST, 'replace' ) );

$csv = stripslashes( filter_input( INPUT_POST, 'csv' ) );
//if dry run is checked we run the replace function with dry run and return
if ( TRUE === $dry_run ) {
$this->run_replace( $search, $replace, $tables, $dry_run );
$this->run_replace( $search, $replace, $tables, $dry_run, $csv );

return FALSE;
}
Expand All @@ -152,11 +152,11 @@ public function save() {

if ( 'export' === $export_or_save ) {
//'export'-button was checked
$report = $this->dbe->db_backup( $search, $replace, $tables );
$report = $this->dbe->db_backup( $search, $replace, $tables, FALSE, '', $csv );
$this->downloader->show_modal( $report );
} else {
//"Save changes to database" was checked
$this->run_replace( $search, $replace, $tables, $dry_run );
$this->run_replace( $search, $replace, $tables, $dry_run, $csv );
}

return TRUE;
Expand All @@ -180,7 +180,7 @@ protected function get_submit_button_title() {
*
* @return null
*/
protected function run_replace( $search, $replace, $tables, $dry_run ) {
protected function run_replace( $search, $replace, $tables, $dry_run, $csv = null ) {

echo '<div class="updated notice is-dismissible">';
if ( $dry_run ) {
Expand All @@ -201,7 +201,7 @@ protected function run_replace( $search, $replace, $tables, $dry_run ) {
}
$this->replace->set_dry_run( $dry_run );

$report = $this->replace->run_search_replace( $search, $replace, $tables );
$report = $this->replace->run_search_replace( $search, $replace, $tables, $csv );

if ( is_wp_error( $report ) ) {
$this->add_error( __( $report->get_error_message(), 'search-and-replace' ) );
Expand Down Expand Up @@ -240,7 +240,7 @@ protected function is_request_valid() {
$replace = filter_input( INPUT_POST, 'replace' );

//if search field is empty and replace field is not empty quit. If both fields are empty, go on (useful for backup of single tables without changing)
if ( '' === $search && '' === $replace ) {
if ( '' === $search && '' !== $replace ) {
$this->add_error( esc_attr__( 'Search field is empty.', 'search-and-replace' ) );

return FALSE;
Expand Down Expand Up @@ -289,5 +289,20 @@ private function get_replace_value() {
}

}

/**
* shows the csv value in template
*/
private function get_csv_value() {

$csv = isset( $_POST[ 'csv' ] ) ? $_POST[ 'csv' ] : '';
$dry_run = isset( $_POST[ 'dry_run' ] ) ? TRUE : FALSE;
if ( $dry_run ) {
$csv = stripslashes( $csv );
$csv = htmlentities( $csv );
echo $csv;
}

}

}
4 changes: 4 additions & 0 deletions inc/templates/search_replace.php
Expand Up @@ -19,6 +19,10 @@
<th><label for="replace"><strong><?php esc_html_e( 'Replace with: ', 'search-and-replace' ); ?></strong></label></th>
<td><input id="replace" type="text" name="replace" value="<?php $this->get_replace_value() ?>" /></td>
</tr>
<tr>
<th><label for="csv"><strong><?php esc_html_e( 'CSV Format Search/Replace:', 'search-and-replace'); ?></strong></label></th>
<td><textarea id="csv" cols="62" name="csv" placeholder="search value, replace value (one per line)"><?php $this->get_csv_value() ?></textarea></td>
</tr>
<tr>
<th><strong><?php esc_html_e( 'Select tables', 'search-and-replace' ); ?></strong></th>
<td><?php $this->show_table_list(); ?><br>
Expand Down

0 comments on commit afc7553

Please sign in to comment.