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