source: branches/feature-module-update/convert.php @ 15081

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

svn priperties 変更

  • Property svn:executable set to *
  • Property svn:keywords set to Id
  • Property svn:mime-type set to application/x-httpd-php; charset=EUC-JP
Line 
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:convert.php 15079 2007-07-20 07:20:36Z nanasess $
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
35foreach ($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 */
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.