forked from MaZderMind/replicate-sequences
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.php
More file actions
117 lines (94 loc) · 3.15 KB
/
update.php
File metadata and controls
117 lines (94 loc) · 3.15 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
include('conf.php');
if(!$conf['abuse'] || $conf['abuse'] == 'your@email.here')
die('you MUST configure an abuse-email');
printf("connecting to database\n");
$db = new PDO('mysql:host='.$conf['host'].';dbname='.$conf['db'], $conf['user'], $conf['password']);
printf("ok\n");
if(!@$conf['create'] === false)
{
printf("creating database tables if not exist\n");
if($db->exec(file_get_contents('struct.sql')) === false)
die(print_r($db->errorInfo(), true));
printf("ok\n");
}
fetch($conf + array(
'table' => 'minute_replicate',
'base' => 'https://planet.osm.org/replication/minute/',
'firstSeq' => 1,
));
fetch($conf + array(
'table' => 'hour_replicate',
'base' => 'https://planet.osm.org/replication/hour/',
'firstSeq' => 1,
));
fetch($conf + array(
'table' => 'day_replicate',
'base' => 'https://planet.osm.org/replication/day/',
'firstSeq' => 1,
));
function fetch($conf)
{
global $db;
$stm = $db->prepare('INSERT IGNORE INTO '.$conf['table'].' (sequenceNumber, timestamp) VALUES (:sequenceNumber, :timestamp)');
$ctx = stream_context_create(array(
'http' => array(
'user_agent' => "updater for replicate-sequence Tool (https://github.com/MaZderMind/replicate-sequences) on ".php_uname('n').", contact ".$conf['abuse']." in case of abuse or problems",
),
));
$statefile = $conf['base'].'state.txt';
printf("fetching current remote statefile %s\n", $statefile);
$stateText = file_get_contents($statefile, false, $ctx);
if(!is_string($stateText))
{
printf("error fetching remote statefile\n");
exit(2);
}
if(!preg_match('/sequenceNumber=([0-9]+)/', $stateText, $match))
{
printf("format error in remote statefile\n");
exit(3);
}
$remoteSeq = intval($match[1]);
$localSeq = $db->query("SELECT MAX(sequenceNumber) FROM $conf[table]")->fetchColumn();
if(is_null($localSeq))
{
$localSeq = $conf['firstSeq'];
printf("no sequenceNumber in table, starting with %u\n", $conf['firstSeq']);
}
printf("current local sequenceNumber is %u\n", $localSeq);
printf("current remote sequenceNumber is %u\n", $remoteSeq);
if($remoteSeq == $localSeq)
{
printf("database is current, nothing to do\n");
return;
}
$seqRange = $remoteSeq - $localSeq;
printf("fetching %u statefiles\n", $seqRange);
for($seq = $localSeq; $seq <= $remoteSeq; $seq++)
{
$seqPath = str_pad($seq, 9, '0', STR_PAD_LEFT);
$seqPath = trim(chunk_split($seqPath, 3, '/'), '/');
$statefile = $conf['base'].$seqPath.'.state.txt';
$stateText = file_get_contents($statefile);
if(!is_string($stateText))
{
printf("error fetching remote statefile %s\n", $statefile);
exit(2);
}
if(!preg_match('/timestamp=([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2})\\\\:([0-9]{2})\\\\:([0-9]{2})Z/', $stateText, $match))
{
printf("format error in remote statefile %s\n", $statefile);
exit(3);
}
$original = array_shift($match);
$timestamp = vsprintf('%04u-%02u-%02u %02u:%02u:%02u', $match);
if($seq % 100 == 0 || $seq == $localSeq || $seq == $remoteSeq-1)
printf("(%.2f%%) sequenceNumber=%s timestamp=%s\n", ($seq - $localSeq) / $seqRange * 100, $seq, $timestamp);
$stm->execute(array(
'sequenceNumber' => $seq,
'timestamp' => $timestamp,
));
}
}
?>