diff --git a/generators/geo.php b/generators/geo.php index 1c46751..e040bd9 100755 --- a/generators/geo.php +++ b/generators/geo.php @@ -5,120 +5,120 @@ class GeoName{ public static $files = array( - 'countries.php' => array( - 'src' => 'http://download.geonames.org/export/dump/countryInfo.txt', - 'srcFile' => 'countryInfo.txt', - 'map' => '_mapCountry' - ), - 'big_cities.php' => array( - 'src' => 'http://download.geonames.org/export/dump/cities15000.zip', - 'srcFile' => 'cities15000.txt', - 'map' => '_mapCity' - ), - 'time_zones.php' => array( - 'src' => 'http://download.geonames.org/export/dump/timeZones.txt', - 'srcFile' => '.txt', - 'map' => '_mapTimeZone' - ), + 'countries.php' => array( + 'src' => 'http://download.geonames.org/export/dump/countryInfo.txt', + 'srcFile' => 'countryInfo.txt', + 'map' => '_mapCountry' + ), + 'big_cities.php' => array( + 'src' => 'http://download.geonames.org/export/dump/cities15000.zip', + 'srcFile' => 'cities15000.txt', + 'map' => '_mapCity' + ), + 'time_zones.php' => array( + 'src' => 'http://download.geonames.org/export/dump/timeZones.txt', + 'srcFile' => '.txt', + 'map' => '_mapTimeZone' + ), ); public static function generate($file) { - extract(GeoName::$files[$file]); + extract(GeoName::$files[$file]); - $srcFile = PhpArray::fetch($src, $srcFile); - echo sprintf("-> Parsing %s\n", basename($srcFile)); + $srcFile = PhpArray::fetch($src, $srcFile); + echo sprintf("-> Parsing %s\n", basename($srcFile)); - $items = array(); - $fp = fopen($srcFile, "r"); - $i = 0; - while (($row = fgetcsv($fp, 0, "\t")) !== false) { - if (count($row) == 1 || preg_match('/^#/', $row[0])) { - continue; - } + $items = array(); + $fp = fopen($srcFile, "r"); + $i = 0; + while (($row = fgetcsv($fp, 0, "\t")) !== false) { + if (count($row) == 1 || preg_match('/^#/', $row[0])) { + continue; + } - $i++; - $item = call_user_func(array('GeoName', $map), $row, $i); - if ($item) { - $items[] = $item; - } + $i++; + $item = call_user_func(array('GeoName', $map), $row, $i); + if ($item) { + $items[] = $item; } + } - echo sprintf("-> Writing %s\n", $file); + echo sprintf("-> Writing %s\n", $file); - $out = PhpArray::generate(compact('src', 'items')); - file_put_contents(ROOT . '/' . $file, $out); + $out = PhpArray::generate(compact('src', 'items')); + file_put_contents(ROOT . '/' . $file, $out); } protected static function _mapTimeZone($row, $i) { - static $header; - if ($i == 1) { - $header = $row; - foreach ($header as $key => $val) { - if (!preg_match('/(gmt|dst).+(\d+\. \w+ \d{4})$/i', $val, $match)) { - continue; - } - $day = date('Y-m-d', strtotime($match[2])); - $header[$key] = array('type' => strtolower($match[1]), 'day' => $day); - } - return false; + static $header; + if ($i == 1) { + $header = $row; + foreach ($header as $key => $val) { + if (!preg_match('/(gmt|dst).+(\d+\. \w+ \d{4})$/i', $val, $match)) { + continue; + } + $day = date('Y-m-d', strtotime($match[2])); + $header[$key] = array('type' => strtolower($match[1]), 'day' => $day); } - return $row[0]; - // return array( - // 'timezone_id' => $row[0], - // ($header[1]['type'].'_offset') => array( - // $header[1]['day'] => $row[1] - // ), - // ($header[2]['type'].'_offset') => array( - // $header[2]['day'] => $row[1], - // ), - // ); + return false; + } + return $row[0]; + // return array( + // 'timezone_id' => $row[0], + // ($header[1]['type'].'_offset') => array( + // $header[1]['day'] => $row[1] + // ), + // ($header[2]['type'].'_offset') => array( + // $header[2]['day'] => $row[1], + // ), + // ); } protected static function _mapCountry($row) { - return array( - 'name' => $row[4], - 'iso' => $row[0], - 'iso3' => $row[1], - 'iso_numeric' => $row[2], - 'fips' => $row[3], - 'captial' => $row[5], - 'area' => $row[6], - 'population' => $row[7], - 'continent' => $row[8], - 'tld' => $row[9], - 'currency_code' => $row[10], - 'currency_name' => $row[11], - 'phone_code' => $row[12], - 'postal_code_format' => $row[13], - 'postal_code_regex' => $row[14], - 'languages' => $row[15], - 'geoname_id' => $row[16], - 'neighbours' => $row[17], - ); + return array( + 'name' => $row[4], + 'iso' => $row[0], + 'iso3' => $row[1], + 'iso_numeric' => $row[2], + 'fips' => $row[3], + 'captial' => $row[5], + 'area' => $row[6], + 'population' => $row[7], + 'continent' => $row[8], + 'tld' => $row[9], + 'currency_code' => $row[10], + 'currency_name' => $row[11], + 'phone_code' => $row[12], + 'postal_code_format' => $row[13], + 'postal_code_regex' => $row[14], + 'languages' => $row[15], + 'geoname_id' => $row[16], + 'neighbours' => $row[17], + ); } protected static function _mapCity($row) { - return array( - 'geo_name_id' => $row[0], - 'name' => $row[1], - 'ascii_name' => $row[2], - 'alternate_name' => array_filter(preg_split('/(? $row[4], - 'longitude' => $row[5], - 'feature_class' => $row[6], - 'feature_code' => $row[7], - 'country_iso' => $row[8], - 'alternate_country_iso' => array_filter(preg_split('/(? $row[10], - // 'admin2_code' => $row[11], - // 'admin3_code' => $row[12], - // 'admin4_code' => $row[13], - 'population' => $row[14], - // 'elevation' => $row[15], - 'average_elevation' => $row[16], - 'timezone_id' => $row[17], - 'modified' => $row[18], - ); + return array( + 'geo_name_id' => $row[0], + 'name' => $row[1], + 'ascii_name' => $row[2], + 'alternate_name' => array_filter(preg_split('/(? $row[4], + 'longitude' => $row[5], + 'feature_class' => $row[6], + 'feature_code' => $row[7], + 'country_iso' => $row[8], + 'alternate_country_iso' => array_filter(preg_split('/(? $row[10], + // 'admin2_code' => $row[11], + // 'admin3_code' => $row[12], + // 'admin4_code' => $row[13], + 'population' => $row[14], + // 'elevation' => $row[15], + 'average_elevation' => $row[16], + 'timezone_id' => $row[17], + 'modified' => $row[18], + ); } } diff --git a/generators/libs/php_array.php b/generators/libs/php_array.php index e98ae80..69e2ae3 100644 --- a/generators/libs/php_array.php +++ b/generators/libs/php_array.php @@ -4,11 +4,11 @@ class PhpArray{ public static function generate($vars) { - extract($vars); + extract($vars); - // Make var_export real pretty - $items = preg_replace('/(\n\s+)?array \(/', 'array(', var_export($items, true)); - $template = << EOD; - return $template; + return $template; } public static function fetch($src, $file) { - $srcFile = TMP . '/' . $file; - if (file_exists($srcFile)) { - return $srcFile; - } + $srcFile = TMP . '/' . $file; + if (file_exists($srcFile)) { + return $srcFile; + } - $tmpDirOk = is_dir(TMP) || mkdir(TMP); - if (!$tmpDirOk) { - trigger_error('Could not create tmp dir', E_USER_ERROR); - } + $tmpDirOk = is_dir(TMP) || mkdir(TMP); + if (!$tmpDirOk) { + trigger_error('Could not create tmp dir', E_USER_ERROR); + } - $downloadFile = TMP . '/' . basename($src); - if (!file_exists($downloadFile)) { - echo sprintf("-> Downloading %s\n", basename($src)); - $cmd = sprintf('curl -o %s %s', escapeshellarg($downloadFile), escapeshellarg($src)); - system($cmd, $error); - if ($error) { - trigger_error('Could not download file', E_USER_ERROR); - } + $downloadFile = TMP . '/' . basename($src); + if (!file_exists($downloadFile)) { + echo sprintf("-> Downloading %s\n", basename($src)); + $cmd = sprintf('curl -o %s %s', escapeshellarg($downloadFile), escapeshellarg($src)); + system($cmd, $error); + if ($error) { + trigger_error('Could not download file', E_USER_ERROR); } + } - if (pathinfo($src, PATHINFO_EXTENSION) != 'zip') { - return $downloadFile; - } + if (pathinfo($src, PATHINFO_EXTENSION) != 'zip') { + return $downloadFile; + } - echo sprintf("-> Unzipping %s\n", basename($downloadFile)); - $cmd = sprintf('unzip -o %s -d %s', escapeshellarg($downloadFile), escapeshellarg(TMP)); - exec($cmd, $stdout, $error); - if ($error) { - trigger_error('Could not unzip downloaded file', E_USER_ERROR); - } + echo sprintf("-> Unzipping %s\n", basename($downloadFile)); + $cmd = sprintf('unzip -o %s -d %s', escapeshellarg($downloadFile), escapeshellarg(TMP)); + exec($cmd, $stdout, $error); + if ($error) { + trigger_error('Could not unzip downloaded file', E_USER_ERROR); + } - if (!preg_match_all('/\s+inflating:\s(.+)$/m', join("\n", $stdout), $matches, PREG_SET_ORDER)) { - trigger_error('Empty archive ', E_USER_ERROR); - } + if (!preg_match_all('/\s+inflating:\s(.+)$/m', join("\n", $stdout), $matches, PREG_SET_ORDER)) { + trigger_error('Empty archive ', E_USER_ERROR); + } - foreach ($matches as $match) { - $regex = sprintf('/%s$/', preg_quote($file, '/')); - if (preg_match($regex, $match[1])) { - return $match[1]; - } + foreach ($matches as $match) { + $regex = sprintf('/%s$/', preg_quote($file, '/')); + if (preg_match($regex, $match[1])) { + return $match[1]; } - trigger_error('File was not found in archive', E_USER_ERROR); + } + trigger_error('File was not found in archive', E_USER_ERROR); } } diff --git a/generators/mime.php b/generators/mime.php index ad7aa2f..5f87bcb 100755 --- a/generators/mime.php +++ b/generators/mime.php @@ -12,7 +12,7 @@ list(, $mimeType, $extensions) = $match; $extensions = preg_split('/\s+/', $extensions); foreach ($extensions as $ext) { - $items[$ext] = $mimeType; + $items[$ext] = $mimeType; } }