-
Notifications
You must be signed in to change notification settings - Fork 1
/
cron_update_cs.php
executable file
·80 lines (60 loc) · 1.81 KB
/
cron_update_cs.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
<?php
/*
* Cron to update the user changeset number in the database
*/
require('vendor/autoload.php');
$ini = parse_ini_file("variables.ini.php");
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection(
[
'driver' => 'mysql',
'host' => $ini['host'],
'database' => $ini['database'],
'username' => $ini['username'],
'password' => $ini['password'],
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]
);
$capsule->setAsGlobal();
$capsule->bootEloquent();
//// Script output
$verbose = 0; //no output
if (isset($argv[1]) && $argv[1]=="-v") {
$verbose=1;
print "Update changeset number in DB\n";
}
//make a call to the API to retrieve the user total changeset number
function total_changesets($user_id)
{
$site = 'http://www.openstreetmap.org';
$api = '/api/0.6/user';
$url = $site.$api.'/'.$user_id;
$xml = simpleXML_load_file($url, "SimpleXMLElement", LIBXML_NOCDATA);
if ($xml === false) {
return -1; //error
} else {
return $xml->user->changesets['count'];
}
}
///////////////MAIN
$back_time = time() - ($ini['days_ago'] * 24 * 60 * 60);
$query = "SELECT user_id FROM new_user WHERE
(total_changesets IS NULL OR total_changesets < ".$ini['cs_upper_limit'].")
AND first_edit_date > $back_time
ORDER BY user_id DESC
";
$result = Capsule::select($query);
foreach ($result as $elem) {
sleep(3); // three seconds delay between each call
$tcs = total_changesets($elem->user_id);
if ($tcs > 0) {
Capsule::insert(
'UPDATE new_user SET total_changesets='. $tcs .',last_check='. time() .' WHERE user_id='. $elem->user_id
);
}
if ($verbose) {
print "User ID: $elem->user_id cs:$tcs\n";
}
}