使用DeepSeek AI大模型修复博客主题两处BUG

使用DeepSeek AI大模型修复博客主题两处BUG

王忘杰
2025-08-20 / 0 评论 / 14 阅读 / 正在检测是否收录...

企业微信截图_20250820151742.jpg

我使用的是Joe主题,项目地址https://github.com/HaoOuBa/Joe,近期将网站程序从AppNode更换为1Panel后,观察到博客存在两个错误日志

报错1
2025/08/20 11:36:35 [error] 105#105: *114111 FastCGI sent in stderr: "PHP message: PHP Notice:  Undefined variable: fontFormat in /www/sites/90apt.com/index/usr/themes/Joe/public/config.php on line 32" while reading response header from upstream, client: 127.0.0.1, server: 90apt.com, request: "GET /5089 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "90apt.com", referrer: 

报错2
2025/08/20 14:26:50 [error] 105#105: *116889 FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Typecho\Db\Adapter\SQLException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DESC LIMIT 8 OFFSET 0' at line 1 in /www/sites/90apt.com/index/var/Typecho/Db/Adapter/Pdo.php:111Stack trace:
#0 /www/sites/90apt.com/index/var/Typecho/Db.php(391): Typecho\Db\Adapter\Pdo->query()
#1 /www/sites/90apt.com/index/var/Typecho/Db.php(424): Typecho\Db->query()
#2 /www/sites/90apt.com/index/usr/themes/Joe/core/route.php(381): Typecho\Db->fetchAll()
#3 /www/sites/90apt.com/index/usr/themes/Joe/core/core.php(70): _getArticleFiling()
#4 /www/sites/90apt.com/index/var/Widget/Archive.php(752): themeInit()
#5 /www/sites/90apt.com/index/var/Typecho/Widget.php(155): Widget\Archive->execute()
#6 /www/sites/90apt.com/index/var/Typecho/Widget.php(209): Typecho\Widget::widget()
#7 /www/sites/90apt.com/index/var/Widget/ExceptionHandle.php(2" while reading response header from upstream,

报错1 AI判断修复

错误解释
这是一个 PHP Notice(通知) 级别的错误,具体原因是:

错误信息:Undefined variable: fontFormat

含义:在代码中使用了一个名为 $fontFormat 的变量,但这个变量在使用之前并没有被定义(即没有赋值)。

发生位置:在文件 /www/sites/90apt.com/index/usr/themes/Joe/public/config.php 的第 32 行。

简单来说,PHP 引擎在执行到第32行时,发现代码想要读取 $fontFormat 的值,但它完全不知道这个变量是什么,因为它从未被创建过,所以抛出了一个通知(Notice)。

在您提供的代码中,我发现了可能导致 Notice 错误的根源。问题出现在 $fontFormat 变量的使用上。

问题分析
在您的代码中,$fontFormat 变量是通过一系列条件判断来赋值的:

php
<?php
$fontUrl = $this->options->JCustomFont;
if (strpos($fontUrl, 'woff2') !== false) $fontFormat = 'woff2';
elseif (strpos($fontUrl, 'woff') !== false) $fontFormat = 'woff';
elseif (strpos($fontUrl, 'ttf') !== false) $fontFormat = 'truetype';
elseif (strpos($fontUrl, 'eot') !== false) $fontFormat = 'embedded-opentype';
elseif (strpos($fontUrl, 'svg') !== false) $fontFormat = 'svg';
?>
问题是:如果 $fontUrl 不包含任何这些扩展名,或者为空,$fontFormat 变量将不会被定义。然后在 CSS 部分:

php
<?php if ($fontFormat) : ?>src: url('<?php echo $fontUrl ?>') format('<?php echo $fontFormat ?>');
<?php endif; ?>
这里直接使用了 $fontFormat 变量,但没有确保它已经被定义。

解决方案
方案1:初始化变量(推荐)
在条件判断之前先初始化变量:

php
<?php
$fontUrl = $this->options->JCustomFont;
$fontFormat = ''; // 初始化变量
if (strpos($fontUrl, 'woff2') !== false) $fontFormat = 'woff2';
elseif (strpos($fontUrl, 'woff') !== false) $fontFormat = 'woff';
elseif (strpos($fontUrl, 'ttf') !== false) $fontFormat = 'truetype';
elseif (strpos($fontUrl, 'eot') !== false) $fontFormat = 'embedded-opentype';
elseif (strpos($fontUrl, 'svg') !== false) $fontFormat = 'svg';
?>
方案2:使用 isset() 检查
修改 CSS 部分的条件判断:

php
<?php if (isset($fontFormat) && $fontFormat) : ?>src: url('<?php echo $fontUrl ?>') format('<?php echo $fontFormat ?>');
<?php endif; ?>

报错2 AI判断修复

这个错误是 SQL 语法错误,具体原因是你的 SQL 查询语句中缺少了 ORDER BY 子句所需的列名。错误信息显示:

sql
DESC LIMIT 8 OFFSET 0
MySQL 期望在 DESC 前有一个具体的列名(例如 ORDER BY created DESC),但当前查询中直接使用了 DESC,导致语法错误。

这个错误是由于在_getArticleFiling函数中的SQL查询语法问题导致的。具体来说,问题出现在第381行附近的SQL语句中,其中GROUP BY子句后面直接使用了DESC,而没有指定排序的列名。

以下是修复方案:

找到这个有问题的SQL语句:

php
$sql = "SELECT FROM_UNIXTIME(created, '%Y 年 %m 月') as date FROM `{$prefix}contents` WHERE created < {$time} AND (password is NULL or password = '') AND status = 'publish' AND type = 'post' GROUP BY FROM_UNIXTIME(created, '%Y 年 %m 月') DESC LIMIT {$pageSize} OFFSET {$offset}";
将其修改为:

php
$sql = "SELECT FROM_UNIXTIME(created, '%Y 年 %m 月') as date FROM `{$prefix}contents` WHERE created < {$time} AND (password is NULL or password = '') AND status = 'publish' AND type = 'post' GROUP BY date ORDER BY date DESC LIMIT {$pageSize} OFFSET {$offset}";
主要修改点:

将GROUP BY FROM_UNIXTIME(created, '%Y 年 %m 月') DESC改为GROUP BY date ORDER BY date DESC

使用别名date进行分组和排序,这样更清晰且符合SQL语法

小结
不懂代码的小白也能修复BUG,AI无敌

0

评论

博主关闭了所有页面的评论