-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupdate.php
More file actions
111 lines (87 loc) · 4.16 KB
/
Copy pathupdate.php
File metadata and controls
111 lines (87 loc) · 4.16 KB
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
<?php
$addon = rex_addon::get('statistics');
$previousRuntimePause = (bool) rex_config::get('statistics', 'statistics_pause_tracking_runtime', false);
rex_config::set('statistics', 'statistics_pause_tracking_runtime', true);
try {
$sql = rex_sql::factory();
$tableExists = static function (string $table): bool {
if ('' === $table) {
return false;
}
return rex_sql_table::get($table)->exists();
};
$deduplicateCountTable = static function (string $table, array $keyColumns, string $countColumn = 'count') use ($sql, $tableExists): void {
if (!$tableExists($table)) {
return;
}
$quotedKeys = array_map([$sql, 'escapeIdentifier'], $keyColumns);
$groupBy = implode(', ', $quotedKeys);
$columns = implode(', ', array_merge($quotedKeys, [$sql->escapeIdentifier($countColumn)]));
$duplicates = $sql->getValue(
'SELECT COUNT(*) FROM ('
. 'SELECT 1 FROM ' . $sql->escapeIdentifier($table)
. ' GROUP BY ' . $groupBy
. ' HAVING COUNT(*) > 1'
. ') AS duplicate_rows'
);
if ((int) $duplicates === 0) {
return;
}
$tempTable = $table . '_dedup_tmp';
$sql->setQuery('DROP TEMPORARY TABLE IF EXISTS ' . $sql->escapeIdentifier($tempTable));
$sql->setQuery(
'CREATE TEMPORARY TABLE ' . $sql->escapeIdentifier($tempTable) . ' AS '
. 'SELECT ' . $groupBy . ', SUM(' . $sql->escapeIdentifier($countColumn) . ') AS ' . $sql->escapeIdentifier($countColumn)
. ' FROM ' . $sql->escapeIdentifier($table)
. ' GROUP BY ' . $groupBy
);
$sql->setQuery('TRUNCATE TABLE ' . $sql->escapeIdentifier($table));
$sql->setQuery(
'INSERT INTO ' . $sql->escapeIdentifier($table)
. ' (' . $columns . ') '
. 'SELECT ' . $columns . ' FROM ' . $sql->escapeIdentifier($tempTable)
);
};
$deduplicateCountTable(rex::getTable('pagestats_visits_per_day'), ['date', 'domain']);
$deduplicateCountTable(rex::getTable('pagestats_visitors_per_day'), ['date', 'domain']);
$deduplicateCountTable(rex::getTable('pagestats_data'), ['type', 'name']);
$deduplicateCountTable(rex::getTable('pagestats_media'), ['url', 'date']);
$deduplicateCountTable(rex::getTable('pagestats_api'), ['name', 'date']);
$deduplicateCountTable(rex::getTable('pagestats_bot'), ['name', 'category', 'producer']);
// create tables
$addon->includeFile(__DIR__ . '/install.php');
// Cleanup legacy frontend asset that is no longer shipped.
// Important for updates where public addon assets may still contain stale files.
rex_file::delete(rex_path::addonAssets('statistics', 'exceljs.min.js'));
rex_file::delete(rex_path::addonAssets('statistics', 'jspdf.umd.min.js'));
// Keep the new per-URL visitor metric disabled on existing installations
// until editors explicitly enable it in settings.
if (!rex_config::has('statistics', 'statistics_pages_visitors_enabled')) {
rex_config::set('statistics', 'statistics_pages_visitors_enabled', false);
}
// version 3 migrations
// copy old config settings
if (rex_config::has("statistics/api", "statistics_api_enable")) {
rex_config::set("statistics", "statistics_api_enable", rex_config::get("statistics/api", "statistics_api_enable"));
}
// Keep backward compatibility: when the new dedicated campaign setting does not exist yet,
// initialize it from the previous shared API toggle.
if (!rex_config::has('statistics', 'statistics_google_campaigns_enable')) {
rex_config::set(
'statistics',
'statistics_google_campaigns_enable',
(bool) rex_config::get('statistics', 'statistics_api_enable', false)
);
}
if (rex_config::has("statistics/media", "statistics_media_log_all")) {
rex_config::set("statistics", "statistics_media_log_all", rex_config::get("statistics/media", "statistics_media_log_all"));
}
if (rex_config::has("statistics/media", "statistics_media_log_mm")) {
rex_config::set("statistics", "statistics_media_log_mm", rex_config::get("statistics/media", "statistics_media_log_mm"));
}
// remove plugins
rex_dir::delete(rex_path::addon('statistics', 'plugins'));
rex_package_manager::synchronizeWithFileSystem();
} finally {
rex_config::set('statistics', 'statistics_pause_tracking_runtime', $previousRuntimePause);
}