source: branches/comu/convert.php @ 12040

Revision 12040, 2.7 KB checked in by nanasess, 17 years ago (diff)

除外ファイル名を指定できるよう変更.
変換ファイル拡張子の指定方法を変更.
コメントを追加し, リファクタリング.

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