Ignore:
Timestamp:
2011/05/19 23:45:07 (13 years ago)
Author:
Seasoft
Message:

#144 (郵便番号DB のアップデート)
#1317 (郵便番号DB登録 [自動登録]と[削除]のタイミングで、KEN_ALL_utf-8.CSV も削除)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/version-2_11-dev/data/class/pages/admin/basis/LC_Page_Admin_Basis_ZipInstall.php

    r20911 r20945  
    5151    var $exec; 
    5252    var $tpl_count_mtb_zip; 
     53 
     54    /** CSV の更新日時 */ 
     55    var $tpl_csv_datetime; 
     56 
     57    /** ZIP アーカイブファイルの取得元 */ 
     58    var $zip_download_url = 'http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip'; 
     59 
     60    /** 日本郵便から取得した ZIP アーカイブファイルの保管パス */ 
     61    var $zip_csv_temp_realfile; 
    5362 
    5463    // }}} 
     
    7079        $this->tpl_mode = $this->getMode(); 
    7180        $this->exec = (boolean)$_GET['exec']; 
     81        $this->zip_csv_temp_realfile = DATA_REALDIR . 'downloads/tmp/ken_all.zip'; 
    7282    } 
    7383 
     
    105115                    $this->lfAutoCommitZip(); 
    106116                    break; 
    107                 // 手動登録 
     117                // DB手動登録 
    108118                case 'manual': 
    109119                    $this->insertMtbZip($this->arrForm['startRowNum']); 
     
    114124 
    115125        switch ($this->tpl_mode) { 
    116             // 手動削除 
     126            // 削除 
    117127            case 'delete': 
    118128                $this->lfDeleteZip(); 
     
    120130                // 進捗・完了画面を表示しない 
    121131                $this->tpl_mode = null; 
    122  
     132                break; 
     133 
     134            // 郵便番号CSV更新 
     135            case 'update_csv'; 
     136                $this->lfDownloadZipFileFromJp(); 
     137                $this->lfExtractZipFile(); 
     138 
     139                // 進捗・完了画面を表示しない 
     140                $this->tpl_mode = null; 
    123141                break; 
    124142        } 
     
    126144        $this->tpl_line = $this->countZipCsv(); 
    127145        $this->tpl_count_mtb_zip = $this->countMtbZip(); 
     146        $this->tpl_csv_datetime = $this->lfGetCsvDatetime(); 
     147        // XXX PHP4 を切捨てたら、ダウンロードの必要性チェックなども行いたい 
     148        // $arrHeader = get_headers($this->zip_download_url, 1); 
    128149    } 
    129150 
     
    140161        $objQuery =& SC_Query_Ex::getSingletonInstance(); 
    141162 
     163        $this->lfDownloadZipFileFromJp(); 
     164        $this->lfExtractZipFile(); 
     165 
    142166        $objQuery->begin(); 
    143         $objQuery->delete('mtb_zip'); 
     167        $this->lfDeleteZip(); 
    144168        $this->insertMtbZip(); 
    145169        $objQuery->commit(); 
    146170    } 
    147171 
     172    /** 
     173     * テーブルデータと UTF-8 変換済みの郵便番号 CSV を削除 
     174     * 
     175     * @return void 
     176     */ 
    148177    function lfDeleteZip() { 
    149178        $objQuery =& SC_Query_Ex::getSingletonInstance(); 
    150179 
     180        // DB 
    151181        $objQuery->delete('mtb_zip'); 
     182 
     183        // UTF-8 変換済みの郵便番号 CSV 
     184        unlink(ZIP_CSV_UTF8_REALFILE); 
    152185    } 
    153186 
     
    179212        </head> 
    180213        <body> 
    181         <p>進捗状況</p> 
     214        <p>DB 登録進捗状況</p> 
    182215        <div style="background-color: #494E5F;"> 
    183216        <?php 
     
    271304 
    272305    function openZipCsv() { 
    273         // http://www.post.japanpost.jp/zipcode/dl/kogaki/lzh/ken_all.lzh 
    274306        $this->convertZipCsv(); 
    275307        $fp = fopen(ZIP_CSV_UTF8_REALFILE, 'r'); 
     
    325357        return $line; 
    326358    } 
     359 
     360    /** 
     361     * 日本郵便から郵便番号 CSV の ZIP アーカイブファイルを取得 
     362     * 
     363     * @return void 
     364     */ 
     365    function lfDownloadZipFileFromJp() { 
     366        $res = copy($this->zip_download_url, $this->zip_csv_temp_realfile); 
     367        if (!$res) { 
     368            SC_Utils_Ex::sfDispException($this->zip_download_url . ' の取得または ' . $this->zip_csv_temp_realfile . ' への書き込みに失敗しました。'); 
     369        } 
     370    } 
     371 
     372    /** 
     373     * ZIP アーカイブファイルを展開して、郵便番号 CSV を上書き 
     374     * 
     375     * @return void 
     376     */ 
     377    function lfExtractZipFile() { 
     378        if (!function_exists('zip_open')) { 
     379            SC_Utils_Ex::sfDispException('PHP 拡張モジュール「zip」を有効にしてください。'); 
     380        } 
     381 
     382        $zip = zip_open($this->zip_csv_temp_realfile); 
     383        if (!is_resource($zip)) { 
     384            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' をオープンできません。'); 
     385        } 
     386 
     387        do { 
     388            $entry = zip_read($zip); 
     389        } while ($entry && zip_entry_name($entry) != 'KEN_ALL.CSV'); 
     390 
     391        if (!$entry) { 
     392            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' に対象ファイルが見つかりません。'); 
     393        } 
     394 
     395        // 展開時の破損を考慮し、別名で一旦展開する。 
     396        $tmp_csv_realfile = ZIP_CSV_REALFILE . '.tmp'; 
     397 
     398        $res = zip_entry_open($zip, $entry, 'rb'); 
     399        if (!$res) { 
     400            SC_Utils_Ex::sfDispException($this->zip_csv_temp_realfile . ' の展開に失敗しました。'); 
     401        } 
     402 
     403        $fp = fopen($tmp_csv_realfile, 'w'); 
     404        if (!$fp) { 
     405            SC_Utils_Ex::sfDispException($tmp_csv_realfile . ' を開けません。'); 
     406        } 
     407 
     408        $res = fwrite($fp, zip_entry_read($entry, zip_entry_filesize($entry))); 
     409        if ($res === FALSE) { 
     410            SC_Utils_Ex::sfDispException($tmp_csv_realfile . ' の書き込みに失敗しました。'); 
     411        } 
     412 
     413        fclose($fp); 
     414        zip_close($zip); 
     415 
     416        // CSV 削除 
     417        $res = unlink(ZIP_CSV_REALFILE); 
     418        if (!$res) { 
     419            SC_Utils_Ex::sfDispException(ZIP_CSV_REALFILE . ' を削除できません。'); 
     420        } 
     421 
     422        // CSV ファイル名変更 
     423        $res = rename($tmp_csv_realfile, ZIP_CSV_REALFILE); 
     424        if (!$res) { 
     425            SC_Utils_Ex::sfDispException('ファイル名を変更できません。: ' . $tmp_csv_realfile . ' -> ' . ZIP_CSV_REALFILE); 
     426        } 
     427    } 
     428 
     429    /** 
     430     * CSV の更新日時を取得 
     431     * 
     432     * @return string CSV の更新日時 (整形済みテキスト) 
     433     */ 
     434    function lfGetCsvDatetime() { 
     435        return date('Y/m/d H:i:s', filemtime(ZIP_CSV_REALFILE)); 
     436    } 
    327437} 
    328438?> 
Note: See TracChangeset for help on using the changeset viewer.