source: branches/feature-module-update/data/class/batch/SC_Batch_Update.php @ 16786

Revision 16786, 5.4 KB checked in by satou, 16 years ago (diff)

#184
修正しました。

  • Property svn:eol-style set to LF
  • Property svn:keywords set to "Id Revision Date"
RevLine 
[15665]1<?php
2/*
[16582]3 * This file is part of EC-CUBE
4 *
[15665]5 * Copyright(c) 2000-2007 LOCKON CO.,LTD. All Rights Reserved.
6 *
7 * http://www.lockon.co.jp/
[16582]8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
[15665]22 */
23
24// {{{ requires
[15895]25require_once(CLASS_PATH . "batch/SC_Batch.php");
[15665]26
27/**
28 * アップデート機能 のバッチクラス.
29 *
30 * XXX Singleton にするべき...
31 *
32 * @package Batch
33 * @author LOCKON CO.,LTD.
34 * @version $Id$
35 */
36class SC_Batch_Update extends SC_Batch {
37
38    /**
39     * 変換したいファイルの拡張子をカンマ区切りで羅列.
40     */
41    var $includes = "php,inc,tpl,css,sql,js";
42
43    /**
44     * 除外するファイル名をカンマ区切りで羅列.
45     */
[16343]46    var $excludes = "distinfo.php,update.php";
[15665]47
48    /**
49     * バッチ処理を実行する.
50     *
51     * @param string $target アップデータファイルのディレクトリパス
52     * @return void
53     */
54    function execute($target = ".") {
[16420]55        $arrLog = array();
[15665]56        $includeArray = explode(',', $this->includes);
57        $excludeArray = explode(',', $this->excludes);
[16343]58        $fileArrays = $this->listdirs($target);
[15665]59
60        foreach ($fileArrays as $path) {
61            if (is_file($path)) {
62
63                // ファイル名を取得
64                $fileName = pathinfo($path, PATHINFO_BASENAME);
65
66                // 拡張子を取得
67                $suffix = pathinfo($path, PATHINFO_EXTENSION);
68
69                // distinfo の変数定義
70                $distinfo = isset($distinfo) ? $distinfo : "";
71
72                // distinfo.php を読み込む
73                if ($fileName == "distinfo.php") {
[16420]74                    include_once($path);
[15665]75                }
76
77                // 除外ファイルをスキップ
78                if (in_array($fileName, $excludeArray)) {
[16420]79                    $arrLog[] = "excludes by " . $path . "\n";
[15665]80                    continue;
81                }
82
83                // sha1 を取得
84                $sha1 = sha1_file($path);
85
[16420]86                $arrLog[] = $sha1 . " => " . $path . "\n";
[15665]87
88
89                // 変換対象を順に処理
90                foreach ($includeArray as $include) {
91                    if ($suffix == $include) {
92
93                        // ファイル内容を取得
94                        $contents = file_get_contents($path);
95
96                        // 書き出し先を取得
97                        if (!empty($distinfo[$sha1])) {
98                            $out = $distinfo[$sha1];
99                        } else {
[16420]100                            $arrLog[] = "ハッシュ値が一致しないため, コピー先が取得できません.";
101                            die();
[15665]102                        }
103
104                        // ファイルを書き出しモードで開く
[16420]105                        $handle = @fopen($out, "w");
[15665]106                        if (!$handle) {
[16420]107                            // ディレクトリ作成を行ってリトライ
108                            $this->mkdir_p($out);
109                            $handle = @fopen($out, "w");
110                            if (!$handle) {
111                                $arrLog[] = "Cannot open file (". $out . ")\n";
112                                continue;
113                            }
[15665]114                        }
115
116                        // 取得した内容を書き込む
117                        if (fwrite($handle, $contents) === false) {
[16420]118                            $arrLog[] = "Cannot write to file (" . $out . ")\n";
[15665]119                            continue;
120                        }
121
[16420]122                        $arrLog[] =  "copyed " . $out . "\n";
[15665]123                        // ファイルを閉じる
124                        fclose($handle);
125                    }
126                }
127            }
128        }
[16420]129        $arrLog[] = "Finished Successful!\n";
130        return $arrLog;
[15665]131    }
132
133    /**
134     * $dir を再帰的に辿ってパス名を配列で返す.
135     *
136     * @param string 任意のパス名
137     * @return array $dir より下層に存在するパス名の配列
138     * @see http://www.php.net/glob
139     */
140    function listdirs($dir) {
141        static $alldirs = array();
142        $dirs = glob($dir . '/*');
[16786]143        if (is_array($dirs) && count($dirs) > 0) {
[15665]144            foreach ($dirs as $d) $alldirs[] = $d;
145        }
[16786]146        if (is_array($dirs)) {
147            foreach ($dirs as $dir) $this->listdirs($dir);
148        }
[15665]149        return $alldirs;
150    }
[16420]151
152    /**
153     * mkdir -p
154     *
155     * @param string $path 絶対パス
156     */
157    function mkdir_p($path){
158        $path = dirname($path);
159        $path = str_replace ('\\', '/', $path);
160
161        $arrDirs = explode("/", $path);
162        $dir = '';
163
164        foreach($arrDirs as $n){
165            $dir .= $n . '/';
166            if(!file_exists($dir)) {
167                if (!@mkdir($dir)) return;
168            }
169        }
[15665]170}
[16420]171}
[15665]172?>
Note: See TracBrowser for help on using the repository browser.