forked from cristiroma/countries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.php
101 lines (90 loc) · 2.5 KB
/
build.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/**
* Author: Cristian Romanescu <cristi _at_ eaudeweb dot ro>
* Created: 201407071327
*/
require_once 'bootstrap.php';
global $argv;
if(count($argv) < 2) {
echo "Usage: php build.php <json|csv|validate_flags|gh_pages>\n";
exit(-1);
}
$cmd = $argv[1];
call_user_func("exec_$cmd");
/** Export SQL data in JSON */
function exec_json() {
global $config;
$countries = db_query('SELECT * FROM country ORDER BY name');
$v = json_encode($countries, JSON_PRETTY_PRINT);
file_put_contents($config->json_dump, $v);
}
/** Export SQL data in CSV */
function exec_csv() {
global $config;
$countries = db_query('SELECT * FROM country ORDER BY name');
$fp = fopen($config->csv_dump, 'w');
fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
foreach($countries as $country) {
$row = array();
$row[0] = $country->name;
$row[1] = $country->name_official;
$row[2] = $country->code2l;
$row[3] = $country->code3l;
$row[4] = $country->flag_32;
$row[5] = $country->flag_128;
$row[6] = $country->latitude;
$row[7] = $country->longitude;
$row[8] = $country->zoom;
fputcsv($fp, $row);
}
fclose($fp);
}
/** Validate flag files exist on disk */
function exec_validate_flags() {
$countries = db_query('SELECT * FROM country ORDER BY name');
foreach($countries as $country) {
if(empty($country->flag_32)) {
echo "WARN: 32-pixel flag not set for: {$country->name}\n";
} else {
$f32 = "data/flags/{$country->flag_32}";
if(!is_file($f32)) {
echo "ERR : missing 32-pixel flag on disk for: {$country->name}\n";
}
}
if(empty($country->flag_128)) {
echo "WARN: 128-pixel flag not set for: {$country->name}\n";
} else {
$f128 = "data/flags/{$country->flag_128}";
if(!is_file($f128)) {
echo "ERR : missing 128-pixel flag on disk for: {$country->name}\n";
}
}
}
echo "No output above means everything is OK\n";
}
function exec_gh_pages() {
ob_start();
require_once 'www/gh-index.php';
mkdir('gh-pages');
$data = ob_get_clean();
if ($f = fopen('gh-pages/index.html', 'w+')) {
fwrite($f, $data);
fclose($f);
}
$countries = get_countries();
/** @var Country $country */
foreach($countries as $country) {
$filename = slugify($country->getCode3l()) . '.html';
if ($f = fopen('gh-pages/' . $filename, 'w+')) {
$_GET['code'] = $country->getCode3l();
ob_start();
$page_title = $country->getNameOfficial();
require 'www/includes/header.inc';
require 'www/detail.php';
require 'www/includes/footer.inc';
$data = ob_get_clean();
fwrite($f, $data);
fclose($f);
}
}
}