forked from nbuy/xoops-modules-eguide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
duplicate.php
142 lines (127 loc) · 3.2 KB
/
duplicate.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
#!/usr/bin/php -q
<?php
// only command line execute this script
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
exit;
}
define( 'ORIGIN_NAME', 'eguide' );
$prog = array_shift( $argv );
if ( count( $argv ) < 1 ) {
echo "usage: $prog [-v] dirname ...\n";
echo " dirname: /^[a-zA-Z0-9_]+\$/\n";
exit;
}
# for duplicatable (not D3, old style)
$mydirpath = __DIR__;
$myprefix = $mydirname = basename( $mydirpath );
chdir( $mydirpath );
// make target file lists
// force writable permission files
$writable = array();
// rewrite prefix files
$modifies = array();
foreach ( find_file( 'templates', ORIGIN_NAME . '_*.(html|xml)' ) as $sfile ) {
$modifies[ $sfile ] = preg_replace( '/\/' . preg_quote( ORIGIN_NAME ) . '_/', '/{prefix}_', $sfile );
}
$modifies['sql/mysql.sql'] = 'sql/mysql_{prefix}.sql';
function duplicate( $dir, $writable, $files, $verb = false ) {
$base = '../' . $dir;
$id = preg_replace( '/^\D+/', '', $dir );
if ( ! empty( $id ) ) {
$id = (int) $id;
}
if ( ! is_dir( $base ) ) {
mkdir( $base );
}
$base .= '/';
foreach ( find_file() as $name ) {
if ( $verb ) {
echo " dup: $name\n";
}
if ( is_dir( $name ) ) {
if ( ! @mkdir( $base . $name ) ) {
echo "mkdir error: $name\n";
}
} elseif ( ! @link( $name, $base . $name ) ) {
echo "link error: $name\n";
}
}
foreach ( $writable as $i ) {
$f = $base . $i;
if ( $verb ) {
echo " writable: $f\n";
}
if ( is_dir( $f ) ) {
chmod( $f, 0777 );
} else {
chmod( $f, 0666 );
}
}
foreach ( $files as $file => $dest ) {
$newfile = $base . preg_replace( '/{prefix}/', $dir, $dest );
$body = file_get_contents( $file );
if ( preg_match( '/\.sql$/', $file ) ) {
if ( $verb ) {
echo " $file -> $newfile\n";
}
$body = preg_replace( '/\\s' . preg_quote( ORIGIN_NAME ) . '([\s_])/', " $dir$1", $body );
} elseif ( preg_match( '/\.html$/', $file ) ) {
$body = preg_replace( '/' . preg_quote( ORIGIN_NAME ) . '_/', $dir . "_", $body );
if ( $verb ) {
echo " $file -> $newfile\n";
}
}
file_put_contents( $newfile, $body );
}
}
function find_file( $dir = '.', $pat = '' ) {
$files = array();
$dh = opendir( $dir );
$reg = preg_replace( array( '/\./', '/\*/' ), array( '\.', '.*' ), $pat );
while ( $file = readdir( $dh ) ) {
if ( $file == '.' || $file == '..' ) {
continue;
}
if ( preg_match( '/~$/', $file ) ) {
continue;
}
$path = preg_replace( '/\.\//', '', "$dir/$file" );
if ( is_dir( $path ) ) {
if ( $file == "CVS" ) {
continue;
}
if ( ! $pat ) {
$files[] = $path;
}
$files = array_merge( $files, find_file( $path, $pat ) );
} else {
if ( $pat && ! preg_match( "/^$reg$/", $file ) ) {
continue;
}
$files[] = $path;
}
}
closedir( $dh );
return $files;
}
if ( ! function_exists( 'file_put_contents' ) ) {
// php 4.3.11 no have?
function file_put_contents( $file, $text ) {
$fp = fopen( $file, "w" );
fwrite( $fp, $text );
fclose( $fp );
}
}
$verb = false;
if ( $argv[0] == '-v' ) {
array_shift( $argv );
$verb = true;
}
foreach ( $argv as $dir ) {
if ( preg_match( '/^[a-zA-Z0-9_]+$/', $dir ) ) {
echo "Duplicate: $dir\n";
duplicate( $dir, $writable, $modifies, $verb );
} else {
echo "Error dirname: $dir\n";
}
}