Files

114 lines
4.0 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp <admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace addon\seo;
use think\facade\Db;
use ywxapp\AddonBase;
use ywxapp\model\BaseModel;
/**
* SEO增强插件
*
* 安装时框架自动导入 install.sql 建表(wxapp_seo_config / keywords / snapshots);
* 卸载按统一前缀清理全部表;升级时经 callAddonHook('upgrade') 收敛表结构。
*/
class Addon extends addon
{
/**
* 安装钩子(框架自动导入 install.sql 后调用)。
*/
public function install(): bool
{
return true;
}
/**
* 卸载钩子:清理本插件全部表。
* 与 install.sql 表名前缀严格对齐(wxapp_seo_),直接复用统一前缀,
* 避免依赖 database.prefix 配置(install.sql 为硬写前缀)。
*/
public function uninstall(): bool
{
$prefix = 'wxapp_seo_';
$tables = [
'config', // SEO 配置表
'keywords', // 内链关键词表
'snapshots', // 收录排名快照表
];
foreach ($tables as $t) {
try {
Db::execute("DROP TABLE IF EXISTS `{$prefix}{$t}`");
} catch (\Exception $e) {
// 忽略
}
}
return true;
}
/**
* 升级时确保全部表存在(读 install.sqlCREATE TABLE IF NOT EXISTS 幂等建表)。
* 单一事实源 = install.sql,避免复制 DDL 导致漂移。
*/
private function ensureTablesFromInstallSql(): void
{
$sqlFile = __DIR__ . DIRECTORY_SEPARATOR . 'install.sql';
if (!is_file($sqlFile)) {
return;
}
$content = (string) file_get_contents($sqlFile);
$content = preg_replace('/--.*|\/\*[\s\S]*?\*\//', '', $content);
$stmts = array_filter(
array_map('trim', explode(';', $content)),
function ($s) {
return strlen($s) > 5 && preg_match('/^CREATE\s+TABLE/i', $s);
}
);
foreach ($stmts as $sql) {
if (preg_match('/CREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?`?([\w]+)`?/i', $sql, $m)) {
BaseModel::ensureTable($m[1], $sql);
}
}
}
/**
* 升级钩子(在线升级 / 后台手动升级均会触发)。
*
* 在线升级(AddonService::onlineUpgrade)已重导 install.sql 补全表;
* 但「已存在表新增列」install.sql 的 CREATE TABLE IF NOT EXISTS 对已有表无效。
* 此处作为升级收敛点:确保全部表存在 + 对已知易漂移列做幂等补列兜底。
* 未来新增列统一在 $columnFixes 登记(须带 DEFAULT x 或 NULL,保证降级安全),
* 避免各控制器/模型重复 ensureColumn/ALTER 导致 DDL 漂移。
*
* @param string $currentVersion 升级前版本号
*/
public function upgrade($currentVersion = ''): bool
{
// 1) 确保全部表存在(幂等,单一事实源 install.sql
$this->ensureTablesFromInstallSql();
// 2) 补列兜底:'完整表名(含前缀)' => ['列名' => '列定义']
// BaseModel::ensureColumn 先探测存在性,重复执行安全。
// install.sql 当前已含全部列,故此处留空;新增列在此登记即可。
$columnFixes = [
// 'wxapp_seo_keywords' => [
// 'new_col' => "varchar(50) NOT NULL DEFAULT '' COMMENT '示例'",
// ],
];
foreach ($columnFixes as $table => $cols) {
foreach ($cols as $column => $def) {
BaseModel::ensureColumn($table, $column, $def);
}
}
return true;
}
}