Repository navigation
Expand file tree
/
Copy pathDatabase.php
More file actions
281 lines (261 loc) · 10.4 KB
/
Copy pathDatabase.php
File metadata and controls
281 lines (261 loc) · 10.4 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
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
}
/**
* Database installation and migration helpers shared by MySQL and SQLite.
*/
class VisitorLoggerPro_Database
{
public static function install()
{
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$table = self::quoteIdentifier($prefix . 'visitor_log');
if (self::isSQLite($db)) {
$sql = "CREATE TABLE IF NOT EXISTS {$table} ("
. 'id INTEGER PRIMARY KEY AUTOINCREMENT,'
. 'ip VARCHAR(45) NOT NULL,'
. 'route VARCHAR(255) NOT NULL,'
. 'country VARCHAR(100) DEFAULT NULL,'
. 'region VARCHAR(100) DEFAULT NULL,'
. 'city VARCHAR(100) DEFAULT NULL,'
. 'isp VARCHAR(150) DEFAULT NULL,'
. "visitor_hash CHAR(32) NOT NULL DEFAULT '',"
. "user_agent TEXT DEFAULT '',"
. 'time DATETIME NOT NULL'
. ')';
} else {
$sql = "CREATE TABLE IF NOT EXISTS {$table} ("
. '`id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,'
. '`ip` VARCHAR(45) NOT NULL,'
. '`route` VARCHAR(255) NOT NULL,'
. '`country` VARCHAR(100) DEFAULT NULL,'
. '`region` VARCHAR(100) DEFAULT NULL,'
. '`city` VARCHAR(100) DEFAULT NULL,'
. '`isp` VARCHAR(150) DEFAULT NULL,'
. "`visitor_hash` CHAR(32) NOT NULL DEFAULT '',"
. "`user_agent` TEXT NULL,"
. '`time` DATETIME NOT NULL'
. ') ENGINE=InnoDB DEFAULT CHARSET=utf8mb4';
}
$db->query($sql, Typecho_Db::WRITE);
self::addMissingColumns($db, $prefix);
self::createIndexes($db, $prefix);
self::backfillVisitorHashes($db, $prefix);
self::migrateLocations($db, $prefix);
}
public static function isSQLite($db = null)
{
$db = $db ?: Typecho_Db::get();
return stripos($db->getAdapterName(), 'sqlite') !== false;
}
public static function visitorHash($ip, $userAgent)
{
return md5((string) $ip . "\x1f" . (string) $userAgent);
}
/**
* File upgrades can happen while the plugin remains active. Ensure the
* new column exists before the first post-upgrade visitor is inserted.
*/
public static function ensureIspColumn()
{
static $ready = false;
if ($ready) {
return;
}
$db = Typecho_Db::get();
$prefix = $db->getPrefix();
$columns = self::getColumns($db, $prefix);
if (!isset($columns['isp'])) {
$table = self::quoteIdentifier($prefix . 'visitor_log');
try {
$db->query(
"ALTER TABLE {$table} ADD COLUMN `isp` VARCHAR(150) DEFAULT NULL",
Typecho_Db::WRITE
);
} catch (Exception $e) {
// A concurrent request may have completed the same migration.
$columns = self::getColumns($db, $prefix);
if (!isset($columns['isp'])) {
throw $e;
}
}
}
$ready = true;
}
public static function siteDate($format = 'Y-m-d H:i:s', $timestamp = null)
{
$timestamp = $timestamp === null ? time() : (int) $timestamp;
$timezone = Helper::options()->timezone;
if (is_numeric($timezone)) {
return gmdate($format, $timestamp + (int) $timezone);
}
try {
$date = new DateTime('@' . $timestamp);
$date->setTimezone(new DateTimeZone((string) $timezone));
return $date->format($format);
} catch (Exception $e) {
return date($format, $timestamp);
}
}
/**
* Run retention cleanup at most once per calendar day.
*/
public static function maybeCleanup($force = false)
{
$options = Helper::options();
$pluginOptions = $options->plugin('VisitorLoggerPro');
$enabled = !isset($pluginOptions->autoCleanup) || (string) $pluginOptions->autoCleanup === '1';
if (!$enabled) {
return 0;
}
$days = isset($pluginOptions->retentionDays) ? (int) $pluginOptions->retentionDays : 90;
$days = max(1, min(3650, $days));
$today = self::siteDate('Y-m-d');
$lastRun = isset($options->visitorLoggerProLastCleanup) ? (string) $options->visitorLoggerProLastCleanup : '';
if (!$force && $lastRun === $today) {
return 0;
}
$db = Typecho_Db::get();
$deleted = $db->query(
$db->delete('table.visitor_log')->where('time < ?', self::siteDate('Y-m-d H:i:s', time() - $days * 86400))
);
self::setRuntimeOption($db, 'visitorLoggerProLastCleanup', $today);
return (int) $deleted;
}
private static function addMissingColumns($db, $prefix)
{
$columns = self::getColumns($db, $prefix);
$definitions = array(
'region' => 'VARCHAR(100) DEFAULT NULL',
'city' => 'VARCHAR(100) DEFAULT NULL',
'isp' => 'VARCHAR(150) DEFAULT NULL',
'user_agent' => self::isSQLite($db) ? "TEXT DEFAULT ''" : 'TEXT NULL',
'visitor_hash' => "CHAR(32) NOT NULL DEFAULT ''"
);
$table = self::quoteIdentifier($prefix . 'visitor_log');
foreach ($definitions as $name => $definition) {
if (!isset($columns[$name])) {
$db->query(
"ALTER TABLE {$table} ADD COLUMN " . self::quoteIdentifier($name) . " {$definition}",
Typecho_Db::WRITE
);
}
}
}
private static function getColumns($db, $prefix)
{
$tableName = $prefix . 'visitor_log';
$rows = self::isSQLite($db)
? $db->fetchAll('PRAGMA table_info(' . self::quoteIdentifier($tableName) . ')')
: $db->fetchAll('SHOW COLUMNS FROM ' . self::quoteIdentifier($tableName));
$columns = array();
foreach ($rows as $row) {
$name = self::isSQLite($db) ? $row['name'] : $row['Field'];
$columns[$name] = true;
}
return $columns;
}
private static function createIndexes($db, $prefix)
{
$tableName = $prefix . 'visitor_log';
$indexes = array(
'idx_vlp_time' => array('time'),
'idx_vlp_ip_time' => array('ip', 'time'),
'idx_vlp_country' => array('country'),
'idx_vlp_region' => array('region'),
'idx_vlp_route' => array('route'),
'idx_vlp_visitor_hash' => array('visitor_hash')
);
if (self::isSQLite($db)) {
foreach ($indexes as $name => $columns) {
$quoted = array_map(array(__CLASS__, 'quoteIdentifier'), $columns);
$db->query(
'CREATE INDEX IF NOT EXISTS ' . self::quoteIdentifier($name) . ' ON '
. self::quoteIdentifier($tableName) . ' (' . implode(', ', $quoted) . ')',
Typecho_Db::WRITE
);
}
return;
}
$existing = $db->fetchAll('SHOW INDEX FROM ' . self::quoteIdentifier($tableName));
$existingNames = array();
foreach ($existing as $row) {
$existingNames[$row['Key_name']] = true;
}
foreach ($indexes as $name => $columns) {
if (!isset($existingNames[$name])) {
// 兼容仍使用 767 字节索引上限的旧版 MySQL。
$quoted = $name === 'idx_vlp_route'
? array(self::quoteIdentifier('route') . '(100)')
: array_map(array(__CLASS__, 'quoteIdentifier'), $columns);
$db->query(
'ALTER TABLE ' . self::quoteIdentifier($tableName) . ' ADD INDEX '
. self::quoteIdentifier($name) . ' (' . implode(', ', $quoted) . ')',
Typecho_Db::WRITE
);
}
}
}
private static function backfillVisitorHashes($db, $prefix)
{
do {
$rows = $db->fetchAll(
$db->select('id', 'ip', 'user_agent')
->from($prefix . 'visitor_log')
->where('visitor_hash IS NULL OR visitor_hash = ?', '')
->limit(500)
);
foreach ($rows as $row) {
$db->query(
$db->update($prefix . 'visitor_log')
->rows(array('visitor_hash' => self::visitorHash($row['ip'], $row['user_agent'])))
->where('id = ?', (int) $row['id'])
);
}
} while (count($rows) === 500);
}
private static function migrateLocations($db, $prefix)
{
if (!class_exists('VisitorLoggerPro_Location')) {
require_once dirname(__FILE__) . '/Location.php';
}
$lastId = 0;
do {
$rows = $db->fetchAll(
$db->select('id', 'country', 'region', 'city', 'isp')
->from($prefix . 'visitor_log')
->where('id > ?', $lastId)
->where('(region IS NULL OR region = ? OR city IS NULL OR city = ?)', '', '')
->order('id', Typecho_Db::SORT_ASC)
->limit(500)
);
foreach ($rows as $row) {
$lastId = (int) $row['id'];
$location = VisitorLoggerPro_Location::parse(
$row['country'], $row['region'], $row['city'], $row['isp']
);
$db->query(
$db->update($prefix . 'visitor_log')->rows($location)->where('id = ?', $lastId)
);
}
} while (count($rows) === 500);
}
private static function setRuntimeOption($db, $name, $value)
{
$exists = $db->fetchRow($db->select('name')->from('table.options')->where('name = ?', $name)->limit(1));
if ($exists) {
$db->query($db->update('table.options')->rows(array('value' => $value))->where('name = ?', $name));
} else {
$db->query($db->insert('table.options')->rows(array('name' => $name, 'user' => 0, 'value' => $value)));
}
}
public static function quoteIdentifier($identifier)
{
if (!preg_match('/^[A-Za-z0-9_]+$/', $identifier)) {
throw new InvalidArgumentException('Invalid database identifier');
}
return '`' . $identifier . '`';
}
}