Merge branch 'xorg/master' into xorg/f/geocoding
[platal.git] / bin / formatAddresses.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2011 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22
23 require './connect.db.inc.php';
24 require_once '../classes/address.php';
25 require_once '../classes/geocoder.php';
26 require_once '../classes/gmapsgeocoder.php';
27
28 $globals->debug = 0; // Do not store backtraces
29
30 $targets = array(
31 'g' => 'formatted_address',
32 'p' => 'postalText'
33 );
34 $ranges = array(
35 'f' => ' != \'\'',
36 'e' => ' = \'\'',
37 'a' => 'IS NOT NULL'
38 );
39
40 $options = getopt('g::t:r:h::', array('geocode::', 'target:', 'range:', 'help::'));
41
42 $help_required = isset($options['h']) || isset($options['help']);
43 $geocoding_required = isset($options['g']) || isset($options['geocode']);
44 $target = '';
45 $range = '';
46
47 if (isset($options['t'])) {
48 $target = $options['t'];
49 } elseif (isset($options['target'])) {
50 $target = $options['target'];
51 }
52
53 if (isset($options['r'])) {
54 $range = $options['r'];
55 } elseif ($options['range']) {
56 $range = $options['range'];
57 }
58
59 $missing_option = !array_key_exists($target, $targets) || !array_key_exists($range, $ranges);
60
61 if ($missing_option || $help_required) {
62 echo <<<EOF
63 SYNOPSIS
64 formatAddresses.php [-g] -t [g|p] -r [f|e|a]
65
66 DESCRIPTION
67 formatAddresses.php formats addresses. If the addresses need geocoding, this
68 must be specified (-g). The targetted group of addresses must be specified
69 (non formatted addresses, formatted addresses, all addresses).
70
71 OPTIONS
72 -g, --geocode
73 Geocodes the adresses. If not required, the address will not be
74 geolocated.
75 -t, --target [ g | p ]
76 The selection will be made either on the geocoding
77 (formatted_address) or the postal address (postalText).
78 -r, --range [ f | e | a ]
79 The selection will include the addresses corresponding to the right
80 target, which are formatted, empty (non formatted) or all addresses.
81 -h, --help
82 displays this help
83
84 EOF;
85 exit;
86 }
87
88 print "Formats addresses addresses.\n";
89
90 $where = '';
91 if ($range != 'a') {
92 $where = 'WHERE ' . $targets[$target] . $ranges[$range];
93 }
94
95 if ($geocoding_required) {
96 // Waiting time is computed as follows: 3600 * 24 / LIMIT,
97 // where LIMIT is google's limit reduced to take into account site geocoding.
98 $wait = ceil(3600 * 24 / 2000);
99 $display_limit = 1;
100 } else {
101 $wait = 0;
102 $display_limit = 100;
103 }
104
105 $it = XDB::rawIterator('SELECT *
106 FROM profile_addresses
107 ' . $where . '
108 ORDER BY pid, jobid, type, id');
109
110 $total = $it->total();
111 $i = 0;
112 $j = 0;
113 $skipped = 0;
114 printf("\r%u / %u", $i, $total);
115
116 while ($item = $it->next()) {
117 $address = new Address($item);
118 $address->changed = ($geocoding_required ? 1 : 0);
119 $address->format();
120 if ($address->delete()) {
121 $address->save();
122 } else {
123 ++$skipped;
124 }
125
126 ++$i;
127 if ($i == $display_limit) {
128 ++$j;
129 $i = 0;
130 printf("\r%u / %u", $i + $display_limit * $j, $total);
131 }
132 sleep($wait * $address->geocoding_calls);
133 }
134 printf("\r%u / %u", $i + $display_limit * $j, $total);
135
136 if ($skipped != 0) {
137 printf("\n%u addresses skipped.\n", $skipped);
138 }
139
140 print "\nDone.\n";
141
142 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
143 ?>