forked from interconnectit/Search-Replace-DB
-
Notifications
You must be signed in to change notification settings - Fork 1
/
srdb.cli.php
executable file
·238 lines (200 loc) · 6.74 KB
/
srdb.cli.php
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/php -q
<?php
/**
* To run this script, execute something like this:
* `./srdb.cli.php -h localhost -u root -n test -s "findMe" -r "replaceMe"`
* use the --dry-run flag to do a dry run without searching/replacing.
*/
// php 5.3 date timezone requirement, shouldn't affect anything
date_default_timezone_set( 'Europe/London' );
// include the srdb class
require_once( realpath( dirname( __FILE__ ) ) . '/srdb.class.php' );
$opts = array(
'h:' => 'host:',
'n:' => 'name:',
'u:' => 'user:',
'p:' => 'pass:',
'c:' => 'char:',
's:' => 'search:',
'r:' => 'replace:',
't:' => 'tables:',
'i:' => 'include-cols:',
'x:' => 'exclude-cols:',
'g' => 'regex',
'l:' => 'pagesize:',
'z' => 'dry-run',
'e:' => 'alter-engine:',
'a:' => 'alter-collation:',
'v:' => 'verbose:',
'port:',
'help'
);
$required = array(
'h:',
'n:',
'u:',
'p:'
);
function strip_colons( $string ) {
return str_replace( ':', '', $string );
}
// store arg values
$arg_count = $_SERVER[ 'argc' ];
$args_array = $_SERVER[ 'argv' ];
$short_opts = array_keys( $opts );
$short_opts_normal = array_map( 'strip_colons', $short_opts );
$long_opts = array_values( $opts );
$long_opts_normal = array_map( 'strip_colons', $long_opts );
// store array of options and values
$options = getopt( implode( '', $short_opts ), $long_opts );
if ( isset( $options[ 'help' ] ) ) {
echo "
#####################################################################
interconnect/it Safe Search & Replace tool
#####################################################################
This script allows you to search and replace strings in your database
safely without breaking serialised PHP.
Please report any bugs or fork and contribute to this script via
Github: https://github.com/interconnectit/search-replace-db
Argument values are strings unless otherwise specified.
ARGS
-h, --host
Required. The hostname of the database server.
-n, --name
Required. Database name.
-u, --user
Required. Database user.
-p, --pass
Required. Database user's password.
--port
Optional. Port on database server to connect to.
The default is 3306. (MySQL default port).
-s, --search
String to search for or `preg_replace()` style
regular expression.
-r, --replace
None empty string to replace search with or
`preg_replace()` style replacement.
-t, --tables
If set only runs the script on the specified table, comma
separate for multiple values.
-i, --include-cols
If set only runs the script on the specified columns, comma
separate for multiple values.
-x, --exclude-cols
If set excludes the specified columns, comma separate for
multiple values.
-g, --regex [no value]
Treats value for -s or --search as a regular expression and
-r or --replace as a regular expression replacement.
-l, --pagesize
How rows to fetch at a time from a table.
-z, --dry-run [no value]
Prevents any updates happening so you can preview the number
of changes to be made
-e, --alter-engine
Changes the database table to the specified database engine
eg. InnoDB or MyISAM. If specified search/replace arguments
are ignored. They will not be run simultaneously.
-a, --alter-collation
Changes the database table to the specified collation
eg. utf8_unicode_ci. If specified search/replace arguments
are ignored. They will not be run simultaneously.
-v, --verbose [true|false]
Defaults to true, can be set to false to run script silently.
--help
Displays this help message ;)
";
exit;
}
// missing field flag, show all missing instead of 1 at a time
$missing_arg = false;
// check required args are passed
foreach( $required as $key ) {
$short_opt = strip_colons( $key );
$long_opt = strip_colons( $opts[ $key ] );
if ( ! isset( $options[ $short_opt ] ) && ! isset( $options[ $long_opt ] ) ) {
fwrite( STDERR, "Error: Missing argument, -{$short_opt} or --{$long_opt} is required.\n" );
$missing_arg = true;
}
}
// bail if requirements not met
if ( $missing_arg ) {
fwrite( STDERR, "Please enter the missing arguments.\n" );
exit( 1 );
}
// new args array
$args = array(
'verbose' => true,
'dry_run' => false
);
// create $args array
foreach( $options as $key => $value ) {
// transpose keys
if ( ( $is_short = array_search( $key, $short_opts_normal ) ) !== false )
$key = $long_opts_normal[ $is_short ];
// boolean options as is, eg. a no value arg should be set true
if ( in_array( $key, $long_opts ) )
$value = true;
switch ( $key ) {
// boolean options.
case 'verbose':
$value = (boolean)filter_var( $value, FILTER_VALIDATE_BOOLEAN );
break;
}
// change to underscores
$key = str_replace( '-', '_', $key );
$args[ $key ] = $value;
}
// modify the log output
class icit_srdb_cli extends icit_srdb {
public function log( $type = '' ) {
$args = array_slice( func_get_args(), 1 );
$output = "";
switch( $type ) {
case 'error':
list( $error_type, $error ) = $args;
$output .= "$error_type: $error";
break;
case 'search_replace_table_start':
list( $table, $search, $replace ) = $args;
$output .= "{$table}: replacing {$search} with {$replace}";
break;
case 'search_replace_table_end':
list( $table, $report ) = $args;
$time = number_format( $report[ 'end' ] - $report[ 'start' ], 8 );
$output .= "{$table}: {$report['rows']} rows, {$report['change']} changes found, {$report['updates']} updates made in {$time} seconds";
break;
case 'search_replace_end':
list( $search, $replace, $report ) = $args;
$time = number_format( $report[ 'end' ] - $report[ 'start' ], 8 );
$dry_run_string = $this->dry_run ? "would have been" : "were";
$output .= "
Replacing {$search} with {$replace} on {$report['tables']} tables with {$report['rows']} rows
{$report['change']} changes {$dry_run_string} made
{$report['updates']} updates were actually made
It took {$time} seconds";
break;
case 'update_engine':
list( $table, $report, $engine ) = $args;
$output .= $table . ( $report[ 'converted' ][ $table ] ? ' has been' : 'has not been' ) . ' converted to ' . $engine;
break;
case 'update_collation':
list( $table, $report, $collation ) = $args;
$output .= $table . ( $report[ 'converted' ][ $table ] ? ' has been' : 'has not been' ) . ' converted to ' . $collation;
break;
}
if ( $this->verbose )
echo $output . "\n";
}
}
$report = new icit_srdb_cli( $args );
// Only print a separating newline if verbose mode is on to separate verbose output from result
if ($args[ 'verbose' ]) {
echo "\n";
}
if ( $report && ( ( isset( $args[ 'dry_run' ] ) && $args[ 'dry_run' ] ) || empty( $report->errors[ 'results' ] ) ) ) {
echo "And we're done!\n";
} else {
echo "Check the output for errors. You may need to ensure verbose output is on by using -v or --verbose.\n";
}