s/a\.uid/$UID/ && s/p\.pid/$PID/
[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
ce0b2c6f 16 XDB::execute("INSERT INTO profile_phones (pid, link_type, link_id, tel_id ,tel_type,
b235d980
GB
17 search_tel, display_tel, pub, comment)
18 VALUES " . $values . "
19 ON DUPLICATE KEY UPDATE display_tel = VALUES(display_tel)");
20}
21
e4cd7a1f
SJ
22$res = XDB::query("SELECT DISTINCT phonePrefix
23 FROM geoloc_countries
24 WHERE phonePrefix IS NOT NULL");
b235d980
GB
25$prefixes = $res->fetchColumn();
26foreach ($prefixes as $i => $prefix) {
e4cd7a1f
SJ
27 $res = XDB::query("SELECT phoneFormat
28 FROM geoloc_countries
29 WHERE phonePrefix = {?} AND phoneFormat != '' LIMIT 1",
b235d980
GB
30 $prefix);
31 if ($res->numRows() > 0) {
32 $format = $res->fetchOneCell();
33 //Build regexp for mysql query
34 $len = strlen($format);
35 $regexp = "^";
36 $nbPar = 0;
37 for ($i = 0; $i < $len; $i++) {
38 $char = $format[$i];
39 switch ($char) {
40 case 'p':
41 $regexp .= $prefix;
42 break;
43 case '#':
44 if ($nbPar == 0) {
45 $regexp .= '(';
46 $nbPar++;
47 }
48 $regexp .= '[0-9](';
49 $nbPar++;
50 break;
51 default:
52 //Appends the char after escaping it if necessary
53 $escape = array('[', ']', '{', '}', '(', ')', '*', '+', '?', '.', '^', '$', '|', '\\');
54 if (in_array($char, $escape)) {
55 $regexp .= '[' . $char . ']';
56 } else {
57 $regexp .= $char;
58 }
59 }
60 }
61 //allows additionnal spaces and numbers
62 $regexp .= '[0-9 ]*';
63 //closes parenthesis
64 for ($i = 0; $i < $nbPar; $i++) {
65 $regexp .= ')?';
66 }
67 $regexp .= '$';
ce0b2c6f 68 $res = XDB::iterator("SELECT pid, link_type, link_id, tel_id, tel_type, search_tel,
b235d980
GB
69 display_tel, pub, comment
70 FROM profile_phones
71 WHERE search_tel LIKE {?} AND display_tel NOT REGEXP {?}",
72 $prefix . '%', $regexp);
73 if ($res->numRows() > 0)
74 {
75 //To speed up the update of phone numbers, theses updates are grouped by block of 1000
76 $values = '';
77 $i = 0;
78 while ($phone = $res->next()) {
79 $disp = format_display_number($phone['search_tel'], $error, array('format' => $format, 'phoneprf' => $prefix));
80 if ($values != '') {
81 $values .= ",\n";
82 }
ce0b2c6f 83 $values .= "('" . addslashes($phone['pid']) . "', '" . addslashes($phone['link_type'])
b235d980
GB
84 . "', '" . addslashes($phone['link_id'])
85 . "', '" . addslashes($phone['tel_id']) . "', '" . addslashes($phone['tel_type'])
86 . "', '" . addslashes($phone['search_tel']) . "', '" . addslashes($disp)
87 . "', '" . addslashes($phone['pub']) . "', '" . addslashes($phone['comment']) . "')";
88 $i++;
89 if ($i == 1000) {
90 do_update_by_block($values);
91 $values = '';
92 $i = 0;
93 }
94 }
95 if ($values != '') {
96 do_update_by_block($values);
97 }
98 }
99 }
100}
101
102// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
103?>