Added missing exclusion flags
Put them into class
This commit is contained in:
parent
541b71e9ac
commit
ecf187ffb6
@ -12,122 +12,19 @@ if ( !$argv[1] )
|
||||
-s <str ...> Strings that will replace the matches
|
||||
|
||||
Optional:
|
||||
*-e <patterns> Exclude defined pattern
|
||||
*-l <File> Output log file
|
||||
-e <patterns> Exclude defined pattern
|
||||
-l <File> Output log file
|
||||
-t Test without modifying anything
|
||||
|
||||
*Not available yet.
|
||||
|
||||
" ) ;
|
||||
|
||||
//------- End Usage -----------
|
||||
|
||||
|
||||
|
||||
|
||||
//Define globals
|
||||
$test = false;
|
||||
$recursive = false;
|
||||
$searchDirs = array();
|
||||
$patterns = array();
|
||||
$replaceStrs = array();
|
||||
|
||||
//------- Main Functions --------
|
||||
function getDirName ( $dir ) {
|
||||
return is_dir ( $dir ) ? $dir : dirname ( $dir ) ;
|
||||
}
|
||||
|
||||
function getFileList ( $dir ) {
|
||||
$fileList = array();
|
||||
$iterator = new DirectoryIterator ( getDirName ( $dir ) ) ;
|
||||
foreach ( $iterator as $fileinfo ) {
|
||||
if ( $fileinfo->isFile() ) {
|
||||
$fileList[] = $dir."/".$fileinfo->getFilename();
|
||||
}
|
||||
}
|
||||
return $fileList;
|
||||
}
|
||||
|
||||
function getDirList ( $dir ) {
|
||||
$dirList = array();
|
||||
$iterator = new DirectoryIterator ( getDirName ( $dir ) ) ;
|
||||
foreach ( $iterator as $fileinfo ) {
|
||||
if ( $fileinfo->isDir() && !$fileinfo->isDot()) {
|
||||
$dirList[] = $fileinfo->getFilename();
|
||||
}
|
||||
}
|
||||
|
||||
return $dirList;
|
||||
}
|
||||
|
||||
function getWholeDirList ( $dir ) {
|
||||
$wholeDirList = array();
|
||||
//getWholeDirList will stop untill getDirList return an empty array.
|
||||
foreach ( getDirList ( $dir ) as $file ) {
|
||||
//Store found path
|
||||
$wholeDirList[] = "$dir/$file";
|
||||
//Method getWholeDirList will self-iterate if sub-directories exist.
|
||||
$wholeDirList = array_merge ( $wholeDirList, getWholeDirList ( "$dir/$file" ) ) ;
|
||||
}
|
||||
return $wholeDirList;
|
||||
}
|
||||
|
||||
function getWholeFileList ( $dir ) {
|
||||
//Get the first level list.
|
||||
$wholeFileList = getFileList( $dir );
|
||||
//getWholeDirList and search its files.
|
||||
foreach ( getWholeDirList ( $dir ) as $dir ) {
|
||||
$wholeFileList = array_merge ( $wholeFileList, getFileList ( $dir ) ) ;
|
||||
}
|
||||
return $wholeFileList;
|
||||
}
|
||||
|
||||
function validateOptions ( ) {
|
||||
global $searchDirs, $patterns, $replaceStrs;
|
||||
|
||||
try {
|
||||
if(count($searchDirs) == 0)
|
||||
throw new Exception("Search directory is missing");
|
||||
else if(count($patterns) == 0)
|
||||
throw new Exception("Search pattern is missing");
|
||||
else if(count($replaceStrs) == 0)
|
||||
throw new Exception("Replacement string is missing");
|
||||
else if(count($replaceStrs) != count($patterns))
|
||||
throw new Exception("Replacement string and pattern count not match");
|
||||
} catch(Exception $e) {
|
||||
var_dump( $GLOBALS );
|
||||
die("Error: $e.\n");
|
||||
}
|
||||
}
|
||||
|
||||
function setOptions( $item, $value = NULL ) {
|
||||
global $test, $recursive, $searchDirs, $patterns, $replaceStrs;
|
||||
switch($item) {
|
||||
case "-d":
|
||||
if($value)
|
||||
$searchDirs[] = $value;
|
||||
break;
|
||||
case "-p":
|
||||
if($value)
|
||||
$patterns[] = $value;
|
||||
break;
|
||||
case "-r":
|
||||
$recursive = true;
|
||||
break;
|
||||
case "-s":
|
||||
if($value)
|
||||
$replaceStrs[] = $value;
|
||||
break;
|
||||
case "-t":
|
||||
$test = true;
|
||||
break;
|
||||
default:
|
||||
//Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
//parseOptions utilitiese by tgckpg
|
||||
function parseOptions ( $argStream, $handler ) {
|
||||
class ArgvParser
|
||||
{
|
||||
//parseOptions utilitiese by tgckpg
|
||||
static function PARSE ( $argStream, $handler ) {
|
||||
//Chop first useless argument -- argv[0]
|
||||
array_shift ( $argStream ) ;
|
||||
//Initiate ArrayObject for iterator
|
||||
@ -164,25 +61,36 @@ function parseOptions ( $argStream, $handler ) {
|
||||
} else {
|
||||
//Try other approach.
|
||||
}// End if
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//------- End Main Functions --------
|
||||
class RegRename
|
||||
{
|
||||
|
||||
//Define globals
|
||||
private $test = false;
|
||||
private $recursive = false;
|
||||
private $searchDirs = array();
|
||||
private $patterns = array();
|
||||
private $replaceStrs = array();
|
||||
private $excludes = array();
|
||||
|
||||
parseOptions ( $argv, 'setOptions' ) ;
|
||||
validateOptions();
|
||||
private $logfile = NULL;
|
||||
|
||||
public function begin()
|
||||
{
|
||||
$this->validateOptions();
|
||||
|
||||
//------- Begin Operations -------
|
||||
foreach ( $searchDirs as $dir ) {
|
||||
echo "Directory: $dir\nBuilding file list ...\n";
|
||||
if ( $recursive ) {
|
||||
$fileList = getWholeFileList($dir);
|
||||
//------- Begin Operations -------
|
||||
foreach ( $this->searchDirs as $dir ) {
|
||||
$this->elog("Directory: $dir\nBuilding file list ...");
|
||||
if ( $this->recursive ) {
|
||||
$fileList = $this->getWholeFileList($dir);
|
||||
} else {
|
||||
$fileList = getFileList($dir);
|
||||
$fileList = $this->getFileList($dir);
|
||||
}
|
||||
echo "Searching ...\n";
|
||||
$this->elog("Searching ...");
|
||||
foreach ( $fileList as $file ) {
|
||||
//
|
||||
// Get the file name
|
||||
@ -198,29 +106,154 @@ foreach ( $searchDirs as $dir ) {
|
||||
//
|
||||
preg_match_all('/^(.+?)([^\/\\\\]+)$/', $file, $matches );
|
||||
$newName = $matches[2][0];
|
||||
foreach ( $patterns as $key => $pattern ) {
|
||||
foreach ( $this->patterns as $key => $pattern ) {
|
||||
//Rename over the patterns
|
||||
$newName = preg_replace( $pattern, $replaceStrs[$key], $newName );
|
||||
$newName = preg_replace( $pattern, $this->replaceStrs[$key], $newName );
|
||||
}
|
||||
|
||||
if( $matches[2][0] != $newName ) {
|
||||
if( $test ) {
|
||||
echo "File \"{$matches[2][0]}\" will be renamed to \"$newName\"\n";
|
||||
|
||||
// Excluede any matches
|
||||
foreach ( $this->excludes as $key => $pattern ) {
|
||||
if(preg_match_all($pattern, $file)) {
|
||||
$this->elog("S File \"{$matches[2][0]}\" excluded on $pattern");
|
||||
continue 2;
|
||||
}
|
||||
}
|
||||
|
||||
if( $this->test ) {
|
||||
$this->elog("R File \"{$matches[2][0]}\" will be renamed to \"$newName\"");
|
||||
} else {
|
||||
rename ( $file, "{$matches[1][0]}$newName" );
|
||||
}
|
||||
if ( $logfile ) {
|
||||
|
||||
if ( $this->logfile ) {
|
||||
file_put_contents($this->logfile, $this->log);
|
||||
}
|
||||
}/* else {
|
||||
echo "File \"{$matches[0][0]}\" will not be renamed\n";
|
||||
}
|
||||
//*/
|
||||
}
|
||||
echo "\n";
|
||||
$this->elog();
|
||||
}
|
||||
if ( $this->test ) $this->elog("*** Test enabled ***\n");
|
||||
//------- End Operations -------*/
|
||||
}
|
||||
|
||||
public function setOption( $item, $value = NULL ) {
|
||||
switch($item) {
|
||||
case "-d":
|
||||
if($value)
|
||||
$this->searchDirs[] = $value;
|
||||
break;
|
||||
case "-p":
|
||||
if($value)
|
||||
$this->patterns[] = $value;
|
||||
break;
|
||||
case "-r":
|
||||
$this->recursive = true;
|
||||
break;
|
||||
case "-s":
|
||||
if($value)
|
||||
$this->replaceStrs[] = $value;
|
||||
break;
|
||||
case "-t":
|
||||
$this->test = true;
|
||||
break;
|
||||
case "-e":
|
||||
$this->excludes[] = $value;
|
||||
break;
|
||||
case "-l":
|
||||
$this->logfile = $value;
|
||||
break;
|
||||
default:
|
||||
//Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
//------- Main Functions --------
|
||||
|
||||
function elog( $line = "" ) {
|
||||
$this->log .= $line;
|
||||
$this->log .= "\n";
|
||||
echo "$line\n";
|
||||
}
|
||||
|
||||
|
||||
private function getDirName ( $dir ) {
|
||||
return is_dir ( $dir ) ? $dir : dirname ( $dir ) ;
|
||||
}
|
||||
|
||||
private function getFileList ( $dir ) {
|
||||
$fileList = array();
|
||||
$iterator = new DirectoryIterator ( $this->getDirName ( $dir ) ) ;
|
||||
foreach ( $iterator as $fileinfo ) {
|
||||
if ( $fileinfo->isFile() ) {
|
||||
$fileList[] = $dir."/".$fileinfo->getFilename();
|
||||
}
|
||||
}
|
||||
return $fileList;
|
||||
}
|
||||
|
||||
private function getDirList ( $dir ) {
|
||||
$dirList = array();
|
||||
$iterator = new DirectoryIterator ( $this->getDirName ( $dir ) ) ;
|
||||
foreach ( $iterator as $fileinfo ) {
|
||||
if ( $fileinfo->isDir() && !$fileinfo->isDot()) {
|
||||
$dirList[] = $fileinfo->getFilename();
|
||||
}
|
||||
}
|
||||
|
||||
return $dirList;
|
||||
}
|
||||
|
||||
private function getWholeDirList ( $dir ) {
|
||||
$wholeDirList = array();
|
||||
//getWholeDirList will stop untill getDirList return an empty array.
|
||||
foreach ( $this->getDirList ( $dir ) as $file ) {
|
||||
//Store found path
|
||||
$wholeDirList[] = "$dir/$file";
|
||||
//Method getWholeDirList will self-iterate if sub-directories exist.
|
||||
$wholeDirList = array_merge ( $wholeDirList, $this->getWholeDirList ( "$dir/$file" ) ) ;
|
||||
}
|
||||
return $wholeDirList;
|
||||
}
|
||||
|
||||
private function getWholeFileList ( $dir ) {
|
||||
//Get the first level list.
|
||||
$wholeFileList = $this->getFileList( $dir );
|
||||
//getWholeDirList and search its files.
|
||||
foreach ( $this->getWholeDirList ( $dir ) as $dir ) {
|
||||
$wholeFileList = array_merge ( $wholeFileList, $this->getFileList ( $dir ) ) ;
|
||||
}
|
||||
return $wholeFileList;
|
||||
}
|
||||
|
||||
private function validateOptions ( ) {
|
||||
try {
|
||||
if(count($this->searchDirs) == 0)
|
||||
throw new Exception("Search directory is missing");
|
||||
else if(count($this->patterns) == 0)
|
||||
throw new Exception("Search pattern is missing");
|
||||
else if(count($this->replaceStrs) == 0)
|
||||
throw new Exception("Replacement string is missing");
|
||||
else if(count($this->replaceStrs) != count($this->patterns))
|
||||
throw new Exception("Replacement string and pattern count not match");
|
||||
} catch(Exception $e) {
|
||||
var_dump( $GLOBALS );
|
||||
die("Error: $e.\n");
|
||||
}
|
||||
}
|
||||
|
||||
//------- End Main Functions --------
|
||||
}
|
||||
if ( $test ) echo "*** Test enabled ***\n\n";
|
||||
|
||||
|
||||
|
||||
//------- End Operations -------*/
|
||||
|
||||
|
||||
$rrgr = new RegRename();
|
||||
ArgvParser::PARSE ( $argv, array($rrgr, 'setOption') ) ;
|
||||
$rrgr->begin();
|
||||
|
||||
?>
|
||||
|
Loading…
Reference in New Issue
Block a user