From eab6599075bea1033c5ba9a3495e5a11ba03b271 Mon Sep 17 00:00:00 2001 From: zhangsan <646228430@qq.com> Date: Thu, 20 Mar 2025 10:28:44 +0800 Subject: [PATCH] =?UTF-8?q?3.20=20category=E5=92=8Ctitle=E4=B8=80=E6=A0=B7?= =?UTF-8?q?=E5=88=99=E6=9B=B4=E6=96=B0=EF=BC=8C=E5=90=A6=E5=88=99=E6=8F=92?= =?UTF-8?q?=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../typecho_direct_mysql_publisher.py | 92 +++++++++++-------- 1 file changed, 53 insertions(+), 39 deletions(-) diff --git a/typecho_markdown_upload/typecho_direct_mysql_publisher.py b/typecho_markdown_upload/typecho_direct_mysql_publisher.py index 6b119b9..76021e0 100644 --- a/typecho_markdown_upload/typecho_direct_mysql_publisher.py +++ b/typecho_markdown_upload/typecho_direct_mysql_publisher.py @@ -34,9 +34,6 @@ class TypechoDirectMysqlPublisher: }) def __get_category_id(self, category_name): - """ - 从 self.__exist_categories 查找匹配的分类 ID - """ for item in self.__exist_categories: if item['name'] == category_name: return item['mid'] @@ -84,18 +81,17 @@ class TypechoDirectMysqlPublisher: def publish_post(self, title, content, category): """ - 如果同一分类下 (category) 已存在相同 title,则直接返回已存在的 cid; - 否则插入新文章并返回新 cid。 + 如果 (category, title) 已存在 → 更新旧文章 + 否则 → 插入新文章 """ cursor = self.__db.cursor() - # 1. 获取分类 ID(不存在则插入) + # 1. 获取分类 ID(若不存在则新建) mid = self.__get_category_id(category) if mid < 0: mid = self.__add_category(category) - # 2. 查重:同一分类下 (mid) 是否已存在相同 title - # 通过连接 contents & relationships 表判断 + # 2. 查找同一分类下,是否已存在相同 title 的文章 check_sql = """ SELECT c.cid FROM %s c @@ -111,44 +107,62 @@ class TypechoDirectMysqlPublisher: ) cursor.execute(check_sql) exist_row = cursor.fetchone() - if exist_row: - # 已有同标题文章,直接返回 - print(f"[INFO] 发现同一分类下已存在相同标题: {title}, cid={exist_row[0]},跳过插入。") - return exist_row[0] - # 3. 插入新文章 now_time_int = int(time.time()) content = '' + content - insert_sql = ( - "INSERT INTO %s " - "(`title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) " - "VALUES " - "('%s', NULL, %d, %d, '%s', 0, 1, NULL, 'post', 'publish', NULL, 0, '1', '1', '1', 0)" - ) % ( - self.__contents_table_name, - escape_string(title), - now_time_int, - now_time_int, - escape_string(content) - ) - cursor.execute(insert_sql) - cid = cursor.lastrowid + if exist_row: + # ========== 执行更新逻辑 ========== + cid = exist_row[0] + update_sql = """ + UPDATE %s + SET modified=%d, + text='%s' + WHERE cid=%d + """ % ( + self.__contents_table_name, + now_time_int, + escape_string(content), + cid + ) + cursor.execute(update_sql) - # 4. 更新 slug = cid - update_slug_sql = ( - "UPDATE %s SET slug=%d WHERE cid=%d" - ) % (self.__contents_table_name, cid, cid) - cursor.execute(update_slug_sql) + # 如果你需要修改 slug、authorId、status 等字段,可在这里加上 + # 不需要改 relationships (分类关系) 和分类计数,因为 category 没变 - # 5. 建立文章与分类的关系 - self.__insert_relationship(cursor, cid, mid) + print(f"[INFO] 更新文章成功: title={title}, cid={cid}, category={category}") - # 6. 更新分类下文章数 - self.__update_category_count(cursor, mid) + else: + # ========== 执行插入逻辑 ========== + insert_sql = ( + "INSERT INTO %s " + "(`title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) " + "VALUES " + "('%s', NULL, %d, %d, '%s', 0, 1, NULL, 'post', 'publish', NULL, 0, '1', '1', '1', 0)" + ) % ( + self.__contents_table_name, + escape_string(title), + now_time_int, + now_time_int, + escape_string(content) + ) + cursor.execute(insert_sql) + cid = cursor.lastrowid - # 7. 提交 + # slug = cid + update_slug_sql = ( + "UPDATE %s SET slug=%d WHERE cid=%d" + ) % (self.__contents_table_name, cid, cid) + cursor.execute(update_slug_sql) + + # 建立文章与分类的关系 + self.__insert_relationship(cursor, cid, mid) + + # 分类下文章数 +1 + self.__update_category_count(cursor, mid) + + print(f"[INFO] 插入新文章成功: title={title}, cid={cid}, category={category}") + + # 3. 提交事务 self.__db.commit() - - print(f"[INFO] 插入新文章成功: title={title}, cid={cid}, category={category}") return cid