Commit | Line | Data |
---|---|---|
c1ecaa25 SJ |
1 | #!/usr/bin/php5 -q |
2 | <?php | |
3 | /*************************************************************************** | |
ba6ae046 | 4 | * Copyright (C) 2003-2013 Polytechnique.org * |
c1ecaa25 SJ |
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'; | |
49f0caac SJ |
25 | require_once '../classes/geocoder.php'; |
26 | require_once '../classes/gmapsgeocoder.php'; | |
4ea29d3e | 27 | require_once '../classes/visibility.php'; |
c1ecaa25 SJ |
28 | |
29 | $globals->debug = 0; // Do not store backtraces | |
30 | ||
49f0caac | 31 | $targets = array( |
4ea29d3e SJ |
32 | 'g' => 'pa.formatted_address', |
33 | 'p' => 'pa.postalText' | |
49f0caac SJ |
34 | ); |
35 | $ranges = array( | |
36 | 'f' => ' != \'\'', | |
37 | 'e' => ' = \'\'', | |
4ea29d3e | 38 | 'a' => ' IS NOT NULL' |
49f0caac SJ |
39 | ); |
40 | ||
41 | $options = getopt('g::t:r:h::', array('geocode::', 'target:', 'range:', 'help::')); | |
42 | ||
43 | $help_required = isset($options['h']) || isset($options['help']); | |
44 | $geocoding_required = isset($options['g']) || isset($options['geocode']); | |
45 | $target = ''; | |
46 | $range = ''; | |
47 | ||
48 | if (isset($options['t'])) { | |
49 | $target = $options['t']; | |
50 | } elseif (isset($options['target'])) { | |
51 | $target = $options['target']; | |
52 | } | |
53 | ||
54 | if (isset($options['r'])) { | |
55 | $range = $options['r']; | |
56 | } elseif ($options['range']) { | |
57 | $range = $options['range']; | |
58 | } | |
59 | ||
60 | $missing_option = !array_key_exists($target, $targets) || !array_key_exists($range, $ranges); | |
61 | ||
62 | if ($missing_option || $help_required) { | |
63 | echo <<<EOF | |
64 | SYNOPSIS | |
65 | formatAddresses.php [-g] -t [g|p] -r [f|e|a] | |
66 | ||
67 | DESCRIPTION | |
68 | formatAddresses.php formats addresses. If the addresses need geocoding, this | |
69 | must be specified (-g). The targetted group of addresses must be specified | |
70 | (non formatted addresses, formatted addresses, all addresses). | |
71 | ||
72 | OPTIONS | |
73 | -g, --geocode | |
74 | Geocodes the adresses. If not required, the address will not be | |
75 | geolocated. | |
76 | -t, --target [ g | p ] | |
77 | The selection will be made either on the geocoding | |
78 | (formatted_address) or the postal address (postalText). | |
79 | -r, --range [ f | e | a ] | |
80 | The selection will include the addresses corresponding to the right | |
81 | target, which are formatted, empty (non formatted) or all addresses. | |
82 | -h, --help | |
83 | displays this help | |
84 | ||
85 | EOF; | |
86 | exit; | |
87 | } | |
88 | ||
5b24448d | 89 | print "Formats addresses.\n"; |
49f0caac | 90 | |
49f0caac | 91 | if ($range != 'a') { |
4ea29d3e SJ |
92 | $where = $targets[$target] . $ranges[$range]; |
93 | } else { | |
94 | $where = null; | |
49f0caac SJ |
95 | } |
96 | ||
97 | if ($geocoding_required) { | |
98 | // Waiting time is computed as follows: 3600 * 24 / LIMIT, | |
99 | // where LIMIT is google's limit reduced to take into account site geocoding. | |
100 | $wait = ceil(3600 * 24 / 2000); | |
101 | $display_limit = 1; | |
102 | } else { | |
103 | $wait = 0; | |
104 | $display_limit = 100; | |
105 | } | |
106 | ||
4ea29d3e | 107 | $it = Address::iterate(array(), array(), array(), Visibility::get(Visibility::VIEW_PRIVATE), $where); |
49f0caac | 108 | |
c1ecaa25 SJ |
109 | $total = $it->total(); |
110 | $i = 0; | |
111 | $j = 0; | |
49f0caac | 112 | $skipped = 0; |
c1ecaa25 | 113 | printf("\r%u / %u", $i, $total); |
49f0caac | 114 | |
4ea29d3e | 115 | while ($address = $it->next()) { |
49f0caac SJ |
116 | $address->changed = ($geocoding_required ? 1 : 0); |
117 | $address->format(); | |
118 | if ($address->delete()) { | |
3f96e56b | 119 | $address->save(false); |
49f0caac SJ |
120 | } else { |
121 | ++$skipped; | |
122 | } | |
c1ecaa25 SJ |
123 | |
124 | ++$i; | |
49f0caac | 125 | if ($i == $display_limit) { |
c1ecaa25 SJ |
126 | ++$j; |
127 | $i = 0; | |
49f0caac | 128 | printf("\r%u / %u", $i + $display_limit * $j, $total); |
c1ecaa25 | 129 | } |
49f0caac | 130 | sleep($wait * $address->geocoding_calls); |
c1ecaa25 | 131 | } |
49f0caac SJ |
132 | printf("\r%u / %u", $i + $display_limit * $j, $total); |
133 | ||
134 | if ($skipped != 0) { | |
135 | printf("\n%u addresses skipped.\n", $skipped); | |
136 | } | |
137 | ||
138 | print "\nDone.\n"; | |
c1ecaa25 SJ |
139 | |
140 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
141 | ?> |