Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 95 additions & 95 deletions generators/geo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[3])),
'latitude' => $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[9])),
// 'admin1_code' => $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[3])),
'latitude' => $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[9])),
// 'admin1_code' => $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],
);
}
}

Expand Down
78 changes: 39 additions & 39 deletions generators/libs/php_array.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
// Make var_export real pretty
$items = preg_replace('/(\n\s+)?array \(/', 'array(', var_export($items, true));
$template = <<<EOD
<?php
/**
* @copyright $src
Expand All @@ -18,52 +18,52 @@ public static function generate($vars) {
\$items = $items;
?>
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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion generators/mime.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
list(, $mimeType, $extensions) = $match;
$extensions = preg_split('/\s+/', $extensions);
foreach ($extensions as $ext) {
$items[$ext] = $mimeType;
$items[$ext] = $mimeType;
}
}

Expand Down