This repository has been archived by the owner on Jan 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathduplicates.php
178 lines (131 loc) · 7.98 KB
/
duplicates.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
<?php
include_once 'data.php';
include_once 'functions.php';
if (isset($_SESSION['permissions']) && $_SESSION['permissions'] == 'A') {
if (!empty($_POST['submit'])) {
if (!isset($_POST['ids']))
$_POST['ids'] = array();
echo '<div class="details alternating_row" style="font-weight:bold">Number of Deleted Duplicates:</div>'
. '<div style="padding:4px 8px">' . count($_POST['ids']) . '</div>'
. '<div style="padding:0 8px"><h3>Done.</h3></div>';
// DELETE ITEMS
database_connect(IL_DATABASE_PATH, 'library');
delete_record($dbHandle, $_POST['ids']);
$dbHandle = null;
} elseif (isset($_GET['find_duplicates']) && $_GET['find_duplicates'] == 'similar') {
echo '<div class="details alternating_row" style="font-weight:bold">Possible Duplicates:</div>'
. '<div style="padding:4px 8px"><input type="checkbox" style="visibility:hidden">'
. '<span style="display:inline-block;margin-left:1em;width:4em">ID</span><span style="margin-left:1em">Title</span></div>';
echo '<form action="duplicates.php" method="POST">';
$duplicates_array = array();
database_connect(IL_DATABASE_PATH, 'library');
$dbHandle->exec("PRAGMA cache_size = 200000");
$dbHandle->exec("PRAGMA temp_store = MEMORY");
$dbHandle->exec("PRAGMA synchronous = OFF");
$dbHandle->exec("CREATE TEMPORARY TABLE search_result (id INTEGER PRIMARY KEY,title)");
$dbHandle->exec("INSERT INTO search_result SELECT id,title FROM library");
$result = $dbHandle->query("SELECT id,title FROM search_result ORDER BY id ASC");
$i = 0;
while ($title = $result->fetch(PDO::FETCH_ASSOC)) {
$title_query = $dbHandle->quote(substr($title['title'], 0, -1) . '%');
$id_result = $dbHandle->query("SELECT id,title FROM search_result WHERE id > $title[id] AND lower(title) LIKE lower($title_query) LIMIT 1");
$id_result = $id_result->fetch(PDO::FETCH_ASSOC);
if (!empty($id_result['id'])) {
$duplicates_array[$i][$title['id']] = $title['title'];
$duplicates_array[$i][$id_result['id']] = $id_result['title'];
$i = $i + 1;
}
}
$dbHandle = null;
while (list($key, $duplicate_pair) = each($duplicates_array)) {
ksort($duplicate_pair);
$id1 = key($duplicate_pair);
$title1 = current($duplicate_pair);
next($duplicate_pair);
$id2 = key($duplicate_pair);
$title2 = current($duplicate_pair);
$duplicate_pair = array();
echo '<div style="padding:4px 8px"><input type="checkbox" name="ids[]" value="' . $id1 . '">'
. '<a href="stable.php?id=' . $id1 . '" target="_blank" style="display:inline-block;margin-left:1em;width:4em">'
. $id1 . '</a><span style="margin-left:1em">' . $title1 . '</span></div>';
echo '<div style="padding:4px 8px"><input type="checkbox" name="ids[]" value="' . $id2 . '">'
. '<a href="stable.php?id=' . $id2 . '" target="_blank" style="display:inline-block;margin-left:1em;width:4em">'
. $id2 . '</a><span style="margin-left:1em">' . $title2 . '</span></div>';
}
if (count($duplicates_array) > 0) {
echo '<div style="padding:8px 8px"><input type="submit" name="submit" value="Delete selected"><br>'
. '<b>Warning! This action cannot be undone. Inspect the items you want to delete for supplementary files and important notes.</b></div>';
} else {
echo '<div style="padding:4px 8px">No duplicates found.</div>';
}
echo '</form>';
} elseif (isset($_GET['find_duplicates']) && $_GET['find_duplicates'] == 'identical') {
echo '<div class="details alternating_row" style="font-weight:bold">Possible Duplicates:</div>'
. '<div style="padding:4px 8px"><input type="checkbox" style="visibility:hidden">'
. '<span style="display:inline-block;margin-left:1em;width:4em">ID</span><span style="margin-left:1em">Title</span></div>';
echo '<form action="duplicates.php" method="POST">';
database_connect(IL_DATABASE_PATH, 'library');
$result = $dbHandle->query("SELECT id,title FROM library WHERE lower(title) IN (SELECT lower(title) FROM library GROUP BY lower(title) HAVING count(*) > 1) ORDER BY title ASC");
$dbHandle = null;
$is_duplicate = false;
while ($row = $result->fetch(PDO::FETCH_NAMED)) {
$is_duplicate = true;
echo '<div style="padding:4px 8px"><input type="checkbox" name="ids[]" value="' . $row['id'] . '">'
. '<a href="stable.php?id=' . $row['id'] . '" target="_blank" style="display:inline-block;margin-left:1em;width:4em">'
. $row['id'] . '</a><span style="margin-left:1em">' . $row['title'] . '</span></div>';
}
if ($is_duplicate) {
echo '<div style="padding:8px 8px"><input type="submit" name="submit" value="Delete selected"><br>'
. '<b>Warning! This action cannot be undone. Inspect the items you want to delete for supplementary files and important notes.</b></div>';
} else {
echo '<div style="padding:4px 8px">No duplicates found.</div>';
}
echo '</form>';
} elseif (isset($_GET['find_duplicates']) && $_GET['find_duplicates'] == 'hash') {
ini_set('max_execution_time', 900);
echo '<div class="details alternating_row" style="font-weight:bold">Possible Duplicates:</div>'
. '<div style="padding:4px 8px"><input type="checkbox" style="visibility:hidden">'
. '<span style="display:inline-block;margin-left:1em;width:4em">ID</span><span style="margin-left:1em">File hash</span></div>';
echo '<form action="duplicates.php" method="POST">';
database_connect(IL_DATABASE_PATH, 'library');
//CALCULATE MD5 HASH FOR EACH PDF IF EMPTY
$result = $dbHandle->query("SELECT id,file,filehash FROM library");
$hashes = array();
while ($row = $result->fetch(PDO::FETCH_NAMED)) {
if (empty($row['filehash']) && is_file(IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($row['file']) . DIRECTORY_SEPARATOR . $row['file'])) {
$hashes[$row['id']] = md5_file(IL_PDF_PATH . DIRECTORY_SEPARATOR . get_subfolder($row['file']) . DIRECTORY_SEPARATOR . $row['file']);
}
}
//SAVE NEW HASHES
$dbHandle->beginTransaction();
while (list($id, $hash) = each($hashes)) {
$hash = $dbHandle->quote($hash);
$id = $dbHandle->quote($id);
$dbHandle->exec("UPDATE library SET filehash=" . $hash . " WHERE id=" . $id);
}
$dbHandle->commit();
$result = null;
$row = null;
$result = $dbHandle->query("SELECT id,filehash FROM library"
. " WHERE filehash IN (SELECT filehash FROM library WHERE filehash!='' GROUP BY filehash HAVING count(*) > 1)"
. " ORDER BY filehash");
$dbHandle = null;
$is_duplicate = false;
while ($row = $result->fetch(PDO::FETCH_NAMED)) {
$is_duplicate = true;
echo '<div style="padding:4px 8px"><input type="checkbox" name="ids[]" value="' . $row['id'] . '">'
. '<a href="stable.php?id=' . $row['id'] . '" target="_blank" style="display:inline-block;margin-left:1em;width:4em">'
. $row['id'] . '</a><span style="margin-left:1em">' . $row['filehash'] . '</span></div>';
}
if ($is_duplicate) {
echo '<div style="padding:8px 8px"><input type="submit" name="submit" value="Delete selected"><br>'
. '<b>Warning! This action cannot be undone. Inspect the items you want to delete for supplementary files and important notes.</b></div>';
} else {
echo '<div style="padding:4px 8px">No duplicates found.</div>';
}
echo '</form>';
}
} else {
echo 'Super user or User permissions required.';
}
?>