Fixes unsubscription from group
[platal.git] / upgrade / newdirectory-0.0.1 / addresses.php
CommitLineData
8f2f5083
SJ
1#!/usr/bin/php5
2<?php
3require_once 'connect.db.inc.php';
4require_once 'geocoding.inc.php';
5
6$globals->debug = 0; // Do not store backtraces.
7
8ed67daa
SJ
8$res = XDB::query('SELECT MIN(pid), MAX(pid)
9 FROM profiles');
8f2f5083
SJ
10
11$pids = $res->fetchOneRow();
12
13$minPid = $pids[0];
14$maxPid = $pids[1];
15
4e2f2ad2 16echo "This will take a few minutes.\n";
8f2f5083
SJ
17
18// Fills the 'text' field in profile_addresses.
19for ($pid = $minPid; $pid < $maxPid + 1; ++$pid) {
20 $res = XDB::iterator("SELECT a.adrid AS id, a.adr1, a.adr2, a.adr3,
21 UNIX_TIMESTAMP(a.datemaj) AS datemaj,
22 a.postcode, a.city, a.cityid, a.region, a.regiontxt,
23 a.pub, a.country, gp.pays AS countrytxt, gp.display,
24 FIND_IN_SET('coord-checked', a.statut) AS checked,
25 FIND_IN_SET('res-secondaire', a.statut) AS secondaire,
26 FIND_IN_SET('courrier', a.statut) AS mail,
27 FIND_IN_SET('temporaire', a.statut) AS temporary,
28 FIND_IN_SET('active', a.statut) AS current,
29 FIND_IN_SET('pro', a.statut) AS pro,
30 a.glat AS precise_lat, a.glng AS precise_lon
8ed67daa
SJ
31 FROM #x4dat#.adresses AS a
32 INNER JOIN #x4dat#.geoloc_pays AS gp ON (gp.a2 = a.country)
33 INNER JOIN account_profiles AS ap ON (a.uid = ap.uid AND FIND_IN_SET('owner', ap.perms))
34 WHERE ap.pid = {?}
35 ORDER BY a.adrid",
8f2f5083
SJ
36 $pid);
37
38 while ($address = $res->next()) {
39 $text = get_address_text($address);
40 XDB::iterator('UPDATE profile_addresses
41 SET text = {?}
42 WHERE pid = {?} AND type = {?} AND id = {?}',
43 $text, $pid, $address['pro'] ? 'job' : 'home', $address['id']);
44 }
45}
46
47echo "Filling the 'text' filles is over. Geocoding will start now and will take a few days\n";
48
49// Tries to geocode all the addresses.
50for ($pid = $minPid; $pid < $maxPid + 1; ++$pid) {
51 $res = XDB::iterator('SELECT *
52 FROM profile_addresses
53 WHERE pid = {?}',
54 $pid);
55
56 while ($address = $res->next()) {
57 $updateTime = $address['updateTime'];
58 $gmapsGeocoder = new GMapsGeocoder();
59 $address = $gmapsGeocoder->getGeocodedAddress($address);
60
61 if (!isset($address['geoloc'])) {
ecad8a08
SJ
62 // TODO: use address and phone classes to update profile_job_enum and profile_phones once they are done.
63
8f2f5083
SJ
64 XDB::execute('DELETE FROM profile_addresses
65 WHERE pid = {?} AND id = {?} AND type = {?}',
66 $address['pid'], $address['id'], $address['type']);
67
68 Geocoder::getAreaId($address, 'administrativeArea');
69 Geocoder::getAreaId($address, 'subAdministrativeArea');
70 Geocoder::getAreaId($address, 'locality');
71 XDB::execute('INSERT INTO profile_addresses (pid, type, id, flags, accuracy,
72 text, postalText, postalCode, localityId,
73 subAdministrativeAreaId, administrativeAreaId,
74 countryId, latitude, longitude, updateTime, pub, comment,
75 north, south, east, west)
76 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?},
77 {?}, {?}, FROM_UNIXTIME({?}), {?}, {?}, {?}, {?}, {?}, {?})',
78 $address['pid'], $address['type'], $address['id'], $flags, $address['accuracy'],
79 $address['text'], $address['postalText'], $address['postalCode'], $address['localityId'],
80 $address['subAdministrativeAreaId'], $address['administrativeAreaId'],
81 $address['countryId'], $address['latitude'], $address['longitude'],
82 $updateTime, $address['pub'], $address['comment'],
83 $address['north'], $address['south'], $address['east'], $address['west']);
84 } else {
85 XDB::execute('UPDATE profile_addresses
86 SET postalText = {?}
87 WHERE pid = {?} AND id = {?} AND type = {?}',
88 $address['postalText'], $address['pid'], $address['id'], $address['type']);
89 }
90
91 sleep(60); // So we don't get blacklisted by Google.
92 }
93}
94
95echo "Geocoding is over.\n";
96
97function get_address_text($adr)
98{
8ed67daa
SJ
99 $t = '';
100 if (isset($adr['adr1']) && $adr['adr1']) $t .= $adr['adr1'];
101 if (isset($adr['adr2']) && $adr['adr2']) $t .= "\n".$adr['adr2'];
102 if (isset($adr['adr3']) && $adr['adr3']) $t .= "\n".$adr['adr3'];
103 $l = '';
8f2f5083
SJ
104 if (isset($adr['display']) && $adr['display']) {
105 $keys = explode(' ', $adr['display']);
106 foreach ($keys as $key) {
107 if (isset($adr[$key])) {
8ed67daa 108 $l .= ' ' . $adr[$key];
8f2f5083 109 } else {
8ed67daa 110 $l .= ' ' . $key;
8f2f5083
SJ
111 }
112 }
113 if ($l) substr($l, 1);
114 } elseif ($adr['country'] == 'US' || $adr['country'] == 'CA' || $adr['country'] == 'GB') {
8ed67daa
SJ
115 if ($adr['city']) $l .= $adr['city'] . ",\n";
116 if ($adr['region']) $l .= $adr['region'] . ' ';
8f2f5083
SJ
117 if ($adr['postcode']) $l .= $adr['postcode'];
118 } else {
8ed67daa 119 if (isset($adr['postcode']) && $adr['postcode']) $l .= $adr['postcode'] . ' ';
8f2f5083
SJ
120 if (isset($adr['city']) && $adr['city']) $l .= $adr['city'];
121 }
8ed67daa 122 if ($l) $t .= "\n" . trim($l);
8f2f5083 123 if ($adr['country'] != '00' && (!$adr['countrytxt'] || $adr['countrytxt'] == strtoupper($adr['countrytxt']))) {
8ed67daa
SJ
124 $res = XDB::query('SELECT countryFR
125 FROM geoloc_countries
126 WHERE iso_3166_1_a2 = {?}',
8f2f5083
SJ
127 $adr['country']);
128 $adr['countrytxt'] = $res->fetchOneCell();
129 }
130 if (isset($adr['countrytxt']) && $adr['countrytxt']) {
8ed67daa 131 $t .= "\n" . $adr['countrytxt'];
8f2f5083
SJ
132 }
133 return trim($t);
134}
135
136/* vim:set et sw=4 sts=4 ts=4: */
137?>