Finishes to move functions from geoloc.inc.php to geocoding.inc.php,
[platal.git] / bin / cron / phones.check.php
CommitLineData
b235d980
GB
1#!/usr/bin/php5
2<?php
3
4require('./connect.db.inc.php');
b235d980
GB
5require_once('profil.func.inc.php');
6
7$globals->debug = 0; //do not store backtraces
8
9
10function do_update_by_block($values)
11{
12 // Update display_tel by block
13 // Because there is no mysql update syntax for multiple updates in one query
14 // we use a multiple insert syntax which will fail because the key already exist
15 // and then update the display_tel
16 XDB::execute("INSERT INTO profile_phones (uid, link_type, link_id, tel_id ,tel_type,
17 search_tel, display_tel, pub, comment)
18 VALUES " . $values . "
19 ON DUPLICATE KEY UPDATE display_tel = VALUES(display_tel)");
20}
21
22$res = XDB::query("SELECT DISTINCT g.phoneprf FROM geoloc_pays AS g WHERE g.phoneprf IS NOT NULL");
23$prefixes = $res->fetchColumn();
24foreach ($prefixes as $i => $prefix) {
25 $res = XDB::query("SELECT g.phoneformat FROM geoloc_pays AS g
26 WHERE g.phoneprf = {?} AND g.phoneformat != '' LIMIT 1",
27 $prefix);
28 if ($res->numRows() > 0) {
29 $format = $res->fetchOneCell();
30 //Build regexp for mysql query
31 $len = strlen($format);
32 $regexp = "^";
33 $nbPar = 0;
34 for ($i = 0; $i < $len; $i++) {
35 $char = $format[$i];
36 switch ($char) {
37 case 'p':
38 $regexp .= $prefix;
39 break;
40 case '#':
41 if ($nbPar == 0) {
42 $regexp .= '(';
43 $nbPar++;
44 }
45 $regexp .= '[0-9](';
46 $nbPar++;
47 break;
48 default:
49 //Appends the char after escaping it if necessary
50 $escape = array('[', ']', '{', '}', '(', ')', '*', '+', '?', '.', '^', '$', '|', '\\');
51 if (in_array($char, $escape)) {
52 $regexp .= '[' . $char . ']';
53 } else {
54 $regexp .= $char;
55 }
56 }
57 }
58 //allows additionnal spaces and numbers
59 $regexp .= '[0-9 ]*';
60 //closes parenthesis
61 for ($i = 0; $i < $nbPar; $i++) {
62 $regexp .= ')?';
63 }
64 $regexp .= '$';
65 $res = XDB::iterator("SELECT uid, link_type, link_id, tel_id, tel_type, search_tel,
66 display_tel, pub, comment
67 FROM profile_phones
68 WHERE search_tel LIKE {?} AND display_tel NOT REGEXP {?}",
69 $prefix . '%', $regexp);
70 if ($res->numRows() > 0)
71 {
72 //To speed up the update of phone numbers, theses updates are grouped by block of 1000
73 $values = '';
74 $i = 0;
75 while ($phone = $res->next()) {
76 $disp = format_display_number($phone['search_tel'], $error, array('format' => $format, 'phoneprf' => $prefix));
77 if ($values != '') {
78 $values .= ",\n";
79 }
80 $values .= "('" . addslashes($phone['uid']) . "', '" . addslashes($phone['link_type'])
81 . "', '" . addslashes($phone['link_id'])
82 . "', '" . addslashes($phone['tel_id']) . "', '" . addslashes($phone['tel_type'])
83 . "', '" . addslashes($phone['search_tel']) . "', '" . addslashes($disp)
84 . "', '" . addslashes($phone['pub']) . "', '" . addslashes($phone['comment']) . "')";
85 $i++;
86 if ($i == 1000) {
87 do_update_by_block($values);
88 $values = '';
89 $i = 0;
90 }
91 }
92 if ($values != '') {
93 do_update_by_block($values);
94 }
95 }
96 }
97}
98
99// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
100?>