-
Notifications
You must be signed in to change notification settings - Fork 73
/
Access_Migration.php
199 lines (186 loc) · 8.25 KB
/
Access_Migration.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
if (!defined('__ACCESS_PLUGIN_ROOT__')) {
throw new Exception('Bootstrap file not found');
}
class Access_Migration {
/**
* 允许访问的业务函数名及其对应的允许访问方法
* @var string[]
*/
private static $rpcTypes = [
'overview' => 'GET',
'migrate' => 'POST',
];
private $config;
private $db;
private $request;
public function __construct($request) {
$this->db = Typecho_Db::get();
$this->request = $request;
$this->config = Typecho_Widget::widget('Widget_Options')->plugin('Access');
}
/**
* 显示是否存在旧版数据
*
* @access public
* @return bool
* @throws Exception
*/
public function exists()
{
$prefix = $this->db->getPrefix();
$adapterName = $this->db->getAdapterName();
if (strpos($adapterName, 'Mysql') !== false || strpos($adapterName, 'SQLite') !== false) {
if ($this->db->fetchRow($this->db->query("SHOW TABLES LIKE '{$prefix}access';", Typecho_Db::READ))) {
return true;
}
if ($this->db->fetchRow($this->db->query("SHOW TABLES LIKE '{$prefix}access_log';", Typecho_Db::READ))) {
return true;
}
} else if (strpos($adapterName, 'Pgsql') !== false) {
if ($this->db->fetchRow($this->db->query("SELECT to_regclass('public.{$prefix}access');", Typecho_Db::READ))) {
return true;
}
if ($this->db->fetchRow($this->db->query("SELECT to_regclass('public.{$prefix}access_log');", Typecho_Db::READ))) {
return true;
}
}
return false;
}
/**
* 显示旧版本数据数量
*
* @access private
* @return ?array
* @throws Exception
*/
private function overview()
{
$resp = [ 'total' => 0 ];
$prefix = $this->db->getPrefix();
$adapterName = $this->db->getAdapterName();
// 统计 v1 版本数据
if (strpos($adapterName, 'Mysql') !== false || strpos($adapterName, 'SQLite') !== false) {
$checkTableV1 = $this->db->fetchRow($this->db->query("SHOW TABLES LIKE '{$prefix}access';", Typecho_Db::READ));
} else if (strpos($adapterName, 'Pgsql') !== false) {
$checkTableV1 = $this->db->fetchRow($this->db->query("SELECT tablename FROM pg_catalog.pg_tables WHERE tablename = '{$prefix}access';", Typecho_Db::READ));
}
if ($checkTableV1) {
$resp['v1'] = $this->db->fetchRow($this->db->select('COUNT(1) AS cnt')->from('table.access'))['cnt'];
$resp['total'] += $resp['v1'];
}
// 统计 v2 版本数据
if (strpos($adapterName, 'Mysql') !== false || strpos($adapterName, 'SQLite') !== false) {
$checkTableV2 = $this->db->fetchRow($this->db->query("SHOW TABLES LIKE '{$prefix}access_log';", Typecho_Db::READ));
} else if (strpos($adapterName, 'Pgsql') !== false) {
$checkTableV2 = $this->db->fetchRow($this->db->query("SELECT tablename FROM pg_catalog.pg_tables WHERE tablename = '{$prefix}access_log';", Typecho_Db::READ));
}
if ($checkTableV2) {
$resp['v2'] = intval($this->db->fetchRow($this->db->select('COUNT(1) AS cnt')->from('table.access_log'))['cnt']);
$resp['total'] += $resp['v2'];
}
return $resp;
}
/**
* 迁移旧版本数据,单次1000条
*
* @access private
* @return ?array
* @throws Exception
*/
private function migrate()
{
$resp = [ 'count' => 0, 'remain' => 0 ];
$step = 1000;
$prefix = $this->db->getPrefix();
$adapterName = $this->db->getAdapterName();
$showTableQuery = strpos($adapterName, 'Pgsql') !== false ?
"SELECT * FROM information_schema.tables WHERE table_name LIKE '{$prefix}access';" :
"SHOW TABLES LIKE '{$prefix}access';";
// 迁移 v1 版本数据
if ($this->db->fetchRow($this->db->query($showTableQuery, Typecho_Db::READ))) {
$remain = intval($this->db->fetchRow($this->db->select('COUNT(1) AS cnt')->from('table.access'))['cnt']);
if ($resp['count'] === 0) {
$rows = $this->db->fetchAll($this->db->select()->from('table.access')->limit($step));
foreach ($rows as $row) {
$id = $row['id'];
unset($row['id']);
$ua = new Access_UA($row['ua']);
$row['browser_id' ] = $ua->getBrowserID();
$row['browser_version' ] = $ua->getBrowserVersion();
$row['os_id' ] = $ua->getOSID();
$row['os_version' ] = $ua->getOSVersion();
$row['path' ] = parse_url($row['url'], PHP_URL_PATH);
$row['query_string' ] = parse_url($row['url'], PHP_URL_QUERY);
$row['ip' ] = $row['ip'];
$row['entrypoint' ] = $row['referer'];
$row['entrypoint_domain'] = $row['referer_domain'];
$row['time' ] = $row['date'];
$row['robot' ] = $ua->isRobot() ? 1 : 0;
$row['robot_id' ] = $ua->getRobotID();
$row['robot_version' ] = $ua->getRobotVersion();
unset($row['date']);
$this->db->query($this->db->insert('table.access_logs')->rows($row));
$resp['count'] += 1;
$this->db->query($this->db->delete('table.access')->where('id = ?', $id));
$remain -= 1;
}
}
if ($remain === 0) {
$dropTableQuery = strpos($adapterName, 'Pgsql') !== false ?
"DROP TABLE IF EXISTS {$prefix}access;" :
"DROP TABLE `{$prefix}access`;";
$this->db->query($dropTableQuery, Typecho_Db::WRITE);
}
$resp['remain'] += $remain;
}
$showTableQuery = strpos($adapterName, 'Pgsql') !== false ?
"SELECT * FROM information_schema.tables WHERE table_name LIKE '{$prefix}access_log';" :
"SHOW TABLES LIKE '{$prefix}access_log';";
// 迁移 v2 版本数据
if ($this->db->fetchRow($this->db->query($showTableQuery, Typecho_Db::READ))) {
$remain = intval($this->db->fetchRow($this->db->select('COUNT(1) AS cnt')->from('table.access_log'))['cnt']);
if ($resp['count'] === 0) {
$rows = $this->db->fetchAll($this->db->select()->from('table.access_log')->limit($step));
foreach ($rows as $row) {
$id = $row['id'];
unset($row['id']);
$row['ip'] = long2ip($row['ip']);
$this->db->query($this->db->insert('table.access_logs')->rows($row));
$resp['count'] += 1;
$this->db->query($this->db->delete('table.access_log')->where('id = ?', $id));
$remain -= 1;
}
}
if ($remain === 0) {
$dropTableQuery = strpos($adapterName, 'Pgsql') !== false ?
"DROP TABLE IF EXISTS {$prefix}access_log;" :
"DROP TABLE `{$prefix}access_log`;";
$this->db->query($dropTableQuery, Typecho_Db::WRITE);
}
$resp['remain'] += $remain;
}
return $resp;
}
/**
* 业务调度入口
*
* @access public
* @param string rpcType 调用过程类型
* @return ?array
* @throws Exception
*/
public function invoke(string $rpcType): ?array {
if(!method_exists($this, $rpcType) || !array_key_exists($rpcType, Access_Migration::$rpcTypes))
throw new Exception('Bad Request', 400);
$method = Access_Migration::$rpcTypes[$rpcType];
if (
($method === 'GET' && !$this->request->isGet())
|| ($method === 'POST' && !$this->request->isPost())
|| ($method === 'PUT' && !$this->request->isPut())
) {
throw new Exception('Method Not Allowed', 405);
}
return $this->$rpcType();
}
}