/
tracking_exporter.php
134 lines (107 loc) · 3.72 KB
/
tracking_exporter.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
<?php
namespace Podlove\Modules\ImportExport\Export;
class TrackingExporter
{
public static function init()
{
add_action('wp_ajax_podlove-export-tracking', [__CLASS__, 'export_tracking']);
add_action('wp_ajax_podlove-export-tracking-status', [__CLASS__, 'export_tracking_status']);
self::init_download();
}
public static function init_download()
{
if (!is_admin()) {
return;
}
if (isset($_GET['podlove_export_tracking']) && $_GET['podlove_export_tracking']) {
if (!current_user_can('administrator')) {
exit;
}
if (!wp_verify_nonce($_REQUEST['_podlove_nonce'], 'podlove_export_tracking_download')) {
exit;
}
delete_transient('podlove_tracking_export_finished');
header('Content-Type: application/octet-stream');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.TrackingExporter::getDownloadFileName());
header('Cache-control: private');
header('Expires: -1');
readfile(TrackingExporter::get_tracking_export_file_path());
exit;
}
}
public static function get_tracking_export_file_path()
{
$upload_dir = wp_upload_dir();
return $upload_dir['basedir'].DIRECTORY_SEPARATOR.'tracking.tmp';
}
public static function export_tracking()
{
global $wpdb;
if (!current_user_can('administrator')) {
exit;
}
if (!wp_verify_nonce($_REQUEST['_podlove_nonce'], 'podlove_export_tracking')) {
exit;
}
// only one export at a time
if (get_option('podlove_tracking_export_all') !== false) {
exit;
}
update_option('podlove_tracking_export_all', $wpdb->get_var('SELECT COUNT(*) FROM '.\Podlove\Model\DownloadIntent::table_name()));
update_option('podlove_tracking_export_progress', 0);
$rowsPerQuery = 1000;
$lastId = 0;
$page = 0;
$fp = gzopen(self::get_tracking_export_file_path(), 'w');
do {
// Keeping track of the $lastId is (roughly) a bajillion times faster than paging via LIMIT.
$sql = '
SELECT
id,
user_agent_id,
media_file_id,
request_id,
accessed_at,
source,
context,
geo_area_id,
lat,
lng,
httprange
FROM
'.\Podlove\Model\DownloadIntent::table_name().'
WHERE id > '.(int) $lastId."
LIMIT 0, {$rowsPerQuery}";
$rows = $wpdb->get_results($sql, ARRAY_A);
foreach ($rows as $row) {
gzwrite($fp, implode(',', $row)."\n");
}
$lastId = $row['id'];
++$page;
update_option('podlove_tracking_export_progress', $page * $rowsPerQuery);
} while (count($rows) > 0);
gzclose($fp);
set_transient('podlove_tracking_export_finished', true, MINUTE_IN_SECONDS * 3);
delete_option('podlove_tracking_export_all');
delete_option('podlove_tracking_export_progress');
exit;
}
public static function export_tracking_status()
{
echo json_encode([
'all' => get_option('podlove_tracking_export_all'),
'progress' => get_option('podlove_tracking_export_progress'),
'finished' => (bool) get_transient('podlove_tracking_export_finished'),
]);
exit;
}
private static function getDownloadFileName()
{
$sitename = sanitize_key(get_bloginfo('name'));
if (!empty($sitename)) {
$sitename .= '.';
}
return $sitename.'tracking.'.date('Y-m-d').'.csv.gz';
}
}