-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_replace.php
More file actions
526 lines (463 loc) · 10.4 KB
/
search_replace.php
File metadata and controls
526 lines (463 loc) · 10.4 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
<?php
/**
* Search and replace class for text based files
*
* @package PHP Search & Replace
* @version 1.0.1
* @author MT Jordan <mtjo62@gmail.com>
* @copyright 2014
* @license zlib/libpng
* @link
*/
class search_replace
{
/**********************************************
* Internal private variables
*********************************************/
/**
* Path of directory to be searched
*
* @access private
* @var str
*/
private $dir_path;
/**
* User defined array of inclusive extensions
*
* @access private
* @var array
*/
private $set_extension = array();
/**
* Array of search file paths
*
* @access private
* @var array
*/
private $file_array;
/**
* Count of valid files to be searched
*
* @access private
* @var int
*/
private $file_cnt = 0;
/**
* Recursive iterator type flag
*
* @access private
* @var bool
*/
private $set_recursive = true;
/**
* Match case flag
*
* @access private
* @var bool
*/
private $set_case = false;
/**
* Match whole word flag
*
* @access private
* @var bool
*/
private $set_whole_word = false;
/**
* Count of modified files
*
* @access private
* @var mixed
*/
private $mod_cnt = 0;
/**
* Use regular expression flag
*
* @access private
* @var bool
*/
private $set_regex;
/**
* String to be searched - needle
*
* @access private
* @var mixed
*/
private $search_needle;
/**
* Replacement string
*
* @access private
* @var mixed
*/
private $search_replacement;
/**********************************************
* Constructor
*********************************************/
/**
* Constructor
*
* @access public
* @param str $dir
* @param mixed $needle
* @param mixed $replacement
*/
public function __construct( $dir, $needle, $replacement )
{
$this->dir_path = $dir;
$this->search_needle = trim( $needle );
$this->search_replacement = trim( $replacement );
}
/**********************************************
* Private methods
*********************************************/
/**
* Verify occurance of needle in string using regex or strpos
*
* @access private
* @param str $string
* @return mixed
*/
private function count_needle( $string )
{
$needle_cnt = false;
if ( $this->set_regex )
{
$needle_cnt = $this->count_needle_regex( $string );
}
elseif ( $this->set_whole_word )
{
$needle_cnt = $this->count_needle_whole_word( $string );
}
else
{
$needle_cnt = $this->count_needle_default( $string );
}
return $needle_cnt;
}
/**
* Verify occurance of needle in string
*
* @access private
* @param str $string
* @return mixed
*/
private function count_needle_default( $string )
{
if ( $this->set_case )
{
$needle_cnt = strpos( $string, $this->search_needle );
}
else
{
$needle_cnt = stripos( $string, $this->search_needle );
}
return ( $needle_cnt !== false ) ? true : false;
}
/**
* Verify occurance of needle in string using regex
*
* @access private
* @param str $string
* @return mixed
*/
private function count_needle_regex( $string )
{
$expression = ( $this->set_case ) ? "`{$this->search_needle}`uU" : "`{$this->search_needle}`iuU";
return ( preg_match( $expression, $string ) ) ? true : false;
}
/**
* Verify occurance of whole word in string
*
* @access private
* @param str $string
* @return mixed
*/
private function count_needle_whole_word( $string )
{
$needle = preg_quote( $this->search_needle );
$expression = ( $this->set_case ) ? "`\b$needle\b`uU" : "`\b$needle\b`iuU";
return ( preg_match( $expression, $string ) ) ? true : false;
}
/**
* Determine that file is text/* mime type
*
* @access private
* @param mixed $file
* @param str $ext
* @return bool
*/
private function get_mime_type( $file )
{
$return_value = false;
//determine if file is text/* mime type - fall back to the deprecated mime_content_type if fileinfo extension not enabled
if ( function_exists( 'finfo_open' ) )
{
$return_value = ( substr_count( finfo_file( finfo_open( FILEINFO_MIME_TYPE ), $file ), 'text/' ) ) ? true : false;
}
else
{
$return_value = ( substr_count( mime_content_type( $file ), 'text/' ) ) ? true : false;
}
return ( $this->set_mime ) ? true : $return_value;
}
/**
* Iterates through directory and saves file if it meets search/replace arguments
*
* @access private
* @return null
*/
private function iterator()
{
$dir = new DirectoryIterator( $this->dir_path );
while ( $dir->valid() )
{
if ( $this->validate_file( $dir ) )
{
$this->write_to_file( $dir->getPathname(), $this->string_replace( file_get_contents( $dir->getPathname() ), $dir->key() ) );
}
$dir->next();
}
}
/**
* Recursively iterates through directory and saves file if it meets search/replace arguments
*
* @access private
* @return null
*/
private function recursive_iterator()
{
$dir = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $this->dir_path ) );
while ( $dir->valid() )
{
if ( $this->validate_file( $dir ) )
{
$this->write_to_file( $dir->key(), $this->string_replace( file_get_contents( $dir->key() ), $dir->key() ) );
}
$dir->next();
}
}
/**
* Performs string replacement functions
*
* @access private
* @param str $string
* @param str $path
* @return mixed
*/
private function string_replace( $string, $path )
{
$return_value = false;
if ( $this->count_needle( $string ) )
{
if ( $this->set_regex )
{
$replace = $this->string_replace_regex( $string );
}
elseif ( $this->set_whole_word )
{
$replace = $this->string_replace_whole_word( $string );
}
else
{
$replace = $this->string_replace_default( $string );
}
if ( $replace !== false )
{
$this->mod_cnt++;
$this->file_array[] = $path . ' - ' . $replace[1];
$return_value = $replace[0];
}
}
return $return_value;
}
/**
* Default string replacement
*
* @access private
* @param str $string
* @return mixed
*/
private function string_replace_default( $string )
{
$count = 0;
if ( $this->set_case )
{
$replace = str_replace( $this->search_needle, $this->search_replacement, $string, $count );
}
else
{
$replace = str_ireplace( $this->search_needle, $this->search_replacement, $string, $count );
}
return ( is_string( $replace ) ) ? array( $replace, $count ) : false;
}
/**
* Regex string replacement
*
* @access private
* @param str $string
* @return mixed
*/
private function string_replace_regex( $string )
{
$count = 0;
$expression = ( $this->set_case ) ? "`{$this->search_needle}`u" : "`{$this->search_needle}`iu";
$replace = preg_replace( $expression, $this->search_replacement, $string, -1, $count );
return ( $replace !== null ) ? array( $replace, $count ) : false;
}
/**
* Whole word string replacement
*
* @access private
* @param str $string
* @return mixed
*/
private function string_replace_whole_word( $string )
{
$count = 0;
$needle = preg_quote( $this->search_needle );
$expression = ( $this->set_case ) ? "`\b$needle\b`u" : "`\b$needle\b`iu";
$replace = preg_replace( $expression, $this->search_replacement, $string, -1, $count );
return ( $replace !== null ) ? array( $replace, $count ) : false;
}
/**
* Determine if file has valid extension, is text/* mime type and is readable/writable
*
* @access private
* @param mixed $dir
* @return bool
*/
private function validate_file( $dir )
{
$return_value = false;
$valid_ext = true;
//if PHP version < than 5.3.6, use pathinfo() to get extension
if ( version_compare( phpversion(), '5.3.6', '<' ) )
{
$path_parts = pathinfo( $dir->getPathname() );
$file_ext = $path_parts['extension'];
}
else
{
$file_ext = $dir->getExtension();
}
//determine if user defined extension was requested
if ( count( $this->set_extension ) )
{
$valid_ext = ( in_array( $file_ext, $this->set_extension ) ) ? true : false;
}
//determine if is file, is readable and writable, is text/* mime type and an inclusive extension
if ( !$dir->isDot() && $dir->isFile() && $dir->isReadable() && $dir->isWritable()&& $this->get_mime_type( $dir->getPathname() ) && $valid_ext )
{
$this->file_cnt++;
$return_value = true;
}
return $return_value;
}
/**
* Write modified string to file
*
* @access public
* @param str $path
* @param str $replace
* @return array
*/
private function write_to_file( $path, $replace )
{
if ( $replace !== false )
{
file_put_contents( $path, $replace );
}
}
/**********************************************
* Public methods
*********************************************/
/**
* Perform search & replace and return results
*
* @access public
* @return array
*/
public function get_results()
{
if ( $this->set_recursive )
{
$this->recursive_iterator();
}
else
{
$this->iterator();
}
//return number of files searched, modified, file path and number of replace instances
return array( $this->file_cnt, $this->mod_cnt, $this->file_array );
}
/**
* Set case-sensitive flag
*
* @access public
* @param bool $case
* @return bool
*/
public function set_case( $case=false )
{
$this->set_case = $case;
}
/**
* Set user defined inclusive extensions to search
*
* @access public
* @param str $extension
* @return array
*/
public function set_extension( $extension=null )
{
//setting this variable will overide $this->set_mime
$this->set_extension = array_filter( explode( ',', strtolower( $extension ) ) );
}
/**
* Set MIME type flag
*
* @access public
* @param bool $mime
* @return bool
*/
public function set_mime( $mime=false )
{
$this->set_mime = $mime;
}
/**
* Set search with regex flag
*
* @access public
* @param bool $regex
* @return bool
*/
public function set_regex( $regex=false )
{
$this->set_regex = $regex;
}
/**
* Set recursive iterator flag
*
* @access public
* @param bool $recursive
* @return bool
*/
public function set_recursive( $recursive=true )
{
$this->set_recursive = $recursive;
}
/**
* Set search whole word flag
*
* @access public
* @param bool $whole_word
* @return bool
*/
public function set_whole_word( $whole_word=false )
{
$this->set_whole_word = $whole_word;
}
}
/* EOF search_replace.php */
/* Location: ./search_replace.php */