3
* php file2sql.php /path/to/tiles
6
ini_set('display_errors', 'On');
10
$GLOBALS['cwd'] = str_replace('\\', '/', getcwd());
11
$GLOBALS['tables'] = array();
12
$GLOBALS['tiles'] = array();
14
$dir = ($_GET['dir'] ? $_GET['dir'] : $_SERVER['argv'][1]);
16
if (!mysql_connect('localhost', 'helioviewer', 'helioviewer')) die ("error connecting to db");
17
mysql_select_db('esahelio_svdb0');
19
//header("Content-type: text/plain");
23
//print_r($GLOBALS['observatory']);
25
echo "Insertion successful\n";
27
function parseDir($dir) {
28
echo "Parsing $dir\n";
30
if ($GLOBALS['count'] == $GLOBALS['limit']) exit;
33
while ($entry = readdir($hd)) {
34
if ($entry == '.' || $entry == '..') continue;
35
if (is_dir("$dir/$entry")) parseDir("$dir/$entry");
36
elseif (substr($entry, -4) == '.jpg' || substr($entry, -4) == '.png') {
37
$tiles[] = "$dir/$entry";
38
//echo "Found tile: $dir/$entry\n";
46
$pathinfo = pathinfo($tiles[0]);
47
$filename = $pathinfo['basename'];
48
$extension = $pathinfo['extension'];
49
$map = substr($filename, 0, 34);
50
list($year, $month, $day, $time, $observatory, $instrument, $detector, $measurement) =
52
$hour = substr($time, 0, 2);
53
$min = substr($time, 2, 2);
54
$sec = substr($time, 4, 2);
56
//$ratio = (float) substr($filename, 47, 7);
59
//Note: MySQL stores dates using the server's local timezone, but outputs UNIX_TIMESTAMP() in GMT time. Rather than requiring the
60
// server switch to using UTC to store dates by default, which is not entirely straight-forward, it is easier to pass it a
61
// local date, so that when UNIX_TIMESTAMP() is called, the returned timestamp is the actual UTC time.
62
$ts = convertToLocalDate("$year-$month-$day $hour:$min:$sec");
64
//echo "original date: " . "$year-$month-$day $hour:$min:$sec\n";
65
//echo "offseted date: $ts\n";
67
$GLOBALS['observatory'][$observatory]['abbreviation'] = $observatory;
68
$GLOBALS['observatory'][$observatory]['instrument'][$instrument]['abbreviation'] = $instrument;
69
$GLOBALS['observatory'][$observatory]['instrument'][$instrument]['detector'][$detector]['abbreviation'] = $detector;
70
//$GLOBALS['observatory'][$observatory]['instrument'][$instrument]['detector'][$detector]['sunImgRatio'] = $ratio;
71
// ToDo: Determine lowestRegularZoomLevel (lowest zoom level that still has the same image/sun diameter ratio as higher levels. The ratio decreases by the factor 2 for lower zoom levels)
72
$GLOBALS['observatory'][$observatory]['instrument'][$instrument]['detector'][$detector]['measurement'][$measurement]['abbreviation'] = $measurement;
73
$GLOBALS['observatory'][$observatory]['instrument'][$instrument]['detector'][$detector]['measurement'][$measurement]['image'][] = array('timestamp' => $ts, 'measurement' => $measurement, 'filetype' => $extension, 'tiles' => $tiles);
78
$GLOBALS['tables']['observatory'][$observatory] = array('abbreviation' => $observatory);
79
$GLOBALS['tables']['instrument'][] = array('abbreviation' => $instrument, 'observatory' => $observatory);
80
$GLOBALS['tables']['detector'][] = array('abbreviation' => $detector, 'instrument' => $instrument);
81
$GLOBALS['tables']['measurement'][] = array('abbreviation' => $measurement, 'detector' => $detector);
82
$GLOBALS['tables']['image'][] = array('timestamp' => "$year-$month-$day $hour:$min:$sec", 'measurement' => $measurement);
84
//$GLOBALS['tiles'][$map] = $tiles;
87
echo "Adding image $map...\n";
89
$query = sprintf("INSERT IGNORE INTO maps VALUES ('%s',
91
%d, %d, %d, %d, %d, %d,
92
'%s', '%s', '%s', '%s', '%s')",
93
mysql_real_escape_string($map),
94
"$year-$month-$day $hour:$min:$sec",
95
$year, $month, $day, $hour, $min, $sec,
96
$observatory, $instrument, $detector, $measurement, $extension
98
$result = mysql_query($query);
100
echo "$query - failed\n";
104
// if (mysql_affected_rows() > 0) {
105
// echo "Adding tiles in $dir: ";
113
function populateDb() {
114
parseLevels($GLOBALS['observatory'], 'observatory');
117
function parseLevels($level, $table, $parentTable = null, $parentId = null) {
118
foreach($level as $key => $entry) {
123
foreach ($entry as $name => $value) {
124
if ($name == 'tiles') {
126
} elseif (is_array($value)) {
127
$subelements = $value;
129
$fields[$name] = $value;
133
if ($parentTable) $fields[$parentTable] = $parentId;
134
// insert fields into table
135
// get and save the id
136
$fields['id'] = getIdOrInsert($table, $fields, $parentTable, $parentId);
138
if ($tiles) addTiles($tiles, $fields['id']);
139
//if ($parentTable) {
140
//echo "$table($fields[id]) -> $parentTable($parentId)\n";
142
if ($subelements) parseLevels($subelements, $name, $table, $fields['id']);
147
function getIdOrInsert($table, $fields, $parentTable = null, $parentId = null) {
148
//echo "parentField: ${parentTable}Id ($parentId)\n";
149
// Check if row exists
150
$query = "SELECT id FROM $table WHERE";
152
foreach ($fields as $field => $value) {
153
if ($and) $query .= " AND";
156
if ($field == $parentTable) $query .= " ${parentTable}Id=$parentId";
157
else $query .= " $field='$value'";
159
$result = mysql_query($query);
162
if (mysql_num_rows($result) > 0) {
164
$row = mysql_fetch_array($result);
168
$query = "INSERT INTO $table (";
169
if ($fields['abbreviation']) {
175
foreach ($fields as $field => $value) {
176
if ($comma) $query .= ", ";
179
if ($field == $parentTable) $query .= "${parentTable}Id";
180
else $query .= "$field";
183
$query .= ") VALUES (";
184
if ($fields['abbreviation']) {
186
$query .= "'$fields[abbreviation]'";
190
foreach ($fields as $field => $value) {
191
if ($comma) $query .= ", ";
194
if ($field == $parentTable) $query .= $parentId;
195
else $query .= "'$value'";
200
$result = mysql_query($query);
201
return mysql_insert_id();
205
function addTiles($tiles, $imageId) {
209
$num = count($tiles);
211
foreach($tiles as $filepath) {
212
addTile($filepath, $imageId);
213
$p = (int)($c / $num * 100);
224
function addTile($filepath, $imageId) {
226
$pathinfo = pathinfo($filepath);
227
$filename = $pathinfo['basename'];
228
$filetype = $pathinfo['extension'];
229
$map = substr($filename, 0, 34);
230
$zoom = substr($filename, 35, 2);
231
$x = substr($filename, 38, 2);
232
$y = substr($filename, 41, 2);
233
//if ($x == 0 && $y == 0) {
234
// $ratio = (float) substr($filename, 47, 7);
236
//echo "Adding tile: $filename\n";
237
//echo "$zoom, $x, $y, $ratio\n";
238
$query = sprintf("INSERT IGNORE INTO tile (imageId, x, y, zoom, tile) VALUES ($imageId, $x, $y, $zoom, '%s')", mysql_real_escape_string(file_get_contents($filepath)));
239
$result = mysql_query($query);
241
echo "$query - failed\n";
246
function convertToLocalDate ($timestamp) {
247
$time = date_create($timestamp);
248
$offset = date_offset_get($time);
249
$time->modify($offset . " seconds");
250
return $time->format('Y-m-d H:i:s');