Add Article 5 NATO medal image.
[platal.git] / bin / formatAddresses.php
1 #!/usr/bin/php5 -q
2 <?php
3 /***************************************************************************
4 * Copyright (C) 2003-2014 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 require_once '../classes/visibility.php';
28
29 $globals->debug = 0; // Do not store backtraces
30
31 $targets = array(
32 'g' => 'pa.formatted_address',
33 'p' => 'pa.postalText'
34 );
35 $ranges = array(
36 'f' => ' != \'\'',
37 'e' => ' = \'\'',
38 'a' => ' IS NOT NULL'
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
89 print "Formats addresses.\n";
90
91 if ($range != 'a') {
92 $where = $targets[$target] . $ranges[$range];
93 } else {
94 $where = null;
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
107 $it = Address::iterate(array(), array(), array(), Visibility::get(Visibility::VIEW_PRIVATE), $where);
108
109 $total = $it->total();
110 $i = 0;
111 $j = 0;
112 $skipped = 0;
113 printf("\r%u / %u", $i, $total);
114
115 while ($address = $it->next()) {
116 $address->changed = ($geocoding_required ? 1 : 0);
117 $address->format();
118 if ($address->delete()) {
119 $address->save(false);
120 } else {
121 ++$skipped;
122 }
123
124 ++$i;
125 if ($i == $display_limit) {
126 ++$j;
127 $i = 0;
128 printf("\r%u / %u", $i + $display_limit * $j, $total);
129 }
130 sleep($wait * $address->geocoding_calls);
131 }
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";
139
140 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
141 ?>