-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.php
More file actions
144 lines (128 loc) · 4.78 KB
/
database.php
File metadata and controls
144 lines (128 loc) · 4.78 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
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
<?php
/**
* CloudScale Analytics - Database
*
* Creates the wp_cspv_views_v2 and wp_cspv_referrers_v2 tables.
* Plugin uses V2 hourly bucket schema exclusively.
*
* @package CloudScale_Free_Analytics
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Run on plugin activation: create all database tables and seed the version option.
*
* @since 1.0.0
* @return void
*/
function cspv_activate() {
cspv_create_table_v2();
cspv_create_table_referrers_v2();
cspv_create_table_geo_v2();
cspv_create_table_visitors_v2();
cspv_create_table_404_v2();
add_option( 'cspv_version', CSPV_VERSION );
}
/**
* Create the V2 hourly bucketed views table.
*
* One row per post per hour per source, with a view_count column.
* Imported data lands as a single row with the full count on the import hour.
* Tracked views increment the bucket for the current hour.
*
* Schema:
* post_id — the post
* viewed_at — hour bucket, always truncated to :00:00
* view_count — number of views in this bucket (1 for real time, N for imports)
* source — 'tracked' | 'imported' | 'manual'
*/
function cspv_create_table_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cspv_views_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATETIME NOT NULL COMMENT 'Hour bucket, always :00:00',
view_count INT UNSIGNED NOT NULL DEFAULT 1,
source VARCHAR(32) NOT NULL DEFAULT 'tracked',
PRIMARY KEY (id),
UNIQUE KEY post_hour_source (post_id, viewed_at, source),
KEY viewed_at (viewed_at),
KEY post_id (post_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* V2 referrer table: one row per post + hour + referrer.
*/
function cspv_create_table_referrers_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cspv_referrers_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATETIME NOT NULL COMMENT 'Hour bucket, always :00:00',
referrer VARCHAR(512) NOT NULL DEFAULT '',
view_count INT UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY post_hour_ref (post_id, viewed_at, referrer(191)),
KEY viewed_at (viewed_at),
KEY post_id (post_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Create the geo tracking table.
*
* Stores country code per post per hour bucket. Country is resolved
* from the CloudFlare CF-IPCountry header (no raw IP stored).
* Later can be extended with city/lat/lng from DB-IP Lite.
*/
function cspv_create_table_geo_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cspv_geo_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATETIME NOT NULL COMMENT 'Hour bucket',
country_code CHAR(2) NOT NULL DEFAULT '',
view_count INT UNSIGNED NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY post_hour_country (post_id, viewed_at, country_code),
KEY country_code (country_code),
KEY viewed_at (viewed_at)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}
/**
* Create the unique visitors tracking table.
*
* Stores a SHA256 hash of the visitor IP per post per calendar day.
* The unique key ensures the same visitor viewing the same post
* on the same day only creates one row (INSERT IGNORE).
* No raw IP is ever stored.
*/
function cspv_create_table_visitors_v2() {
global $wpdb;
$table = $wpdb->prefix . 'cspv_visitors_v2';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS {$table} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
visitor_hash CHAR(64) NOT NULL,
post_id BIGINT(20) UNSIGNED NOT NULL,
viewed_at DATE NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY hash_post_day (visitor_hash, post_id, viewed_at),
KEY viewed_at (viewed_at),
KEY post_id (post_id)
) {$charset_collate};";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $sql );
}