Improves a bit more the script about merge issues linked to phone.
[platal.git] / upgrade / 1.0.1 / merge_issues_phones.php
1 #!/usr/bin/php5
2 <?php
3 // WARNING: this script takes a few minutes to be executed completly, thus run it into a screen.
4
5 require_once 'connect.db.inc.php';
6 require_once '../../classes/phone.php';
7
8 $globals->debug = 0; // Do not store backtraces.
9
10 print "Formats non formated phones. (1/2)\n";
11 $it = XDB::rawIterator("SELECT search_tel AS search, display_tel AS display, comment, link_id,
12 tel_type AS type, link_type, tel_id AS id, pid, pub
13 FROM profile_phones
14 WHERE search_tel = '' OR search_tel IS NULL
15 ORDER BY pid, link_id, tel_id");
16 $total = $it->total();
17 $i = 0;
18 $j = 0;
19 while ($item = $it->next()) {
20 $phone = new Phone($item);
21 $phone->delete();
22 $phone->save();
23
24 ++$i;
25 ++$j;
26 if ($j == 100) {
27 $j = 0;
28 printf("\r%u / %u", $i, $total);
29 }
30 }
31 printf("\r%u / %u", $i, $total);
32 print "\nFormating done.\n\n";
33
34 print "Deletes duplicated phones. (2/2)\n";
35 $pids = XDB::rawFetchColumn("SELECT DISTINCT(pid)
36 FROM profile_phones AS p1
37 WHERE link_type = 'user' AND EXISTS (SELECT *
38 FROM profile_phones AS p2
39 WHERE p2.link_type = 'user' AND p2.pid = p1.pid AND p2.tel_id != p1.tel_id)
40 ORDER BY pid");
41 $total = count($pids);
42 $done = 0;
43 $aux = 0;
44 $deleted = 0;
45 $phones = array();
46 $duplicates = array();
47 foreach ($pids as $pid) {
48 $count = 0;
49 Phone::iterate(array($pid), array(Phone::LINK_PROFILE), array(0));
50 while ($item = $it->next()) {
51 $phones[] = $item;
52 ++$count;
53 }
54 for ($i = 0; $i < $count; ++$i) {
55 for ($j = $i + 1; $j < $count; ++$j) {
56 if ($phones[$i]->search() == $phones[$j]->search()) {
57 $duplicates[$i] = true;
58 }
59 }
60 }
61 foreach ($duplicates as $key => $bool) {
62 unset($phones[$key]);
63 }
64 if (count($phones) != $count) {
65 $deleted += ($count - count($phones));
66 Phone::deletePhones($pid, 'user');
67 $id = 0;
68 foreach ($phones as $phone) {
69 $phone->setId($id);
70 $phone->save();
71 ++$id;
72 }
73 XDB::execute('UPDATE IGNORE profile_merge_issues
74 SET issues = REPLACE(issues, \'phone\', \'\')
75 WHERE pid = {?}', $pid);
76 }
77 unset($duplicates);
78 unset($phones);
79
80 ++$done;
81 ++$aux;
82 if ($aux == 10) {
83 $aux = 0;
84 printf("\r%u / %u", $done, $total);
85 }
86 }
87 printf("\r%u / %u", $done, $total);
88 print "\n$deleted phones deleted.\n\n";
89
90 print "That's all folks!\n";
91
92 /* vim:set et sw=4 sts=4 ts=4: */
93 ?>