1 | #!/usr/local/bin/php |
---|
2 | <?php |
---|
3 | /** |
---|
4 | * ¥Õ¥¡¥¤¥ë¤Î¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤ò $fromEncoding ¤«¤é $toEncoding ¤ØÊÑ´¹¤·¤Þ¤¹. |
---|
5 | * |
---|
6 | * @author Kentaro Ohkouchi<ohkouchi@loop-az.jp> |
---|
7 | * @since PHP4.3.0(cli) |
---|
8 | * @version $Id$ |
---|
9 | */ |
---|
10 | |
---|
11 | /** |
---|
12 | * ÊÑ´¹¤·¤¿¤¤¥Õ¥¡¥¤¥ë¤Î³ÈÄ¥»Ò¤ò¥«¥ó¥Þ¶èÀÚ¤ê¤ÇÍåÎó. |
---|
13 | */ |
---|
14 | $includes = "php,inc,tpl,css,sql,js"; |
---|
15 | |
---|
16 | /** |
---|
17 | * ½ü³°¤¹¤ë¥Õ¥¡¥¤¥ë̾¤ò¥«¥ó¥Þ¶èÀÚ¤ê¤ÇÍåÎó. |
---|
18 | */ |
---|
19 | $excludes = "convert.php"; |
---|
20 | |
---|
21 | /** |
---|
22 | * ÊÑ´¹¸µ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°. |
---|
23 | */ |
---|
24 | $fromEncoding = "EUC-JP"; |
---|
25 | |
---|
26 | /** |
---|
27 | * ÊÑ´¹À襨¥ó¥³¡¼¥Ç¥£¥ó¥°. |
---|
28 | */ |
---|
29 | $toEncoding = "UTF-8"; |
---|
30 | |
---|
31 | $includeArray = explode(',', $includes); |
---|
32 | $excludeArray = explode(',', $excludes); |
---|
33 | $fileArrays = listdirs('.'); |
---|
34 | |
---|
35 | foreach ($fileArrays as $path) { |
---|
36 | if (is_file($path)) { |
---|
37 | |
---|
38 | // ¥Õ¥¡¥¤¥ë̾¤ò¼èÆÀ |
---|
39 | $fileName = pathinfo($path, PATHINFO_BASENAME); |
---|
40 | |
---|
41 | // ³ÈÄ¥»Ò¤ò¼èÆÀ |
---|
42 | $suffix = pathinfo($path, PATHINFO_EXTENSION); |
---|
43 | |
---|
44 | // ½ü³°¥Õ¥¡¥¤¥ë¤ò¥¹¥¥Ã¥× |
---|
45 | if (in_array($fileName, $excludeArray)) { |
---|
46 | echo "excludes by " . $path . "\n"; |
---|
47 | continue; |
---|
48 | } |
---|
49 | |
---|
50 | // ÊÑ´¹Âоݤò½ç¤Ë½èÍý |
---|
51 | foreach ($includeArray as $include) { |
---|
52 | if ($suffix == $include) { |
---|
53 | |
---|
54 | // ¥Õ¥¡¥¤¥ëÆâÍÆ¤ò¼èÆÀ¤·, ¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°ÊÑ´¹ |
---|
55 | $contents = file_get_contents($path); |
---|
56 | $convertedContents = mb_convert_encoding($contents, |
---|
57 | $toEncoding, |
---|
58 | $fromEncoding); |
---|
59 | |
---|
60 | // ½ñ¤¹þ¤ß¤Ç¤¤ë¤«¡© |
---|
61 | if (is_writable($path)) { |
---|
62 | |
---|
63 | // ¥Õ¥¡¥¤¥ë¤ò½ñ¤½Ð¤·¥â¡¼¥É¤Ç³«¤¯ |
---|
64 | $handle = fopen($path, "w"); |
---|
65 | if (!$handle) { |
---|
66 | echo "Cannot open file (". $path . ")"; |
---|
67 | continue; |
---|
68 | } |
---|
69 | |
---|
70 | // ¥³¡¼¥ÉÊÑ´¹¤·¤¿ÆâÍÆ¤ò½ñ¤¹þ¤à |
---|
71 | if (fwrite($handle, $convertedContents) === false) { |
---|
72 | echo "Cannot write to file (" . $path . ")"; |
---|
73 | continue; |
---|
74 | } |
---|
75 | |
---|
76 | echo "converted " . $path . "\n"; |
---|
77 | // ¥Õ¥¡¥¤¥ë¤òÊĤ¸¤ë |
---|
78 | fclose($handle); |
---|
79 | } else { |
---|
80 | |
---|
81 | echo "The file " . $filename . "is not writable"; |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * $dir ¤òºÆµ¢Åª¤Ëé¤Ã¤Æ¥Ñ¥¹Ì¾¤òÇÛÎó¤ÇÊÖ¤¹. |
---|
90 | * |
---|
91 | * @param string Ǥ°Õ¤Î¥Ñ¥¹Ì¾ |
---|
92 | * @return array $dir ¤è¤ê²¼Áؤ˸ºß¤¹¤ë¥Ñ¥¹Ì¾¤ÎÇÛÎó |
---|
93 | * @see http://www.php.net/glob |
---|
94 | */ |
---|
95 | function listdirs($dir) { |
---|
96 | static $alldirs = array(); |
---|
97 | $dirs = glob($dir . '/*'); |
---|
98 | if (count($dirs) > 0) { |
---|
99 | foreach ($dirs as $d) $alldirs[] = $d; |
---|
100 | } |
---|
101 | foreach ($dirs as $dir) listdirs($dir); |
---|
102 | return $alldirs; |
---|
103 | } |
---|
104 | ?> |
---|