/
CFDBCleanupData.php
195 lines (173 loc) · 8.01 KB
/
CFDBCleanupData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/*
"Contact Form to Database" Copyright (C) 2011-2015 Michael Simpson (email : michael.d.simpson@gmail.com)
This file is part of Contact Form to Database.
Contact Form to Database is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Contact Form to Database is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Contact Form to Database.
If not, see <http://www.gnu.org/licenses/>.
*/
require_once('CF7DBPlugin.php');
class CFDBCleanupData {
/**
* @var CF7DBPlugin
*/
var $plugin;
/**
* @param $plugin CF7DBPlugin
*/
function __construct($plugin) {
$this->plugin = $plugin;
}
/**
* Fix entries from different forms with same submit_time
* @return int number of items fixed in the DB
*/
public function cleanupForms() {
global $wpdb;
$table = $this->plugin->getSubmitsTableName();
$sql = sprintf('select * from (select submit_time, count(form_name) as count
from (
select distinct submit_time, form_name from %s) t group by submit_time
) u where count > 1', $table);
$results = $wpdb->get_results($sql, ARRAY_A);
//print_r($results); // debug
if (!$results) {
return 0;
}
$stSql = "select distinct submit_time, form_name from $table where submit_time = %F";
$inDBSql = "select count(submit_time) from $table where submit_time = %F";
$updateSql = "update $table set submit_time = %F where submit_time = %F and form_name = %s";
$count = 0;
foreach ($results as $row) {
$stResults = $wpdb->get_results($wpdb->prepare($stSql, $row['submit_time']), ARRAY_A);
$idx = 0;
foreach ($stResults as $stResult) {
if ($idx++ == 0) {
continue;
}
$newST = $stResult['submit_time'];
while (true) {
$newST = $newST + 0.0001; // Get new submit time
$inDbAlready = $wpdb->get_var($wpdb->prepare($inDBSql, $newST));
if (!$inDbAlready) {
$wpdb->query($wpdb->prepare($updateSql, $newST, $stResult['submit_time'], $stResult['form_name']));
++$count;
break;
}
}
}
}
return $count;
}
public function deleteEmptyEntries() {
$table = $this->plugin->getSubmitsTableName();
global $wpdb;
return $wpdb->query("DELETE FROM $table WHERE field_name = '' AND field_value = ''");
}
/**
* Fix entries in the same form with duplicate submit_time values
* return int
*/
public function cleanupEntries() {
$count = 0;
$table = $this->plugin->getSubmitsTableName();
$dupSql =
"SELECT DISTINCT b.submit_time FROM (
SELECT a.submit_time, a.form_name, a.field_name, count(a.field_value) AS count
FROM $table a
GROUP BY a.submit_time, a.submit_time, a.form_name, a.field_name) b
WHERE b.count > 1";
$deleteWithFieldOrder = "DELETE FROM $table WHERE submit_time = %F AND form_name = %s AND field_name = %s AND field_value = %s AND field_order = %d LIMIT 1";
$deleteNoFieldOrder = "DELETE FROM $table WHERE submit_time = %F AND form_name = %s AND field_name = %s AND field_value = %s AND field_order IS NULL LIMIT 1";
$updateWithFieldOrder = "UPDATE $table SET submit_time = %F WHERE submit_time = %F AND form_name = %s AND field_name = %s AND field_value = %s AND field_order = %d LIMIT 1";
$updateNoFieldOrder = "UPDATE $table SET submit_time = %F WHERE submit_time = %F AND form_name = %s AND field_name = %s AND field_value = %s AND field_order IS NULL LIMIT 1";
$submitTimes = array();
global $wpdb;
$results = $wpdb->get_results($dupSql, ARRAY_A);
foreach ($results as $row) {
$submitTimes[] = $row['submit_time'];
}
$stSql = "SELECT * FROM $table WHERE submit_time = %F";
foreach ($submitTimes as $st) {
$data = array();
$entryNum = 0;
$data[$entryNum] = array();
$results = $wpdb->get_results($wpdb->prepare($stSql, $st));
foreach ($results as $row) {
foreach ($data[$entryNum] as $entry) {
if ($entry->field_name == $row->field_name) {
$entryNum++;
break;
}
}
$data[$entryNum][] = $row;
}
foreach ($data as $idx => $entry) {
//print "\n\n";
//print_r($entry); // debug
$diff = false;
if ($idx > 0) {
// If the entries are identical, delete one.
foreach ($entry as $field) {
$diff = false;
foreach($data[0] as $firstEntryRow) {
if ($field->field_name == $firstEntryRow->field_name) {
if ($field->field_value != $firstEntryRow->field_value) {
$diff = true;
break;
}
}
}
if ($diff) {
break;
}
}
if (!$diff) {
foreach ($entry as $field) {
// Delete duplicate entries
$deleteSql = is_numeric($field->field_order) ?
$wpdb->prepare($deleteWithFieldOrder, $field->submit_time, $field->form_name, $field->field_name, $field->field_value, intval($field->field_order)) :
$wpdb->prepare($deleteNoFieldOrder, $field->submit_time, $field->form_name, $field->field_name, $field->field_value);
//echo "$deleteSql\n"; // debug
$wpdb->query($deleteSql);
}
++$count;
} else {
$newST = $this->getNewSubmitTime($entry[0]->submit_time + 0.0001 * $idx);
foreach ($entry as $field) {
// Give different entries a slightly different submit_time
$updateSql = is_numeric($field->field_order) ?
$wpdb->prepare($updateWithFieldOrder, $newST, $field->submit_time, $field->form_name, $field->field_name, $field->field_value, intval($field->field_order)) :
$wpdb->prepare($updateNoFieldOrder, $newST, $field->submit_time, $field->form_name, $field->field_name, $field->field_value);
//echo "$updateSql\n"; // debug
$wpdb->query($updateSql);
}
++$count;
}
}
}
}
return $count;
}
public function getNewSubmitTime($submitTime) {
global $wpdb;
$table = $this->plugin->getSubmitsTableName();
$inDBSql = 'select count(submit_time) from ' . $table . ' where submit_time = %F';
while (true) {
$submitTime = $submitTime + 0.0001; // Propose new submit time
$inDbAlready = $wpdb->get_var($wpdb->prepare($inDBSql, $submitTime));
if (!$inDbAlready) {
break;
}
}
return $submitTime;
}
}