public function start($args) { // 주어진 옵션을 확인한다. $valid_options = false; if (count($args->args) === 2 && ctype_digit($args->args[0]) && ctype_digit($args->args[1]) && (strlen($args->args[1]) === 5 || strlen($args->args[1]) === 6)) { $address_id = $args->args[0]; $postcode = $args->args[1]; } else { echo 'Usage: php indexer.php set-postcode <address-id> <postcode>' . PHP_EOL; exit(1); } // DB 연결을 확인한다. Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); if (!($db = Postcodify_Utility::get_db())) { echo '[ERROR] MySQL DB에 접속할 수 없습니다.' . PHP_EOL; exit(1); } // 주어진 주소가 존재하는지 확인한다. $ps_select = $db->prepare('SELECT pa.*, pr.* FROM postcodify_addresses pa JOIN postcodify_roads pr ON pa.road_id = pr.road_id WHERE pa.id = ? ORDER BY id LIMIT 1'); $ps_select->execute(array($address_id)); $entry = $ps_select->fetchObject(); if (!$entry) { echo '[ERROR] "' . $address_id . '" 레코드를 찾을 수 없습니다.' . PHP_EOL; exit(1); } // 우편번호를 업데이트한다. $column_name = strlen($postcode) === 6 ? 'postcode6' : 'postcode5'; $ps_update = $db->prepare('UPDATE postcodify_addresses SET ' . $column_name . ' = ? WHERE id = ?'); $ps_update->execute(array($postcode, $entry->id)); $entry->{$column_name} = $postcode; // 변경 내역을 표시한다. echo ' #' . $entry->id . ' ' . str_pad($entry->postcode6, 6, ' ') . ' ' . str_pad($entry->postcode5, 5, ' ') . ' ' . $this->format_address($entry) . PHP_EOL; }
public function start($args) { Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); if (in_array('--no-old-postcodes', $args->options)) { $this->_no_old_postcodes = true; } if (!($db = Postcodify_Utility::get_db())) { echo '[ERROR] MySQL DB에 접속할 수 없습니다.' . PHP_EOL; exit(1); } $this->_schema = (include POSTCODIFY_LIB_DIR . '/resources/schema.php'); $pass = true; echo '테이블 확인 중...' . PHP_EOL; $pass = $this->check_tables($db) && $pass; echo '인덱스 확인 중...' . PHP_EOL; $pass = $this->check_indexes($db) && $pass; if ($pass) { echo '데이터 확인 중...' . PHP_EOL; $pass = $pass && $this->check_data_count($db); $pass = $pass && $this->check_missing_postcodes($db); } else { echo 'DB 스키마에 문제가 있으므로 데이터 확인은 시도하지 않습니다.' . PHP_EOL; } if ($pass) { echo 'DB에 문제가 없습니다.' . PHP_EOL; } else { echo 'DB에 문제가 있습니다.' . PHP_EOL; exit(1); } }
public function start($args) { Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); if (in_array('--add-old-postcodes', $args->options)) { $this->_add_old_postcodes = true; } // 어디까지 업데이트했는지 찾아본다. $db = Postcodify_Utility::get_db(); $updated_query = $db->query('SELECT v FROM postcodify_settings WHERE k = \'updated\''); $updated = $updated_query->fetchColumn(); $updated_query->closeCursor(); unset($updated_query); if (!preg_match('/^20[0-9]{6}$/', $updated)) { echo '[ERROR] 기존 DB의 데이터 기준일을 찾을 수 없습니다.' . PHP_EOL; exit(3); } // 범위 데이터를 사용할 수 있는지 확인한다. $tables_query = $db->query("SHOW TABLES LIKE 'postcodify_ranges_roads'"); if ($tables_query->fetchColumn() === false) { $this->_ranges_available = false; } unset($tables_query); unset($db); // 업데이트를 적용한다. $updated_new = $this->load_updates($updated); // 데이터 기준일 정보를 업데이트한다. $updated_new = strval(max(intval($updated), intval($updated_new))); if ($updated_new === strval($updated)) { echo '업데이트할 것이 없습니다.' . PHP_EOL; exit; } else { $db = Postcodify_Utility::get_db(); $updated_query = $db->prepare('UPDATE postcodify_settings SET v = ? WHERE k = \'updated\''); $updated_query->execute(array($updated_new)); unset($updated_query); unset($db); } }
public function start($args) { // SQLite 파일명을 구한다. if (count($args->args)) { $filename = $args->args[0]; if (@file_put_contents($filename, '') === false) { echo $filename . ' 파일을 생성할 수 없습니다. 경로와 퍼미션을 확인해 주십시오.' . PHP_EOL; exit(1); } } else { echo 'SQLite 파일명을 지정해 주십시오.' . PHP_EOL; exit(1); } // MySQL DB에 연결한다. if (!($mysql = Postcodify_Utility::get_db())) { echo '[ERROR] MySQL DB에 접속할 수 없습니다.' . PHP_EOL; exit(1); } // SQLite DB를 초기화한다. Postcodify_Utility::print_message('Postcodify SQLite Converter ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); Postcodify_Utility::print_message('SQLite DB를 초기화하는 중...'); try { $sqlite = $this->initialize_db($filename); } catch (PDOException $e) { echo '[ERROR] SQLite DB 초기화에 실패했습니다.' . PHP_EOL; echo $e->getMessage() . PHP_EOL; exit(1); } Postcodify_Utility::print_ok(); // 데이터를 복사한다. $this->copy_data($mysql, $sqlite); // 인덱스를 생성한다. $this->create_indexes($sqlite); // 인덱스를 최적화한다. Postcodify_Utility::print_message('인덱스 최적화 중...'); $this->wrap_up($sqlite); Postcodify_Utility::print_ok(); }
public function start($args) { Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); if (in_array('--no-old-postcodes', $args->options)) { $this->_no_old_postcodes = true; } $checkenv = new Postcodify_Indexer_CheckEnv(); $checkenv->check(); $this->create_tables(); $this->load_basic_info(); $this->load_road_list(); $this->load_english_aliases(); $this->load_new_ranges(); $this->load_old_ranges(); $this->start_threaded_workers('initial_indexes', '초기 인덱스를 생성하는 중...'); $this->start_threaded_workers('load_addresses', '건물정보 데이터를 로딩하는 중...'); $this->start_threaded_workers('interim_indexes', '중간 인덱스를 생성하는 중...'); $this->start_threaded_workers('load_jibeon', '관련지번 데이터를 로딩하는 중...'); $this->load_pobox(); $this->save_english_keywords(); $this->start_threaded_workers('final_indexes', '최종 인덱스를 생성하는 중...'); }
public function start() { // 다운로드할 경로가 존재하는지 확인한다. Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); $download_path = dirname(POSTCODIFY_LIB_DIR) . '/data'; $downloaded_files = 0; if ((!file_exists($download_path) || !is_dir($download_path)) && !@mkdir($download_path, 0755)) { echo '[ERROR] 다운로드 대상 경로(' . $download_path . ')가 존재하지 않습니다.' . PHP_EOL; exit(2); } // 게시물 목록을 다운로드한다. $html = Postcodify_Utility::download(self::RELATIVE_DOMAIN . self::LIST_URL); // 데이터가 존재하는 가장 최근 년월을 찾는다. $address_maxdate = null; $code_maxdate = null; preg_match_all(self::LIST_REGEXP1, $html, $address_links, PREG_SET_ORDER); foreach ($address_links as $address_link) { $address_maxdate = max($address_maxdate, intval(sprintf('%04d%02d', $address_link[1], $address_link[2]))); } preg_match_all(self::LIST_REGEXP2, $html, $code_links, PREG_SET_ORDER); foreach ($code_links as $code_link) { $code_maxdate = max($code_maxdate, intval(sprintf('%04d%02d', $code_link[1], $code_link[2]))); } $data_maxdate = min($address_maxdate, $code_maxdate); if (strlen($data_maxdate) !== 6) { echo '[ERROR] 다운로드할 데이터를 찾을 수 없습니다.' . PHP_EOL; exit(2); } $data_year = intval(substr($data_maxdate, 0, 4), 10); $data_month = intval(substr($data_maxdate, 4, 2), 10); $data_day = intval(date('t', mktime(12, 0, 0, $data_month, 1, $data_year))); // 데이터 기준일을 YYYYMMDD 포맷으로 정리한다. Postcodify_Utility::print_message('데이터 기준일은 ' . $data_year . '년 ' . $data_month . '월 ' . $data_day . '일입니다.'); // 주소 데이터를 다운로드한다. $download_url = self::RELATIVE_DOMAIN . sprintf(self::DOWNLOAD_URL, $data_year, $data_month, 'RDNMADR'); $filename = sprintf('%04d%02d%s.zip', $data_year, $data_month, 'RDNMADR'); $filepath = $download_path . '/' . $filename; Postcodify_Utility::print_message('다운로드: ' . $filename); $result = Postcodify_Utility::download($download_url, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 도로명코드 데이터를 다운로드한다. $download_url = self::RELATIVE_DOMAIN . sprintf(self::DOWNLOAD_URL, $data_year, $data_month, 'RDNMCODE'); $filename = sprintf('%04d%02d%s.zip', $data_year, $data_month, 'RDNMCODE'); $filepath = $download_path . '/' . $filename; Postcodify_Utility::print_message('다운로드: ' . $filename); $result = Postcodify_Utility::download($download_url, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 우체국 사서함 파일을 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::POBOX_URL)); $filepath = $download_path . '/' . basename(self::POBOX_URL); $result = Postcodify_Utility::download(self::POBOX_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 우편번호 범위 데이터를 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::RANGES_URL)); $filepath = $download_path . '/' . basename(self::RANGES_URL); $result = Postcodify_Utility::download(self::RANGES_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 영문 동 명칭을 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::ENGLISH_URL)); $filepath = $download_path . '/' . basename(self::ENGLISH_URL); $result = Postcodify_Utility::download(self::ENGLISH_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 구 우편번호 범위 데이터를 다운로드한다. Postcodify_Utility::print_message('다운로드: ' . basename(self::OLDADDR_ZIPCODE_URL)); $filepath = $download_path . '/' . basename(self::OLDADDR_ZIPCODE_URL); $result = Postcodify_Utility::download(self::OLDADDR_ZIPCODE_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } Postcodify_Utility::print_message('다운로드: ' . basename(self::OLDADDR_SPECIAL_URL)); $filepath = $download_path . '/' . basename(self::OLDADDR_SPECIAL_URL); $result = Postcodify_Utility::download(self::OLDADDR_SPECIAL_URL, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath) || filesize($filepath) < 1024) { Postcodify_Utility::print_error(); exit(2); } else { Postcodify_Utility::print_ok(filesize($filepath)); $downloaded_files++; } // 파일 수가 맞는지 확인한다. if ($downloaded_files < 7) { echo '[ERROR] 다운로드한 파일 수가 일치하지 않습니다.' . PHP_EOL; exit(2); } }
public function start() { // 다운로드할 경로가 존재하는지 확인한다. Postcodify_Utility::print_message('Postcodify Indexer ' . POSTCODIFY_VERSION); Postcodify_Utility::print_newline(); $download_path = dirname(POSTCODIFY_LIB_DIR) . '/data'; if ((!file_exists($download_path) || !is_dir($download_path)) && !@mkdir($download_path, 0755, true)) { echo '[ERROR] 다운로드 대상 경로(' . $download_path . ')가 존재하지 않습니다.' . PHP_EOL; exit(2); } // 어디까지 업데이트했는지 찾아본다. $db = Postcodify_Utility::get_db(); $updated_query = $db->query('SELECT v FROM postcodify_settings WHERE k = \'updated\''); $updated = $updated_query->fetchColumn(); $updated_query->closeCursor(); if (!preg_match('/^20[0-9]{6}$/', $updated)) { echo '[ERROR] 기존 DB의 데이터 기준일을 찾을 수 없습니다.' . PHP_EOL; exit(3); } $current_time = mktime(12, 0, 0, date('m'), date('d'), date('Y')); $updated_time = mktime(12, 0, 0, substr($updated, 4, 2), substr($updated, 6, 2), substr($updated, 0, 4)); if ($updated_time < $current_time - 86400 * 60) { echo '[ERROR] 마지막 업데이트로부터 60일 이상이 경과하였습니다. DB를 새로 생성하시기 바랍니다.' . PHP_EOL; exit(3); } if ($updated_time >= $current_time) { echo '업데이트가 필요하지 않습니다.' . PHP_EOL; exit(0); } // 다운로드할 업데이트 목록을 생성한다. $updates = array(); for ($time = $updated_time + 86400; $time < $current_time; $time += 86400) { $updates[] = date('Ymd', $time); } // 업데이트를 다운로드한다. foreach ($updates as $date) { $filepath = $download_path . '/' . $date . '_dailynoticedata.zip'; if (file_exists($filepath)) { Postcodify_Utility::print_message('파일이 이미 존재함: ' . $date . '_dailynoticedata.zip'); continue; } Postcodify_Utility::print_message('다운로드: ' . $date . '_dailynoticedata.zip'); $link = self::RELATIVE_DOMAIN . sprintf(self::DOWNLOAD_URL, $date); $result = Postcodify_Utility::download($link, $filepath, array(__CLASS__, 'progress')); if (!$result || !file_exists($filepath)) { Postcodify_Utility::print_error(); @unlink($filepath); continue; } if (filesize($filepath) < 512 && stripos(file_get_contents($filepath), 'not found') !== false) { Postcodify_Utility::print_error(); @unlink($filepath); continue; } $zip = new ZipArchive(); $result = $zip->open($filepath); if (!$result) { Postcodify_Utility::print_error(); @unlink($filepath); continue; } Postcodify_Utility::print_ok(filesize($filepath)); } }