mirror of
https://github.com/tursom/typecho-plugin-Access.git
synced 2024-12-28 05:40:40 +08:00
Merge pull request #19 from izhizheng/master
This commit is contained in:
commit
766c29c2dc
132
Plugin.php
132
Plugin.php
@ -110,64 +110,88 @@ class Access_Plugin implements Typecho_Plugin_Interface
|
||||
}
|
||||
$db = Typecho_Db::get();
|
||||
$adapterName = $db->getAdapterName();
|
||||
if (false === strpos($adapterName, 'Mysql')) {
|
||||
throw new Typecho_Plugin_Exception(_t('你的适配器为%s,目前只支持Mysql', $adapterName));
|
||||
}
|
||||
|
||||
$prefix = $db->getPrefix();
|
||||
$scripts = file_get_contents('usr/plugins/Access/sql/Mysql.sql');
|
||||
$scripts = str_replace('typecho_', $prefix, $scripts);
|
||||
$scripts = str_replace('%charset%', 'utf8', $scripts);
|
||||
$scripts = explode(';', $scripts);
|
||||
try {
|
||||
$configLink = '<a href="' . Helper::options()->adminUrl . 'options-plugin.php?config=Access">' . _t('前往设置') . '</a>';
|
||||
# 初始化数据库如果不存在
|
||||
if (!$db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access_log';", Typecho_Db::READ))) {
|
||||
foreach ($scripts as $script) {
|
||||
$script = trim($script);
|
||||
if ($script) {
|
||||
$db->query($script, Typecho_Db::WRITE);
|
||||
}
|
||||
}
|
||||
$msg = _t('成功创建数据表,插件启用成功,') . $configLink;
|
||||
}
|
||||
# 处理旧版本数据
|
||||
if ($db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access';", Typecho_Db::READ))) {
|
||||
$rows = $db->fetchAll($db->select()->from('table.access'));
|
||||
foreach ($rows as $row) {
|
||||
$ua = new Access_UA($row['ua']);
|
||||
$time = Helper::options()->gmtTime + (Helper::options()->timezone - Helper::options()->serverTimezone);
|
||||
$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'] = bindec(decbin(ip2long($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']);
|
||||
try {
|
||||
$db->query($db->insert('table.access_log')->rows($row));
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
if ($e->getCode() != 23000) {
|
||||
throw new Typecho_Plugin_Exception(_t('导入旧版数据失败,插件启用失败,错误信息:%s。', $e->getMessage()));
|
||||
|
||||
if (strpos($adapterName, 'Mysql') !== false) {
|
||||
$prefix = $db->getPrefix();
|
||||
$scripts = file_get_contents('usr/plugins/Access/sql/Mysql.sql');
|
||||
$scripts = str_replace('typecho_', $prefix, $scripts);
|
||||
$scripts = str_replace('%charset%', 'utf8', $scripts);
|
||||
$scripts = explode(';', $scripts);
|
||||
try {
|
||||
$configLink = '<a href="' . Helper::options()->adminUrl . 'options-plugin.php?config=Access">' . _t('前往设置') . '</a>';
|
||||
# 初始化数据库如果不存在
|
||||
if (!$db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access_log';", Typecho_Db::READ))) {
|
||||
foreach ($scripts as $script) {
|
||||
$script = trim($script);
|
||||
if ($script) {
|
||||
$db->query($script, Typecho_Db::WRITE);
|
||||
}
|
||||
|
||||
}
|
||||
$msg = _t('成功创建数据表,插件启用成功,') . $configLink;
|
||||
}
|
||||
$db->query("DROP TABLE `{$prefix}access`;", Typecho_Db::WRITE);
|
||||
$msg = _t('成功创建数据表并更新数据,插件启用成功,') . $configLink;
|
||||
# 处理旧版本数据
|
||||
if ($db->fetchRow($db->query("SHOW TABLES LIKE '{$prefix}access';", Typecho_Db::READ))) {
|
||||
$rows = $db->fetchAll($db->select()->from('table.access'));
|
||||
foreach ($rows as $row) {
|
||||
$ua = new Access_UA($row['ua']);
|
||||
$time = Helper::options()->gmtTime + (Helper::options()->timezone - Helper::options()->serverTimezone);
|
||||
$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' ] = bindec(decbin(ip2long($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']);
|
||||
try {
|
||||
$db->query($db->insert('table.access_log')->rows($row));
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
if ($e->getCode() != 23000)
|
||||
throw new Typecho_Plugin_Exception(_t('导入旧版数据失败,插件启用失败,错误信息:%s。', $e->getMessage()));
|
||||
}
|
||||
}
|
||||
$db->query("DROP TABLE `{$prefix}access`;", Typecho_Db::WRITE);
|
||||
$msg = _t('成功创建数据表并更新数据,插件启用成功,') . $configLink;
|
||||
}
|
||||
return $msg;
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
throw new Typecho_Plugin_Exception(_t('数据表建立失败,插件启用失败,错误信息:%s。', $e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
throw new Typecho_Plugin_Exception($e->getMessage());
|
||||
}
|
||||
return $msg;
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
throw new Typecho_Plugin_Exception(_t('数据表建立失败,插件启用失败,错误信息:%s。', $e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
throw new Typecho_Plugin_Exception($e->getMessage());
|
||||
} else if (strpos($adapterName, 'SQLite') !== false) {
|
||||
$prefix = $db->getPrefix();
|
||||
$scripts = file_get_contents('usr/plugins/Access/sql/SQLite.sql');
|
||||
$scripts = str_replace('typecho_', $prefix, $scripts);
|
||||
$scripts = explode(';', $scripts);
|
||||
try {
|
||||
$configLink = '<a href="' . Helper::options()->adminUrl . 'options-plugin.php?config=Access">' . _t('前往设置') . '</a>';
|
||||
# 初始化数据库如果不存在
|
||||
if (!$db->fetchRow($db->query("SELECT name FROM sqlite_master WHERE TYPE='table' AND name='{$prefix}access_log';", Typecho_Db::READ))) {
|
||||
foreach ($scripts as $script) {
|
||||
$script = trim($script);
|
||||
if ($script) {
|
||||
$db->query($script, Typecho_Db::WRITE);
|
||||
}
|
||||
}
|
||||
$msg = _t('成功创建数据表,插件启用成功,') . $configLink;
|
||||
} else {
|
||||
$msg = _t('数据表已经存在,插件启用成功,') . $configLink;
|
||||
}
|
||||
return $msg;
|
||||
} catch (Typecho_Db_Exception $e) {
|
||||
throw new Typecho_Plugin_Exception(_t('数据表建立失败,插件启用失败,错误信息:%s。', $e->getMessage()));
|
||||
} catch (Exception $e) {
|
||||
throw new Typecho_Plugin_Exception($e->getMessage());
|
||||
}
|
||||
} else {
|
||||
throw new Typecho_Plugin_Exception(_t('你的适配器为%s,目前只支持Mysql和SQLite', $adapterName));
|
||||
}
|
||||
}
|
||||
|
||||
|
36
sql/SQLite.sql
Normal file
36
sql/SQLite.sql
Normal file
@ -0,0 +1,36 @@
|
||||
CREATE TABLE `typecho_access_log` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
`ua` varchar(255) default '' ,
|
||||
`browser_id` varchar(32) default '' ,
|
||||
`browser_version` varchar(32) default '' ,
|
||||
`os_id` varchar(32) default '' ,
|
||||
`os_version` varchar(32) default '' ,
|
||||
`url` varchar(255) default '' ,
|
||||
`path` varchar(255) default '' ,
|
||||
`query_string` varchar(255) default '' ,
|
||||
`ip` int(32) default '0' ,
|
||||
`entrypoint` varchar(255) default '' ,
|
||||
`entrypoint_domain` varchar(100) default '' ,
|
||||
`referer` varchar(255) default '' ,
|
||||
`referer_domain` varchar(100) default '' ,
|
||||
`time` int(32) default '0' ,
|
||||
`content_id` int(10) default NULL,
|
||||
`meta_id` int(10) default NULL,
|
||||
`robot` tinyint(1) default '0' ,
|
||||
`robot_id` varchar(32) default '' ,
|
||||
`robot_version` varchar(32) default ''
|
||||
);
|
||||
CREATE INDEX `typecho_access_log_time` ON `typecho_access_log` (`time` );
|
||||
CREATE INDEX `typecho_access_log_path` ON `typecho_access_log` (`path` );
|
||||
CREATE INDEX `typecho_access_log_ip_ua` ON `typecho_access_log` (`ip`, `ua` );
|
||||
CREATE INDEX `typecho_access_log_robot` ON `typecho_access_log` (`robot`, `time` );
|
||||
CREATE INDEX `typecho_access_log_os_id` ON `typecho_access_log` (`os_id` );
|
||||
CREATE INDEX `typecho_access_log_robot_id` ON `typecho_access_log` (`robot_id` );
|
||||
CREATE INDEX `typecho_access_log_browser_id` ON `typecho_access_log` (`browser_id` );
|
||||
CREATE INDEX `typecho_access_log_content_id` ON `typecho_access_log` (`content_id` );
|
||||
CREATE INDEX `typecho_access_log_meta_id` ON `typecho_access_log` (`meta_id` );
|
||||
CREATE INDEX `typecho_access_log_entrypoint` ON `typecho_access_log` (`entrypoint` );
|
||||
CREATE INDEX `typecho_access_log_entrypoint_domain` ON `typecho_access_log` (`entrypoint_domain`);
|
||||
CREATE INDEX `typecho_access_log_referer` ON `typecho_access_log` (`referer` );
|
||||
CREATE INDEX `typecho_access_log_referer_domain` ON `typecho_access_log` (`referer_domain` );
|
||||
COMMIT;
|
Loading…
Reference in New Issue
Block a user